-
Notifications
You must be signed in to change notification settings - Fork 4
/
regress_hparam.py
146 lines (109 loc) · 4.28 KB
/
regress_hparam.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import torch
import numpy as np
import argparse
import time
import util
from engine import trainer_regress
from data import prepare_data, prepare_data_handness, prepare_data_mdd, prepare_data_age, prepare_regress
import os
import shutil
import ray
from ray import tune
ray.init(num_cpus=10,num_gpus=2)
import warnings
warnings.filterwarnings('ignore')
def train(args):
# set seed
torch.manual_seed(0)
np.random.seed(0)
# load data
device = torch.device('cuda:0')
train_loader, val_loader, test_loader, adj_mx, seq_len, num_nodes = prepare_regress(n=args.n_samples, dim=args.in_dim, bs=args.batch_size)
engine = trainer_regress(in_dim=args.in_dim, kernel=args.kernel, pool=args.pool, res=args.res, num_nodes=num_nodes, filters=args.n_filters , dropout=args.dropout, lrate=args.lr, wdecay=args.weight_decay, device=args.device, supports=None, blocks=args.blocks)
print("start training...",flush=True)
his_loss =[]
val_time = []
train_time = []
early_stop = 7
es_counter = 0
for i in range(1,args.epochs+1):
if i % 10 == 0:
lr = max(0.00001,args.lr * (0.8 ** (i // 10)))
for g in engine.optimizer.param_groups:
g['lr'] = lr
train_loss = 0
t1 = time.time()
for iter, (x, y) in enumerate(train_loader):
trainx = x.float().to(device)
trainx= trainx.transpose(1, 3)
trainy = y.float().to(device)
metrics = engine.train(trainx, trainy)
train_loss +=metrics
t2 = time.time()
train_time.append(t2-t1)
#validation
valid_loss = 0
s1 = time.time()
for iter, (x, y) in enumerate(val_loader):
testx = x.float().to(device)
testx = testx.transpose(1, 3)
testy = y.float().to(device)
metrics = engine.eval(testx, testy)
valid_loss +=metrics
s2 = time.time()
val_time.append(s2-s1)
mtrain_loss = train_loss/len(train_loader)
mvalid_loss = valid_loss/len(val_loader)
his_loss.append(mvalid_loss)
if mvalid_loss <= min(his_loss):
print('Best val_loss = {:.4f}'.format(mvalid_loss))
es_counter = 0
else:
es_counter += 1
if es_counter>early_stop:
print('No loss improvment.')
break
log = 'Epoch: {:03d}, Train Loss: {:.4f}, Valid Loss: {:.4f}'
print(log.format(i, mtrain_loss, mvalid_loss),flush=True)
torch.save(engine.model.state_dict(), args.save+"_epoch_"+str(i)+"_"+str(round(mvalid_loss,2))+".pth")
#testing
bestid = np.argmin(his_loss)
engine.model.load_state_dict(torch.load(args.save+"_epoch_"+str(bestid+1)+"_"+str(round(his_loss[bestid],2))+".pth"))
print("Training finished")
print("The valid loss on best model is", str(round(his_loss[bestid],4)))
print('-'*30)
train_results = util.inference_regress(engine,train_loader,args.device)
test_results = util.inference_regress(engine,test_loader,args.device)
print('Training results: ', train_results)
print('Test results: ', test_results)
return train_results, test_results, his_loss[bestid]
def run_search(config, checkpoint_dir=None):
args = util.Arguments()
shutil.rmtree(args.save)
os.mkdir(args.save)
args.blocks = config['n_layers']
args.kernel = config['kernel']
args.lr = config['lr']
args.n_filters = config['filters']
args.dropout = config['dropout']
tr_results, test_results, min_val = train(args)
tune.report(
R2=test_results['R2'],
Mse=test_results['MSE'],
Mae=test_results['MAE'],
loss=min_val)
if __name__ == "__main__":
params = {
"lr": tune.loguniform(1e-3, 1e-2),
'n_layers': tune.choice([1,2, 2,3,4,5]),
'filters': tune.choice([4, 6, 8, 16]),
'kernel': tune.choice([3, 5, 6, 7, 9]),
'dropout': tune.choice([0, 0.2, 0.5]),
}
time.sleep(30)
analysis = tune.run(run_search, name='GWnet_ageCont',
config=params,
num_samples=200,
resources_per_trial={"cpu": 10, "gpu": 1})
print(analysis.get_best_config(metric='R2'))
print(analysis.get_best_trial(metric='R2'))