-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTIGON.py
121 lines (86 loc) · 3.62 KB
/
TIGON.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
import os
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from utility import *
if __name__ == '__main__':
args=create_args()
torch.enable_grad()
random.seed(args.seed)
torch.manual_seed(args.seed)
device = torch.device('cuda:' + str(args.gpu)
if torch.cuda.is_available() else 'cpu')
# load dataset
data_train = loaddata(args,device)
integral_time = args.timepoints
time_pts = range(len(data_train))
leave_1_out = []
train_time = [x for i,x in enumerate(time_pts) if i!=leave_1_out]
# model
func = UOT(in_out_dim=data_train[0].shape[1], hidden_dim=args.hidden_dim,n_hiddens=args.n_hiddens,activation=args.activation).to(device)
func.apply(initialize_weights)
# configure training options
options = {}
options.update({'method': 'Dopri5'})
options.update({'h': None})
options.update({'rtol': 1e-3})
options.update({'atol': 1e-5})
options.update({'print_neval': False})
options.update({'neval_max': 1000000})
options.update({'safety': None})
optimizer = optim.Adam(func.parameters(), lr=args.lr, weight_decay= 0.01)
lr_adjust = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[args.niters-400,args.niters-200], gamma=0.5, last_epoch=-1)
mse = nn.MSELoss()
LOSS = []
L2_1 = []
L2_2 = []
Trans = []
Sigma = []
if args.save_dir is not None:
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
ckpt_path = os.path.join(args.save_dir, 'ckpt.pth')
if os.path.exists(ckpt_path):
checkpoint = torch.load(ckpt_path)
func.load_state_dict(checkpoint['func_state_dict'])
optimizer.load_state_dict(checkpoint['optimizer_state_dict'])
print('Loaded ckpt from {}'.format(ckpt_path))
try:
sigma_now = 1
for itr in range(1, args.niters + 1):
optimizer.zero_grad()
loss, loss1, sigma_now, L2_value1, L2_value2 = train_model(mse,func,args,data_train,train_time,integral_time,sigma_now,options,device,itr)
loss.backward()
optimizer.step()
lr_adjust.step()
LOSS.append(loss.item())
Trans.append(loss1[-1].mean(0).item())
Sigma.append(sigma_now)
L2_1.append(L2_value1.tolist())
L2_2.append(L2_value2.tolist())
print('Iter: {}, loss: {:.4f}'.format(itr, loss.item()))
if itr % 500 == 0:
ckpt_path = os.path.join(args.save_dir, 'ckpt_itr{}.pth'.format(itr))
torch.save({'func_state_dict': func.state_dict()}, ckpt_path)
print('Iter {}, Stored ckpt at {}'.format(itr, ckpt_path))
except KeyboardInterrupt:
if args.save_dir is not None:
ckpt_path = os.path.join(args.save_dir, 'ckpt.pth')
torch.save({
'func_state_dict': func.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
}, ckpt_path)
print('Stored ckpt at {}'.format(ckpt_path))
print('Training complete after {} iters.'.format(itr))
ckpt_path = os.path.join(args.save_dir, 'ckpt.pth')
torch.save({
'func_state_dict': func.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),
'LOSS':LOSS,
'TRANS':Trans,
'L2_1': L2_1,
'L2_2': L2_2,
'Sigma': Sigma
}, ckpt_path)
print('Stored ckpt at {}'.format(ckpt_path))