forked from peterbjorgensen/DeepDFT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runner.py
348 lines (297 loc) · 10.9 KB
/
runner.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import os
import sys
import json
import argparse
import math
import logging
import itertools
import timeit
import numpy as np
import torch
import torch.utils.data
torch.set_num_threads(1) # Try to avoid thread overload on cluster
import densitymodel
import dataset
def get_arguments(arg_list=None):
parser = argparse.ArgumentParser(
description="Train graph convolution network", fromfile_prefix_chars="+"
)
parser.add_argument(
"--load_model",
type=str,
default=None,
help="Load model parameters from previous run",
)
parser.add_argument(
"--cutoff",
type=float,
default=5.0,
help="Atomic interaction cutoff distance [Å]",
)
parser.add_argument(
"--split_file",
type=str,
default=None,
help="Train/test/validation split file json",
)
parser.add_argument(
"--num_interactions",
type=int,
default=3,
help="Number of interaction layers used",
)
parser.add_argument(
"--node_size", type=int, default=64, help="Size of hidden node states"
)
parser.add_argument(
"--output_dir",
type=str,
default="runs/model_output",
help="Path to output directory",
)
parser.add_argument(
"--dataset", type=str, default="data/qm9.db", help="Path to ASE database",
)
parser.add_argument(
"--max_steps",
type=int,
default=int(1e6),
help="Maximum number of optimisation steps",
)
parser.add_argument(
"--device",
type=str,
default="cuda",
help="Set which device to use for training e.g. 'cuda' or 'cpu'",
)
parser.add_argument(
"--use_painn_model",
action="store_true",
help="Enable equivariant message passing model (PaiNN)"
)
parser.add_argument(
"--ignore_pbc",
action="store_true",
help="If flag is given, ignore periodic boundary conditions in atoms data",
)
return parser.parse_args(arg_list)
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
def split_data(dataset, args):
# Load or generate splits
if args.split_file:
with open(args.split_file, "r") as fp:
splits = json.load(fp)
else:
datalen = len(dataset)
num_validation = int(math.ceil(datalen * 0.05))
indices = np.random.permutation(len(dataset))
splits = {
"train": indices[num_validation:].tolist(),
"validation": indices[:num_validation].tolist(),
}
# Save split file
with open(os.path.join(args.output_dir, "datasplits.json"), "w") as f:
json.dump(splits, f)
# Split the dataset
datasplits = {}
for key, indices in splits.items():
datasplits[key] = torch.utils.data.Subset(dataset, indices)
return datasplits
def eval_model(model, dataloader, device):
with torch.no_grad():
running_ae = torch.tensor(0., device=device)
running_se = torch.tensor(0., device=device)
running_count = torch.tensor(0., device=device)
for batch in dataloader:
device_batch = {
k: v.to(device=device, non_blocking=True) for k, v in batch.items()
}
outputs = model(device_batch)
targets = device_batch["probe_target"]
running_ae += torch.sum(torch.abs(targets - outputs))
running_se += torch.sum(torch.square(targets - outputs))
running_count += torch.sum(device_batch["num_probes"])
mae = (running_ae / running_count).item()
rmse = (torch.sqrt(running_se / running_count)).item()
return mae, rmse
def get_normalization(dataset, per_atom=True):
try:
num_targets = len(dataset.transformer.targets)
except AttributeError:
num_targets = 1
x_sum = torch.zeros(num_targets)
x_2 = torch.zeros(num_targets)
num_objects = 0
for sample in dataset:
x = sample["targets"]
if per_atom:
x = x / sample["num_nodes"]
x_sum += x
x_2 += x ** 2.0
num_objects += 1
# Var(X) = E[X^2] - E[X]^2
x_mean = x_sum / num_objects
x_var = x_2 / num_objects - x_mean ** 2.0
return x_mean, torch.sqrt(x_var)
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def main():
args = get_arguments()
# Setup logging
os.makedirs(args.output_dir, exist_ok=True)
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s [%(levelname)-5.5s] %(message)s",
handlers=[
logging.FileHandler(
os.path.join(args.output_dir, "printlog.txt"), mode="w"
),
logging.StreamHandler(),
],
)
# Save command line args
with open(os.path.join(args.output_dir, "commandline_args.txt"), "w") as f:
f.write("\n".join(sys.argv[1:]))
# Save parsed command line arguments
with open(os.path.join(args.output_dir, "arguments.json"), "w") as f:
json.dump(vars(args), f)
# Setup dataset and loader
if args.dataset.endswith(".txt"):
# Text file contains list of datafiles
with open(args.dataset, "r") as datasetfiles:
filelist = [os.path.join(os.path.dirname(args.dataset), line.strip('\n')) for line in datasetfiles]
else:
filelist = [args.dataset]
logging.info("loading data %s", args.dataset)
densitydata = torch.utils.data.ConcatDataset([dataset.DensityData(path) for path in filelist])
# Split data into train and validation sets
datasplits = split_data(densitydata, args)
datasplits["train"] = dataset.RotatingPoolData(datasplits["train"], 20)
# Setup loaders
train_loader = torch.utils.data.DataLoader(
datasplits["train"],
2,
num_workers=4,
sampler=torch.utils.data.RandomSampler(datasplits["train"]),
collate_fn=dataset.CollateFuncRandomSample(args.cutoff, 1000, pin_memory=False, disable_pbc=args.ignore_pbc),
)
val_loader = torch.utils.data.DataLoader(
datasplits["validation"],
2,
collate_fn=dataset.CollateFuncRandomSample(args.cutoff, 5000, pin_memory=False, disable_pbc=args.ignore_pbc),
num_workers=0,
)
logging.info("Preloading validation batch")
val_loader = [b for b in val_loader]
# Initialise model
device = torch.device(args.device)
if args.use_painn_model:
net = densitymodel.PainnDensityModel(args.num_interactions, args.node_size, args.cutoff,)
else:
net = densitymodel.DensityModel(args.num_interactions, args.node_size, args.cutoff,)
logging.debug("model has %d parameters", count_parameters(net))
net = net.to(device)
# Setup optimizer
optimizer = torch.optim.Adam(net.parameters(), lr=0.0001)
criterion = torch.nn.MSELoss()
scheduler_fn = lambda step: 0.96 ** (step / 100000)
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, scheduler_fn)
log_interval = 5000
running_loss = torch.tensor(0.0, device=device)
running_loss_count = torch.tensor(0, device=device)
best_val_mae = np.inf
step = 0
# Restore checkpoint
if args.load_model:
state_dict = torch.load(args.load_model)
net.load_state_dict(state_dict["model"])
step = state_dict["step"]
best_val_mae = state_dict["best_val_mae"]
optimizer.load_state_dict(state_dict["optimizer"])
scheduler.load_state_dict(state_dict["scheduler"])
logging.info("start training")
data_timer = AverageMeter("data_timer")
transfer_timer = AverageMeter("transfer_timer")
train_timer = AverageMeter("train_timer")
eval_timer = AverageMeter("eval_time")
endtime = timeit.default_timer()
for _ in itertools.count():
for batch_host in train_loader:
data_timer.update(timeit.default_timer()-endtime)
tstart = timeit.default_timer()
# Transfer to 'device'
batch = {
k: v.to(device=device, non_blocking=True)
for (k, v) in batch_host.items()
}
transfer_timer.update(timeit.default_timer()-tstart)
tstart = timeit.default_timer()
# Reset gradient
optimizer.zero_grad()
# Forward, backward and optimize
outputs = net(batch)
loss = criterion(outputs, batch["probe_target"])
loss.backward()
optimizer.step()
with torch.no_grad():
running_loss += loss * batch["probe_target"].shape[0] * batch["probe_target"].shape[1]
running_loss_count += torch.sum(batch["num_probes"])
train_timer.update(timeit.default_timer()-tstart)
# print(step, loss_value)
# Validate and save model
if (step % log_interval == 0) or ((step + 1) == args.max_steps):
tstart = timeit.default_timer()
with torch.no_grad():
train_loss = (running_loss / running_loss_count).item()
running_loss = running_loss_count = 0
val_mae, val_rmse = eval_model(net, val_loader, device)
logging.info(
"step=%d, val_mae=%g, val_rmse=%g, sqrt(train_loss)=%g",
step,
val_mae,
val_rmse,
math.sqrt(train_loss),
)
# Save checkpoint
if val_mae < best_val_mae:
best_val_mae = val_mae
torch.save(
{
"model": net.state_dict(),
"optimizer": optimizer.state_dict(),
"scheduler": scheduler.state_dict(),
"step": step,
"best_val_mae": best_val_mae,
},
os.path.join(args.output_dir, "best_model.pth"),
)
eval_timer.update(timeit.default_timer()-tstart)
logging.debug(
"%s %s %s %s" % (data_timer, transfer_timer, train_timer, eval_timer)
)
step += 1
scheduler.step()
if step >= args.max_steps:
logging.info("Max steps reached, exiting")
sys.exit(0)
endtime = timeit.default_timer()
if __name__ == "__main__":
main()