-
Notifications
You must be signed in to change notification settings - Fork 1
/
finetune.py
306 lines (281 loc) · 11.4 KB
/
finetune.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
#! /usr/bin/env python
# -*- coding: utf-8
import configparser
import os
import time
import click
import numpy as np
import torch
import torch.nn as nn
from rdkit import RDLogger
from rdkit.rdBase import DisableLog
from torch.utils.tensorboard import SummaryWriter
from torch_geometric.loader import DataLoader
from dataset import AttFPDataset, AttFPTableDataset, load_from_fname, tokenizer
from model import FFNN, LSTM, AttentiveFP, AttentiveFP2, create_annealing_schedule
from sampling import temperature_sampling
from train import train_one_epoch, validate_one_epoch
from utils import get_input_dims, mse_with_nans, read_config_ini
for level in RDLogger._levels:
DisableLog(level)
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
@click.command()
@click.argument("filename")
@click.option("-c", "--checkpoint", default="models/pub_vae_sig", help="Checkpoint folder.")
@click.option("-e", "--epoch_load", default=70, help="Epoch of models to load.")
@click.option("-n", "--run_name", default=None, help="Name of the run for saving (filename if omitted).")
@click.option("-d", "--delimiter", default="\t", help="Column delimiter of input file.")
@click.option("--smls_col", "--sc", default="SMILES", help="Name of column that contains SMILES.")
@click.option("-p", "--props", default=None, help="Comma-seperated list of descriptors to use. All, if omitted")
@click.option("--epochs", "--ne", default=60, help="Nr. of epochs to train.")
@click.option("-o", "--dropout", default=0.1, help="Dropout fraction.")
@click.option("-b", "--batch_size", default=256, help="Number of molecules per batch.")
@click.option("-r", "--random", is_flag=True, help="Randomly sample molecules in each training step.")
@click.option("--epoch_steps", "--es", default=1000, help="If random, number of batches per epoch.")
@click.option("-v", "--val", default=0.05, help="Fraction of the data to use for validation.")
@click.option("-l", "--lr", default=1e-4, help="Learning rate.")
@click.option("--lr_fact", "-lf", default=0.9, help="Learning rate decay factor.")
@click.option("--lr_step", "-ls", default=10, help="LR Step decay after nr. of epochs.")
@click.option("-a", "--after", default=5, help="Epoch steps to save model.")
@click.option("-t", "--temp", default=0.5, help="Temperature to use during SMILES sampling.")
@click.option("--n_sample", "--ns", default=100, help="Nr. SMILES to sample after each trainin epoch.")
@click.option("--weight_prop", "--wp", default=10.0, help="Factor for weighting property loss in VAE loss")
@click.option("--weight_vae", "--wk", default=0.2, help="Factor for weighting KL divergence loss in VAE loss")
@click.option(
"--anneal_type",
"--at",
default="sigmoid",
help="Shape of VAE weight annealing: constant, linear, sigmoid, cyc_linear, cyc_sigmoid, cyc_sigmoid_lin",
)
@click.option("--anneal_start", "--as", default=0, help="Epoch at which to start VAE loss annealing.")
@click.option("--anneal_stop", "--ao", default=None, help="Epoch at which to stop VAE loss annealing (stay const.)")
@click.option("--anneal_cycle", "--ac", default=5, help="Number of epochs for one VAE loss annealing cycle")
@click.option("--anneal_grow", "--ag", default=5, help="Number of annealing cycles with increasing values")
@click.option("--anneal_ratio", "--ar", default=0.75, help="Fraction of annealing vs. constant VAE weight")
@click.option("--scale/--no-scale", default=True, help="Whether to scale all properties from 0 to 1")
@click.option("--n_proc", "--np", default=6, help="Number of CPU processes to use")
def main(
filename,
checkpoint,
epoch_load,
run_name,
delimiter,
smls_col,
props,
epochs,
dropout,
batch_size,
random,
epoch_steps,
val,
lr,
lr_fact,
lr_step,
after,
temp,
n_sample,
weight_prop,
weight_vae,
anneal_cycle,
anneal_grow,
anneal_ratio,
anneal_start,
anneal_stop,
anneal_type,
scale,
n_proc,
):
if run_name is None: # take filename as run name if not specified
run_name = os.path.basename(filename).split(".")[0]
_, t2i = tokenizer()
dim_atom, dim_bond = get_input_dims()
# load model architecture config from trained model
conf = read_config_ini(checkpoint)
# Write parameters to config file and define variables
vae, wae = conf["vae"], conf["wae"]
weight_vae = weight_vae if (vae or wae) else 0.0
anneal_cycle = anneal_cycle if (vae or wae) else 0
anneal_grow = anneal_grow if (vae or wae) else 0.0
anneal_ratio = anneal_ratio if (vae or wae) else 0
vae = vae if not wae else False
ini = configparser.ConfigParser()
config = {
"filename": filename,
"run_name": run_name,
"finetune": True,
"pretrained": checkpoint,
"epochs": epochs,
"dropout": dropout,
"batch_size": batch_size,
"random": random,
"n_proc": n_proc,
"epoch_steps": epoch_steps if random else "full",
"frac_val": val,
"lr": lr,
"lr_fact": lr_fact,
"lr_step": lr_step,
"save_after": after,
"n_gnn_layers": conf["n_gnn_layers"],
"n_rnn_layers": conf["n_rnn_layers"],
"n_mlp_layers": conf["n_mlp_layers"],
"dim_gnn": conf["dim_gnn"],
"n_kernels": conf["n_kernels"],
"dim_embed": conf["dim_embed"],
"dim_rnn": conf["dim_rnn"],
"dim_mlp": conf["dim_mlp"],
"weight_props": weight_prop,
"weight_vae": weight_vae,
"anneal_cycle": anneal_cycle,
"scaled_props": scale,
"vae": vae,
"wae": wae,
}
content = load_from_fname(filename, smls_col=smls_col, delimiter=delimiter)
DatasetClass = AttFPDataset if content.columns.tolist() == [smls_col] else AttFPTableDataset
dataset = DatasetClass(
filename=filename,
delimiter=delimiter,
smls_col=smls_col,
props=props,
random=random,
scaled_props=scale,
steps=int(epoch_steps * batch_size * (1 + val)),
)
config["n_props"] = n_props = dataset.n_props
config["alphabet"] = conf["alphabet"]
# Define paths
path_model = f"models/{run_name}/"
path_loss = f"logs/{run_name}/"
os.makedirs(path_model, exist_ok=True)
os.makedirs(path_loss, exist_ok=True)
print("\nPaths (model, loss): ", path_model, path_loss)
# store config file for later sampling, retraining etc.
with open(f"{path_model}config.ini", "w") as configfile:
ini["CONFIG"] = config
ini.write(configfile)
# Create models and load weights
GNN_Class = AttentiveFP2 if vae else AttentiveFP
gnn = GNN_Class(
in_channels=dim_atom,
hidden_channels=conf["dim_gnn"],
out_channels=conf["dim_rnn"],
edge_dim=dim_bond,
num_layers=conf["n_gnn_layers"],
num_timesteps=conf["n_kernels"],
dropout=conf["dropout"],
)
rnn = LSTM(
input_dim=conf["alphabet"],
embedding_dim=conf["dim_embed"],
hidden_dim=conf["dim_gnn"],
layers=conf["n_rnn_layers"],
dropout=conf["dropout"],
)
mlp = FFNN(
input_dim=conf["dim_rnn"], hidden_dim=conf["dim_mlp"], n_layers=conf["n_mlp_layers"], output_dim=n_props
)
gnn.load_state_dict(torch.load(os.path.join(checkpoint, f"atfp_{epoch_load}.pt"), map_location=DEVICE))
rnn.load_state_dict(torch.load(os.path.join(checkpoint, f"lstm_{epoch_load}.pt"), map_location=DEVICE))
if conf["n_props"] == n_props:
mlp.load_state_dict(torch.load(os.path.join(checkpoint, f"ffnn_{epoch_load}.pt"), map_location=DEVICE))
else:
print("WARNING: Different number of properties compared to pretrained model. Training MLP from scratch.")
gnn = gnn.to(DEVICE)
rnn = rnn.to(DEVICE)
mlp = mlp.to(DEVICE)
# Define optimizer and loss criteria
opt_params = list(rnn.parameters()) + list(gnn.parameters()) + list(mlp.parameters())
optimizer = torch.optim.Adam(opt_params, lr=lr)
schedule = torch.optim.lr_scheduler.StepLR(optimizer, step_size=lr_step, gamma=lr_fact)
criterion1 = nn.CrossEntropyLoss(reduction="mean")
criterion2 = mse_with_nans
train_set, val_set = torch.utils.data.random_split(dataset, [1.0 - val, val])
train_loader = DataLoader(train_set, batch_size=batch_size, shuffle=True, num_workers=n_proc, drop_last=False)
valid_loader = DataLoader(val_set, batch_size=batch_size, shuffle=True, num_workers=n_proc, drop_last=False)
writer = SummaryWriter(path_loss)
print(
f"Using {len(train_set)}{' random' if random else ''} molecules for training "
+ f"and {len(val_set)}{' random' if random else ''} for validation per epoch."
)
# VAE loss weight annealing
anneal_stop = epochs if anneal_stop is None else anneal_stop
anneal = create_annealing_schedule(
epochs,
epoch_steps if random else len(train_loader),
anneal_start,
anneal_stop,
anneal_cycle,
anneal_grow,
anneal_ratio,
anneal_type,
)
for epoch in range(1, epochs + 1):
print(f"\n---------- Epoch {epoch} ----------")
time_start = time.time()
l_s, l_p, l_k, fk = train_one_epoch(
gnn,
rnn,
mlp,
optimizer,
criterion1,
criterion2,
train_loader,
writer,
epoch,
t2i,
weight_prop,
weight_vae,
anneal,
vae,
wae,
)
l_vs, l_vp, l_vk = validate_one_epoch(
gnn,
rnn,
mlp,
criterion1,
criterion2,
valid_loader,
writer,
epoch * len(train_loader),
t2i,
weight_prop,
weight_vae * fk,
vae,
wae,
)
dur = time.time() - time_start
schedule.step()
last_lr = schedule.get_last_lr()[0]
writer.add_scalar("lr", last_lr, (epoch + 1) * len(train_loader))
print(
f"Epoch: {epoch}, Train Loss SMILES: {l_s:.3f}, Train Loss Props.: {l_p:.3f}, Train Loss VAE.: {l_k:.3f}, "
+ f"Val. Loss SMILES: {l_vs:.3f}, Val. Loss Props.: {l_vp:.3f}, Val. Loss VAE.: {l_vk:.3f}, "
+ f"Weight VAE: {fk * weight_vae:.6f}, LR: {last_lr:.6f}, "
+ f"Time: {dur // 60:.0f}min {dur % 60:.0f}sec"
)
valids, _, _, _, _, _ = temperature_sampling(
gnn,
rnn,
temp,
np.random.choice(val_set.dataset.data[smls_col], n_sample),
n_sample,
dataset.max_len,
verbose=True,
vae=vae,
)
valid = len(valids) / n_sample
writer.add_scalar("valid", valid, epoch * len(train_loader))
print(
f"Epoch: {epoch}, Train Loss SMILES: {l_s:.3f}, Train Loss Props.: {l_p:.3f}, Train Loss VAE.: {l_k:.3f}, "
+ f"Val. Loss SMILES: {l_vs:.3f}, Val. Loss Props.: {l_vp:.3f}, Val. Loss VAE.: {l_vk:.3f}, "
+ f"Weight VAE: {fk * weight_vae:.6f}, Frac. valid: {valid:.3f}, LR: {last_lr:.6f}, "
+ f"Time: {dur // 60:.0f}min {dur % 60:.0f}sec"
)
# save loss and models
if epoch % after == 0:
torch.save(gnn.state_dict(), f"{path_model}atfp_{epoch}.pt")
torch.save(rnn.state_dict(), f"{path_model}lstm_{epoch}.pt")
torch.save(mlp.state_dict(), f"{path_model}ffnn_{epoch}.pt")
if __name__ == "__main__":
main()