-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain_locator.py
312 lines (245 loc) · 12.3 KB
/
train_locator.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import os
import sys
import gc
import argparse
import numpy as np
from random import SystemRandom
import h5py
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
import model.utils as utils
from model.locator import Locator
import torchcde
from tensorboardX import SummaryWriter
import matplotlib.pyplot as plt
k = 1/3
parser = argparse.ArgumentParser('Locator')
parser.add_argument('--niters', type=int, default=8)
parser.add_argument('--lr', type=float, default=4e-3, help="Starting learning rate")
parser.add_argument('-b', '--batch-size', type=int, default=128)
parser.add_argument('--dataset', type=str, default='/work/hmzhao/irregular-lc/KMT-locflux-0.003-0.h5', help="Path for dataset")
# parser.add_argument('--dataset', type=str, default='/work/hmzhao/irregular-lc/roman-0-8dof.h5', help="Path for dataset")
parser.add_argument('--save', type=str, default='/work/hmzhao/experiments/locator/', help="Path for save checkpoints")
parser.add_argument('--load', type=str, default=None, help="ID of the experiment to load for evaluation. If None, run a new experiment.")
parser.add_argument('--name', type=str, default=None, help="Name of the experiment")
parser.add_argument('--resume', type=int, default=0, help="Epoch to resume.")
parser.add_argument('-r', '--random-seed', type=int, default=42, help="Random_seed")
parser.add_argument('--device', type=str, default='cuda:0', help="device")
args = parser.parse_args()
device = torch.device(args.device if torch.cuda.is_available() else "cpu")
file_name = os.path.basename(__file__)[:-3]
utils.makedirs(args.save)
#####################################################################################################
if __name__ == '__main__':
torch.manual_seed(args.random_seed)
np.random.seed(args.random_seed)
os.environ['JOBLIB_TEMP_FOLDER'] = '/work/hmzhao/tmp'
print(f'Num of GPUs available: {torch.cuda.device_count()}')
experimentID = args.name
if experimentID is None:
# Make a new experiment ID
experimentID = int(SystemRandom().random() * 100000)
print(f'ExperimentID: {experimentID}')
ckpt_path = os.path.join(args.save, "experiment_" + str(experimentID) + '.ckpt')
ckpt_path_load = os.path.join(args.save, "experiment_" + str(args.load) + '.ckpt')
# ckpt_path_load = ckpt_path
input_command = sys.argv
ind = [i for i in range(len(input_command)) if input_command[i] == "--load"]
if len(ind) == 1:
ind = ind[0]
input_command = input_command[:ind] + input_command[(ind+2):]
input_command = " ".join(input_command)
writer = SummaryWriter(log_dir=f'/work/hmzhao/tbxdata/locator-{experimentID}')
##################################################################
print(f'Loading Data: {args.dataset}')
with h5py.File(args.dataset, mode='r') as dataset_file:
Y = torch.tensor(dataset_file['Y'][...])
X = torch.tensor(dataset_file['X'][...])
# filter nan
nanind = torch.where(~torch.isnan(X[:, 0, 1]))[0]
Y = Y[nanind]
X = X[nanind]
# ind_smallte = torch.where(Y[:, 1] < 40)[0]
# Y = Y[ind_smallte]
# X = X[ind_smallte]
test_size = 1024
train_size = len(Y) - test_size
# train_size = 128
print(f'Training Set Size: {train_size}')
# # normalize
# Y: t_0, t_E, u_0, rho, q, s, alpha, f_s
# use center of mag
# ind_larges = Y[:, 5] > 1
# delta = Y[ind_larges, 4] / (1 + Y[ind_larges, 4]) * (Y[ind_larges, 5] - 1 / Y[ind_larges, 5])
# Y[ind_larges, 0] -= Y[ind_larges, 1] * np.cos(np.pi/180*Y[ind_larges, -2]) * delta
Y = Y[:, [0, 1, -1]]
mean_y = torch.mean(Y, axis=0)
std_y = torch.std(Y, axis=0)
print(f'Y mean: {mean_y}\nY std: {std_y}')
# Y = (Y - mean_y) / std_y
# print(f'normalized Y mean: {torch.mean(Y)}\nY std: {torch.mean(torch.std(Y, axis=0)[~std_mask])}')
X = X[:, :, :2]
# X[:, :, 1] = (X[:, :, 1] - 14.5 - 2.5 * torch.log10(Y[:, [-1]]))
print(f'normalized X mean: {torch.mean(X[:, :, 1])}\nX std: {torch.mean(torch.std(X[:, :, 1], axis=0))}')
Y = Y[:, [0, 1]]
# interpolation
train_coeffs = torchcde.hermite_cubic_coefficients_with_backward_differences(X[:train_size])
train_dataset = torch.utils.data.TensorDataset(train_coeffs, Y[:train_size])
train_dataloader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=False)
test_coeffs = torchcde.hermite_cubic_coefficients_with_backward_differences(X[(-test_size):, :, :]).float().to(device)
test_Y = Y[(-test_size):].float().to(device)
output_dim = Y.shape[-1]
input_dim = X.shape[-1]
del Y
del X
gc.collect()
##################################################################
# Create the model
model = Locator(device, k=k, threshold=0.5).to(device)
##################################################################
# Load checkpoint and evaluate the model
if args.load is not None:
# Load checkpoint.
checkpt = torch.load(ckpt_path_load, map_location='cpu')
ckpt_args = checkpt['args']
state_dict = checkpt['state_dict']
model_dict = model.state_dict()
# 1. filter out unnecessary keys
state_dict = {k: v for k, v in state_dict.items() if k in model_dict}
# 2. overwrite entries in the existing state dict
model_dict.update(state_dict)
# 3. load the new state dict
model.load_state_dict(model_dict)
model.to(device)
##################################################################
# Training
print('Start Training Locator')
log_path = "logs/" + file_name + "_" + str(experimentID) + ".log"
utils.makedirs("logs/")
logger = utils.get_logger(logpath=log_path, filepath=os.path.abspath(__file__))
logger.info(input_command)
logger.info("Experiment Locator " + str(experimentID))
optimizer = optim.Adam(
[
{"params": model.parameters(), "lr": args.lr*1e0},
# {"params": model.cde_func.parameters(), "lr": args.lr*1e0},
# {"params": model.cde_func_r.parameters(), "lr": args.lr*1e0},
# {"params": model.readout.parameters(), "lr": args.lr*1e0}
],
lr=args.lr, weight_decay=0,
)
# optimizer = optim.Adadelta(model.parameters(), lr=args.lr)
# optimizer = optim.SGD(model.parameters(), lr=args.lr)
num_batches = len(train_dataloader)
loss_func = nn.MSELoss()
for epoch in range(args.resume, args.resume + args.niters):
e_dataloader = train_dataloader
num_batches = len(e_dataloader)
utils.update_learning_rate(optimizer, decay_rate = 0.9, lowest = args.lr / 10)
lr = optimizer.state_dict()['param_groups'][0]['lr']
print(f'Epoch {epoch}, Learning Rate {lr}')
writer.add_scalar('learning_rate', lr, epoch)
for i, (batch_coeffs, batch_y) in enumerate(e_dataloader):
model.train()
batch_y = batch_y.float().to(device)
batch_coeffs = batch_coeffs.float().to(device)
# rescaley = (batch_y / 72 * 4000).int()
# left = rescaley[:, [0]] - 2*rescaley[:, [1]]
# right = rescaley[:, [0]] + 2*rescaley[:, [1]]
# z = torch.tile(torch.arange(0, 4000).unsqueeze(0), (len(batch_y), 1)).to(device)
# z = ((z > left) * (z < right)).int()
optimizer.zero_grad()
pred_y, mse_z = model(batch_coeffs, batch_y)
mse = torch.mean((batch_y - pred_y)**2, dim=0).detach().cpu() # * (std_Y**2)
# loss = loss_func(pred_y, batch_y)
loss = mse_z #+ torch.log(loss_func(pred_y, batch_y) + 1e-10) / 10
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=20)
total_norm = 0
parameters = [p for p in model.parameters() if p.grad is not None and p.requires_grad]
for p in parameters:
param_norm = p.grad.detach().data.norm(2)
total_norm += param_norm.item() ** 2
total_norm = total_norm ** 0.5
writer.add_scalar('gradient_norm', total_norm, (i + epoch * num_batches))
optimizer.step()
print(f'batch {i}/{num_batches}, loss {loss.item()}, mse_t0, tE {mse}')
writer.add_scalar('loss/batch_loss', loss.item(), (i + epoch * num_batches))
writer.add_scalar('mse_batch/batch_mse_t0', mse[0], (i + epoch * num_batches))
writer.add_scalar('mse_batch/batch_mse_tE', mse[1], (i + epoch * num_batches))
if (i + epoch * num_batches) % 20 == 0:
model.eval()
torch.save({
'args': args,
'state_dict': model.state_dict(),
}, ckpt_path)
print(f'Model saved to {ckpt_path}')
with torch.no_grad():
pred_y, mse_z = model(test_coeffs, test_Y)
# loss = loss_func(pred_y, test_Y)
loss = mse_z #+ torch.log(loss_func(pred_y, test_Y) + 1e-10) / 10
mse = torch.mean((test_Y - pred_y)**2, dim=0).detach().cpu() # * (std_Y**2)
message = f'Epoch {(i + epoch * num_batches)/num_batches}, Test Loss {loss.item()}, mse_t0, tE {mse}'
writer.add_scalar('loss/test_loss', loss.item(), (i + epoch * num_batches)/20)
writer.add_scalar('mse/test_mse_t0', mse[0], (i + epoch * num_batches)/20)
writer.add_scalar('mse/test_mse_tE', mse[1], (i + epoch * num_batches)/20)
for name, param in model.named_parameters():
writer.add_histogram(name, param.clone().cpu().data.numpy(), (i + epoch * num_batches)/20)
# logger.info("Experiment " + str(experimentID))
logger.info(message)
model.train()
# change dataset
if os.path.exists(args.dataset[:-4] + str((int(args.dataset[-4])+1)) + '.h5'):
args.dataset = args.dataset[:-4] + str((int(args.dataset[-4])+1)) + '.h5'
else:
args.dataset = args.dataset[:-4] + '0.h5'
print(f'Loading Data: {args.dataset}')
with h5py.File(args.dataset, mode='r') as dataset_file:
Y = torch.tensor(dataset_file['Y'][...])
X = torch.tensor(dataset_file['X'][...])
# filter nan
nanind = torch.where(~torch.isnan(X[:, 0, 1]))[0]
Y = Y[nanind]
X = X[nanind]
# ind_smallte = torch.where(Y[:, 1] < 40)[0]
# Y = Y[ind_smallte]
# X = X[ind_smallte]
test_size = 1024
train_size = len(Y) - test_size
# train_size = 128
print(f'Training Set Size: {train_size}')
# # normalize
# Y: t_0, t_E, u_0, rho, q, s, alpha, f_s
# use center of mag
# ind_larges = Y[:, 5] > 1
# delta = Y[ind_larges, 4] / (1 + Y[ind_larges, 4]) * (Y[ind_larges, 5] - 1 / Y[ind_larges, 5])
# Y[ind_larges, 0] -= Y[ind_larges, 1] * np.cos(np.pi/180*Y[ind_larges, -2]) * delta
Y = Y[:, [0, 1, -1]]
mean_y = torch.mean(Y, axis=0)
std_y = torch.std(Y, axis=0)
print(f'Y mean: {mean_y}\nY std: {std_y}')
# Y = (Y - mean_y) / std_y
# print(f'normalized Y mean: {torch.mean(Y)}\nY std: {torch.mean(torch.std(Y, axis=0)[~std_mask])}')
X = X[:, :, :2]
# X[:, :, 1] = (X[:, :, 1] - 14.5 - 2.5 * torch.log10(Y[:, [-1]]))
print(f'normalized X mean: {torch.mean(X[:, :, 1])}\nX std: {torch.mean(torch.std(X[:, :, 1], axis=0))}')
Y = Y[:, [0, 1]]
# interpolation
train_coeffs = torchcde.hermite_cubic_coefficients_with_backward_differences(X[:train_size, :, :])
train_dataset = torch.utils.data.TensorDataset(train_coeffs, Y[:train_size])
train_dataloader = DataLoader(train_dataset, batch_size=args.batch_size, shuffle=False)
test_coeffs = torchcde.hermite_cubic_coefficients_with_backward_differences(X[(-test_size):, :, :]).float().to(device)
test_Y = Y[(-test_size):].float().to(device)
output_dim = Y.shape[-1]
input_dim = X.shape[-1]
del Y
del X
gc.collect()
torch.save({
'args': args,
'state_dict': model.state_dict(),
}, ckpt_path)
print(f'Model saved to {ckpt_path}')
writer.close()