-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrun_train_property_prediction_energy.py
230 lines (206 loc) · 10 KB
/
run_train_property_prediction_energy.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
# Rdkit import should be first, do not move it
try:
from rdkit import Chem
except ModuleNotFoundError:
pass
import os
from tool.utils import available_devices,format_devices
device = available_devices(threshold=10000, n_devices=1)
os.environ["CUDA_VISIBLE_DEVICES"] = format_devices(device)
import copy
import utils
import argparse
from configs.datasets_config import get_dataset_info
from os.path import join
from qm9 import dataset
from energys_prediction.training_energy import get_model, train_epoch
from util.utils import EMA
import torch
import time
import pickle
from qm9.utils import compute_mean_mad
from torch import optim
import logging
from tool.utils import set_logger
parser = argparse.ArgumentParser()
parser.add_argument('--exp_name', type=str, default='predict_mu')
parser.add_argument('--model', type=str, default='egnn_dynamics',
help='our_dynamics | schnet | simple_dynamics | '
'kernel_dynamics | egnn_dynamics |gnn_dynamics')
parser.add_argument('--probabilistic_model', type=str, default='diffusion',
help='diffusion')
parser.add_argument('--diffusion_steps', type=int, default=500)
parser.add_argument('--diffusion_noise_schedule', type=str, default='polynomial_2',
help='learned, cosine')
parser.add_argument('--diffusion_noise_precision', type=float, default=1e-5,
)
parser.add_argument('--n_epochs', type=int, default=3000)
parser.add_argument('--batch_size', type=int, default=128)
parser.add_argument('--lr', type=float, default=2e-4)
parser.add_argument('--weight_decay', type=float, default=1e-16, metavar='N',
help='weight decay')
parser.add_argument('--brute_force', type=eval, default=False,
help='True | False')
parser.add_argument('--actnorm', type=eval, default=True,
help='True | False')
parser.add_argument('--break_train_epoch', type=eval, default=False,
help='True | False')
parser.add_argument('--dp', type=eval, default=True,
help='True | False')
parser.add_argument('--condition_time', type=eval, default=True,
help='True | False')
parser.add_argument('--clip_grad', type=eval, default=True,
help='True | False')
parser.add_argument('--trace', type=str, default='hutch',
help='hutch | exact')
# EGNN args -->
parser.add_argument('--n_layers', type=int, default=6,
help='number of layers')
parser.add_argument('--inv_sublayers', type=int, default=1,
help='number of layers')
parser.add_argument('--nf', type=int, default=128,
help='number of layers')
parser.add_argument('--tanh', type=eval, default=True,
help='use tanh in the coord_mlp')
parser.add_argument('--attention', type=eval, default=True,
help='use attention in the EGNN')
parser.add_argument('--norm_constant', type=float, default=1,
help='diff/(|diff| + norm_constant)')
parser.add_argument('--sin_embedding', type=eval, default=False,
help='whether using or not the sin embedding')
# <-- EGNN args
parser.add_argument('--ode_regularization', type=float, default=1e-3)
parser.add_argument('--dataset', type=str, default='qm9',
help='qm9 | qm9_second_half (train only on the last 50K samples of the training dataset)')
parser.add_argument('--datadir', type=str, default='qm9/temp',
help='qm9 directory')
parser.add_argument('--filter_n_atoms', type=int, default=None,
help='When set to an integer value, QM9 will only contain molecules of that amount of atoms')
parser.add_argument('--dequantization', type=str, default='argmax_variational',
help='uniform | variational | argmax_variational | deterministic')
parser.add_argument('--n_report_steps', type=int, default=1)
parser.add_argument('--no-cuda', action='store_true', default=False,
help='enables CUDA training')
parser.add_argument('--save_model', type=eval, default=True,
help='save model')
parser.add_argument('--generate_epochs', type=int, default=1,
help='save model')
parser.add_argument('--num_workers', type=int, default=0, help='Number of worker for the dataloader')
parser.add_argument('--test_epochs', type=int, default=10)
parser.add_argument('--data_augmentation', type=eval, default=False, help='use attention in the EGNN')
parser.add_argument("--conditioning", nargs='+', default=[],
help='arguments : homo | lumo | alpha | gap | mu | Cv' )
parser.add_argument('--resume', type=str, default= None,
help='')
parser.add_argument('--start_epoch', type=int, default=0,
help='')
parser.add_argument('--ema_decay', type=float, default=0.999,
help='Amount of EMA decay, 0 means off. A reasonable value'
' is 0.999.')
parser.add_argument('--augment_noise', type=float, default=0)
parser.add_argument('--normalize_factors', type=eval, default=[1, 4, 1],
help='normalize factors for [x, categorical, integer]')
parser.add_argument('--remove_h', action='store_true')
parser.add_argument('--include_charges', type=eval, default=True,
help='include atom charge or not')
parser.add_argument('--load_charges', type=eval, default=True,
help='load atom charge or not')
parser.add_argument('--normalization_factor', type=float, default=1,
help="Normalize the sum aggregation of EGNN")
parser.add_argument('--aggregation_method', type=str, default='sum',
help='"sum" or "mean"')
args = parser.parse_args()
#set workpath
workpath = os.path.join('pretrained_models', args.exp_name)
os.makedirs(workpath, exist_ok=True)
set_logger(workpath, 'logs.txt')
args.cuda = not args.no_cuda and torch.cuda.is_available()
device = torch.device("cuda" if args.cuda else "cpu")
dtype = torch.float32
dataset_info = get_dataset_info(args.dataset, args.remove_h)
if args.resume is not None:
exp_name = args.exp_name
resume = args.resume
wandb_usr = args.wandb_usr
normalization_factor = args.normalization_factor
aggregation_method = args.aggregation_method
with open(join(args.resume, 'args.pickle'), 'rb') as f:
args = pickle.load(f)
args.resume = resume
args.break_train_epoch = False
args.exp_name = exp_name
args.start_epoch = args.start_epoch
args.wandb_usr = wandb_usr
# Careful with this -->
if not hasattr(args, 'normalization_factor'):
args.normalization_factor = normalization_factor
if not hasattr(args, 'aggregation_method'):
args.aggregation_method = aggregation_method
logging.info(args)
utils.create_folders(args)
# Retrieve QM9 dataloaders
dataloaders, charge_scale = dataset.retrieve_dataloaders(args)
property_norms = compute_mean_mad(dataloaders, args.conditioning, args.dataset)
args.context_node_nf = 0
# Create EGNN flow
model, nodes_dist, prop_dist = get_model(args, device, dataset_info, dataloaders['train'])
if prop_dist is not None:
prop_dist.set_normalizer(property_norms)
model = model.to(device)
optim = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optim, args.n_epochs)
gradnorm_queue = utils.Queue()
gradnorm_queue.add(3000) # Add large value that will be flushed.
def main():
if args.resume is not None:
flow_state_dict = torch.load(join(args.resume, 'model.npy'))
optim_state_dict = torch.load(join(args.resume, 'optim.npy'))
model.load_state_dict(flow_state_dict)
optim.load_state_dict(optim_state_dict)
# Initialize dataparallel if enabled and possible.
if args.dp and torch.cuda.device_count() > 1:
print(f'Training using {torch.cuda.device_count()} GPUs')
model_dp = torch.nn.DataParallel(model.cpu())
model_dp = model_dp.cuda()
else:
model_dp = model
# Initialize model copy for exponential moving average of params.
if args.ema_decay > 0:
if args.resume is not None:
model_ema = copy.deepcopy(model)
ema_state_dict = torch.load(
join(args.resume, 'model_ema.npy'))
model_ema.load_state_dict(ema_state_dict)
else:
model_ema = copy.deepcopy(model)
ema = EMA(args.ema_decay)
if args.dp and torch.cuda.device_count() > 1:
model_ema_dp = torch.nn.DataParallel(model_ema)
else:
model_ema_dp = model_ema
else:
ema = None
model_ema = model
model_ema_dp = model_dp
for epoch in range(args.start_epoch, args.n_epochs):
start_epoch = time.time()
train_epoch(args=args, loader=dataloaders['train'], epoch=epoch, model=model, model_dp=model_dp,
model_ema=model_ema, ema=ema, device=device, dtype=dtype, property_norms=property_norms,
nodes_dist=nodes_dist, dataset_info=dataset_info,
gradnorm_queue=gradnorm_queue, optim=optim, prop_dist=prop_dist,lr_scheduler=lr_scheduler)
logging.info(f"Epoch took {time.time() - start_epoch:.1f} seconds.")
if epoch % 250 == 0:
utils.save_model(optim, os.path.join(workpath, 'optim_%d.npy' % (epoch)))
utils.save_model(model, os.path.join(workpath, 'model_%d.npy' % (epoch)))
if args.ema_decay > 0:
utils.save_model(model_ema, os.path.join(workpath, 'model_ema_%d.npy' % (epoch)))
with open(os.path.join(workpath, 'args_%d.pickle' % (epoch)), 'wb') as f:
pickle.dump(args, f)
utils.save_model(optim, os.path.join(workpath, 'optim.npy'))
utils.save_model(model, os.path.join(workpath, 'model.npy'))
if args.ema_decay > 0:
utils.save_model(model_ema, os.path.join(workpath, 'model_ema.npy'))
with open(os.path.join(workpath, 'args.pickle'), 'wb') as f:
pickle.dump(args, f)
if __name__ == "__main__":
main()