-
Notifications
You must be signed in to change notification settings - Fork 0
/
pretrain.py
213 lines (172 loc) · 8.79 KB
/
pretrain.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
import argparse
import copy
import importlib
import os
import time
import gc
from torch.nn import CrossEntropyLoss
from torch.optim.lr_scheduler import LambdaLR
from torch.utils.data import DataLoader
from tqdm import tqdm
from logger import Logger
from molecule_dataset import RandomMoleculeDataset
import torch
import numpy as np
from config import MoleculeConfig
from model.molecule_transformer import MoleculeTransformer, dict_to_cpu
def save_checkpoint(checkpoint: dict, filename: str, config: MoleculeConfig):
os.makedirs(config.results_path, exist_ok=True)
path = os.path.join(config.results_path, filename)
torch.save(checkpoint, path)
def train_for_one_epoch(epoch: int, config: MoleculeConfig, network: MoleculeTransformer,
optimizer: torch.optim.Optimizer, dataset: RandomMoleculeDataset):
print("---- Loading dataset")
dataloader = DataLoader(dataset, batch_size=1, shuffle=True, num_workers=config.num_dataloader_workers,
pin_memory=False, persistent_workers=True)
metrics = dict()
# Train for one epoch
network.train()
accumulated_loss = 0
accumulated_loss_lvl_zero = 0
accumulated_loss_lvl_one = 0
accumulated_loss_lvl_two = 0
num_batches = len(dataloader)
progress_bar = tqdm(range(num_batches))
data_iter = iter(dataloader)
for _ in progress_bar:
data = next(data_iter)
input_data = {k: v[0].to(network.device) for k, v in data["input"].items()}
# targets for the logit levels
target_zero = data["target_zero"][0].to(network.device)
target_one = data["target_one"][0].to(network.device)
target_two = data["target_two"][0].to(network.device)
logits_zero, logits_one, logits_two = network(input_data)
# We mask the output according to feasibility
logits_zero[input_data["feasibility_mask_level_zero"]] = float("-inf")
logits_one[input_data["feasibility_mask_level_one"]] = float("-inf")
logits_two[input_data["feasibility_mask_level_two"]] = float("-inf")
criterion = CrossEntropyLoss(reduction="mean", ignore_index=-1)
loss_zero = criterion(logits_zero, target_zero)
loss_zero = torch.tensor(0.) if torch.isnan(loss_zero) else loss_zero
loss_one = criterion(logits_one, target_one)
loss_one = torch.tensor(0.) if torch.isnan(loss_one) else loss_one
loss_two = criterion(logits_two, target_two)
loss_two = torch.tensor(0.) if torch.isnan(loss_two) else loss_two
loss = loss_zero + config.scale_factor_level_one * loss_one + config.scale_factor_level_two * loss_two
# Optimization step
optimizer.zero_grad(set_to_none=True)
loss.backward()
if config.optimizer["gradient_clipping"] > 0:
torch.nn.utils.clip_grad_norm_(network.parameters(), max_norm=config.optimizer["gradient_clipping"])
optimizer.step()
batch_loss = loss.item()
accumulated_loss += loss.item()
accumulated_loss_lvl_zero += loss_zero.item()
accumulated_loss_lvl_one += loss_one.item()
accumulated_loss_lvl_two += loss_two.item()
progress_bar.set_postfix({"batch_loss": batch_loss})
del data
metrics["full_loss"] = accumulated_loss / num_batches
metrics["loss_level_zero"] = accumulated_loss_lvl_zero / num_batches
metrics["loss_level_one"] = accumulated_loss_lvl_one / num_batches
metrics["loss_level_two"] = accumulated_loss_lvl_two / num_batches
torch.cuda.empty_cache()
gc.collect()
return metrics
if __name__ == '__main__':
pretrain_train_dataset = "./data/pretrain_data.pickle" # Path to the pretraining dataset
pretrain_num_epochs = 1000 # For how many epochs to train
batch_size = 128 # Minibatch size. Adjust to your resources. (~32 for 24GB VRAM)
num_batches_per_epoch = 2500 # Number of minibatches per epoch.
training_device = "cuda:0" # Device on which to train. Set to "cpu" if no CUDA available.
num_dataloader_workers = 30 # Number of dataloader workers for creating batches for training
load_checkpoint_from_path = None
print(">> Pretraining Molecule Design")
parser = argparse.ArgumentParser(description='Experiment')
parser.add_argument('--config', help="Path to optional config relative to main.py")
args = parser.parse_args()
if args.config is not None:
# Load config from given path
MoleculeConfig = importlib.import_module(args.config).MoleculeConfig
config = MoleculeConfig()
print(f"Results path: {config.results_path}")
config.training_device = training_device
config.num_dataloader_workers = num_dataloader_workers
config.max_num_atoms = None
config.disallow_oxygen_bonding = False
config.disallow_nitrogen_nitrogen_single_bond = False
config.disallow_rings = False
config.disallow_rings_larger_than = -1
logger = Logger(args, config.results_path, config.log_to_file)
logger.log_hyperparams(config)
# Fix random number generator seed for better reproducibility
np.random.seed(config.seed)
torch.manual_seed(config.seed)
# Setup the neural network for training
network = MoleculeTransformer(config, config.training_device)
# Load checkpoint if needed
if load_checkpoint_from_path is not None:
print(f"Loading checkpoint from path {load_checkpoint_from_path}")
checkpoint = torch.load(load_checkpoint_from_path)
print(f"{checkpoint['pretrain_epochs_trained']} episodes have been trained in the loaded checkpoint.")
else:
checkpoint = {
"model_weights": None,
"best_model_weights": None,
"optimizer_state": None,
"pretrain_epochs_trained": 0,
"pretrain_best_validation_loss": float("inf"),
"epochs_trained": 0,
"validation_metric": float("-inf"), # objective of the best molecule designed during validation.
"best_validation_metric": float("-inf") # corresponding to best model weights
}
if checkpoint["model_weights"] is not None:
network.load_state_dict(checkpoint["model_weights"])
print(f"Policy network is on device {config.training_device}")
network.to(network.device)
network.eval()
if pretrain_num_epochs > 0:
# Training loop
print(f"Starting pre-training for {pretrain_num_epochs} epochs.")
best_validation_metric = checkpoint["pretrain_best_validation_loss"]
print("Setting up optimizer.")
optimizer = torch.optim.Adam(
network.parameters(),
lr=config.optimizer["lr"],
weight_decay=config.optimizer["weight_decay"]
)
if checkpoint["optimizer_state"] is not None and config.load_optimizer_state:
print("Loading optimizer state from checkpoint.")
optimizer.load_state_dict(
checkpoint["optimizer_state"]
)
print("Setting up LR scheduler")
_lambda = lambda epoch: config.optimizer["schedule"]["decay_factor"] ** (
checkpoint["pretrain_epochs_trained"] // config.optimizer["schedule"]["decay_lr_every_epochs"])
scheduler = LambdaLR(optimizer, lr_lambda=_lambda)
dataset = RandomMoleculeDataset(config, pretrain_train_dataset,
batch_size=batch_size,
custom_num_batches=num_batches_per_epoch)
for epoch in range(pretrain_num_epochs):
generated_loggable_dict = train_for_one_epoch(
epoch, config, network, optimizer, dataset
)
checkpoint["pretrain_epochs_trained"] += 1
scheduler.step()
print(f">> Epoch {checkpoint['pretrain_epochs_trained']}. Avg loss level 0: {generated_loggable_dict['loss_level_zero']},"
f" Avg loss level 1: {generated_loggable_dict['loss_level_one']},"
f" Avg loss level 2: {generated_loggable_dict['loss_level_two']}")
logger.log_metrics(generated_loggable_dict, step=epoch)
# Save model
checkpoint["model_weights"] = copy.deepcopy(network.get_weights())
checkpoint["optimizer_state"] = copy.deepcopy(
dict_to_cpu(optimizer.state_dict())
)
#val_metric = generated_loggable_dict["best_gen_obj"] # measure by best objective found during sampling
#checkpoint["validation_metric"] = val_metric
save_checkpoint(checkpoint, "last_model.pt", config)
# @TODO true validation set
if generated_loggable_dict["full_loss"] < checkpoint["pretrain_best_validation_loss"]:
print(">> Got new best model.")
checkpoint["pretrain_best_validation_loss"] = generated_loggable_dict["full_loss"]
save_checkpoint(checkpoint, "best_model.pt", config)