-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
261 lines (254 loc) · 10.2 KB
/
train.py
File metadata and controls
261 lines (254 loc) · 10.2 KB
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
import os
import torch
from torch.utils.data import DataLoader
from torch.nn.utils import clip_grad_norm_
from StFT_3D import StFT
from data_utils import LpLoss, get_grid, TemporalDataset
import pickle
import tempfile
from ray.train import Checkpoint
from ray import train, tune
from ray.train import RunConfig
def train_model(config):
def unnorm_data(data, mean, std, B, C, H, W):
data_copy = data.detach().clone()
return (data_copy.reshape(B, C, H, W)[:, None, :, :, :]) * std + mean
many_params = config["many_params"]
dataset = config["dataset"]
dim = config["dim"]
patch_sizes = many_params[0]
overlaps = many_params[1]
vit_depth = many_params[2]
modes = many_params[3]
mlp_dim = dim
num_heads = config["num_heads"]
snapshots = config["snapshots"]
lr = config["lr"]
max_epochs = config["max_epochs"]
batchsize = config["batchsize"]
cond_time = config["cond_time"]
lift_channel = config["lift_channel"]
act = config["act"]
myloss = LpLoss(size_average=False)
num_levels = len(patch_sizes)
with open(dataset, "rb") as file:
dataset = pickle.load(file)
num_in_states = dataset["channels"]
img_size = dataset["img_size"]
train_data = torch.tensor(dataset["train"], dtype=torch.float32, device="cuda")
test = torch.tensor(dataset["test"], dtype=torch.float32, device="cuda")
val = torch.tensor(dataset["val"], dtype=torch.float32, device="cuda")
train_mean = train_data.mean(dim=(0, 1, 3, 4), keepdim=True)
train_std = train_data.std(dim=(0, 1, 3, 4), keepdim=True)
train_data = (train_data - train_mean) / train_std
test = (test - train_mean) / train_std
val = (val - train_mean) / train_std
train_loader = DataLoader(
TemporalDataset(train_data, snapshot_length=snapshots),
batch_size=batchsize,
shuffle=True,
)
in_channels = (2 + num_in_states) * cond_time
grid = get_grid(img_size[0], img_size[1]).cuda()
out_channesl = num_in_states
model = StFT(
cond_time,
num_in_states + 2,
patch_sizes,
overlaps,
in_channels,
out_channesl,
modes,
img_size=img_size,
lift_channel=lift_channel,
dim=dim,
vit_depth=vit_depth,
num_heads=num_heads,
mlp_dim=mlp_dim,
act=act,
).to("cuda")
optimizer = torch.optim.AdamW(model.parameters(), lr=lr)
best_val = torch.tensor(1e10, dtype=torch.float32, device="cuda")
best_test = torch.tensor(1e10, dtype=torch.float32, device="cuda")
best_test_under_val = torch.tensor(1e10, dtype=torch.float32, device="cuda")
for ep in range(max_epochs):
model.train()
train_l2_levels = torch.zeros(num_levels, dtype=torch.float32, device="cuda")
train_l2 = 0
train_num_examples = 0
for _, example in enumerate(train_loader):
B, L, C, H, W = example.shape
for i in range(L - cond_time):
train_num_examples += B * C
x = example[:, i : (i + cond_time)].cuda()
y = example[:, i + cond_time].cuda()
preds = model(x, grid)
sum_residues = torch.zeros_like(
preds[0].reshape(B * num_in_states, -1),
device="cuda",
dtype=torch.float32,
)
for level in range(num_levels):
cur_preds = preds[level]
sum_residues += cur_preds.reshape(B * num_in_states, -1)
train_l2_levels[level] += myloss(
cur_preds.reshape(B * num_in_states, -1).reshape(
B * num_in_states, -1
),
y.reshape(B * num_in_states, -1),
)
loss = myloss(
sum_residues.reshape(B * num_in_states, -1),
y.reshape(B * num_in_states, -1),
)
optimizer.zero_grad()
loss.backward(retain_graph=False)
clip_grad_norm_(model.parameters(), max_norm=10.0)
optimizer.step()
train_l2 += myloss(
sum_residues.reshape(B * num_in_states, -1),
y.reshape(B * num_in_states, -1),
)
train_l2_levels = train_l2_levels / train_num_examples
train_l2 = train_l2 / train_num_examples
model.eval()
if ep % 10 == 0:
num_examples = 0
l2_val = 0.0
with torch.no_grad():
B, L, C, H, W = val.shape
x_old = None
preds_or = val[:, :cond_time]
for i in range(L - cond_time):
num_examples += B * num_in_states
if i == 0:
x = preds_or
else:
x = torch.cat(
(x_old[:, 1:, :, :, :], preds_or[:, None, :, :, :]), axis=1
)
x_old = x.detach().clone()
y = val[:, i + cond_time].cuda()
preds = model(x, grid)
sum_residues = torch.zeros_like(
preds[0].reshape(B * num_in_states, -1),
device="cuda",
dtype=torch.float32,
)
for level in range(num_levels):
cur_preds = preds[level]
sum_residues += (
cur_preds.reshape(B * num_in_states, -1).detach().clone()
)
l2_val += myloss(
unnorm_data(
sum_residues, train_mean, train_std, B, C, H, W
).reshape(B * num_in_states, -1),
unnorm_data(y, train_mean, train_std, B, C, H, W).reshape(
B * num_in_states, -1
),
)
preds_or = sum_residues.reshape(B, C, H, W)
error_val = l2_val / num_examples
num_examples = 0
l2_test = 0.0
with torch.no_grad():
B, L, C, H, W = test.shape
x_old = None
preds_or = test[:, :cond_time]
for i in range(L - cond_time):
num_examples += B * num_in_states
if i == 0:
x = preds_or
else:
x = torch.cat(
(x_old[:, 1:, :, :, :], preds_or[:, None, :, :, :]), axis=1
)
x_old = x.detach().clone()
y = test[:, i + cond_time].cuda()
preds = model(x, grid)
sum_residues = torch.zeros_like(
preds[0].reshape(B * num_in_states, -1),
device="cuda",
dtype=torch.float32,
)
for level in range(num_levels):
cur_preds = preds[level]
sum_residues += (
cur_preds.reshape(B * num_in_states, -1).detach().clone()
)
l2_test += myloss(
unnorm_data(
sum_residues, train_mean, train_std, B, C, H, W
).reshape(B * num_in_states, -1),
unnorm_data(y, train_mean, train_std, B, C, H, W).reshape(
B * num_in_states, -1
),
)
preds_or = sum_residues.reshape(B, C, H, W)
error_test = (l2_test / num_examples).clone()
if error_test < best_test:
best_test = error_test
if error_val < best_val:
best_val = error_val
best_test_under_val = error_test
with tempfile.TemporaryDirectory() as tempdir:
torch.save(
{"model_state": model.state_dict()},
os.path.join(tempdir, "checkpoint_harrm.pt"),
)
metrics = {
"epoch": ep,
"train_l2": train_l2.item(),
"best_val": best_val.item(),
"best_test_under_val": best_test_under_val.item(),
"best_test": best_test.item(),
"test_error": error_test.item(),
"val_error": error_val.item(),
}
for _ in range(num_levels):
metrics["level_" + str(_) + "_loss"] = train_l2_levels[_].item()
train.report(
metrics=metrics, checkpoint=Checkpoint.from_directory(tempdir)
)
else:
metrics = {
"epoch": ep,
"train_l2": train_l2.item(),
"best_val": best_val.item(),
"best_test_under_val": best_test_under_val.item(),
"best_test": best_test.item(),
"test_error": error_test.item(),
"val_error": error_val.item(),
}
for _ in range(num_levels):
metrics["level_" + str(_) + "_loss"] = train_l2_levels[_].item()
train.report(metrics=metrics)
if __name__ == "__main__":
config = {
"dataset": "/path/to/my/data/plasma.pkl",
"many_params": (
((128, 128), (64, 64)),
((1, 1), (1, 1)),
(6, 6),
((8, 8), (8, 8)),
),
"dim": 128,
"num_heads": 1,
"snapshots": 20,
"lr": 1e-4,
"max_epochs": 100000,
"batchsize": 20,
"cond_time": 5,
"lift_channel": 64,
"act": "gelu",
}
# save_path = "/path/to/my/results"
trainable_with_cpu_gpu = tune.with_resources(train_model, {"cpu": 16, "gpu": 1})
tuner = tune.Tuner(
trainable_with_cpu_gpu,
param_space=config,
run_config=RunConfig(name="train_p"),
storage_path = save_path,
)
tuner.fit()