-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_reinforce_trans_agent.py
250 lines (200 loc) · 9.55 KB
/
train_reinforce_trans_agent.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
import os
import sys
import wandb
import hydra
import torch
import random
import numpy as np
import torch.optim as optim
from omegaconf import DictConfig
from optimizer import BaseOptimizer
path_here = os.path.dirname(os.path.realpath(__file__))
from models.reinforce import TransPolicy
from data import smiles_vocabulary, selfies_vocabulary
def set_seed(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def get_params(model):
return (p for p in model.parameters() if p.requires_grad)
def masked_mean(values, mask, axis=None):
"""Compute mean of tensor with a masked values."""
if axis is not None:
return (values * mask).sum(axis=axis) / mask.sum(axis=axis)
else:
return (values * mask).sum() / mask.sum()
class reinforce_optimizer(BaseOptimizer):
def __init__(self, cfg=None):
super().__init__(cfg)
self.agent_name = cfg.agent_name
def _init(self, cfg):
if cfg.dataset == 'chembl':
saved_path = 'saved/' + cfg.dataset + '/' + cfg.model_name + '_' + cfg.rep + '/' + cfg.saved_name
vocab_path = 'data/chembl/chembl_' + cfg.rep + '_vocab.txt'
if cfg.rep=='smiles':
max_dataset_len = 112
elif cfg.rep=='selfies':
max_dataset_len = 106
if cfg.max_len > max_dataset_len:
cfg.max_len = max_dataset_len
print('*** Changing the maximum length of sampled molecules because it was set to be greater than the maximum length seen during training ***')
elif cfg.dataset == 'zinc250k':
saved_path = 'saved/' + cfg.dataset + '/' + cfg.model_name + '_' + cfg.rep + '/' + cfg.saved_name
vocab_path = 'data/zinc250k/zinc_' + cfg.rep + '_vocab.txt'
max_dataset_len = 73
if cfg.max_len > max_dataset_len:
cfg.max_len = max_dataset_len
print('*** Changing the maximum length of sampled molecules because it was set to be greater than the maximum length seen during training ***')
elif cfg.dataset == 'zinc1m':
saved_path = 'saved/' + cfg.dataset + '/' + cfg.model_name + '_' + cfg.rep + '/' + cfg.saved_name
vocab_path = 'data/zinc1m/zinc_' + cfg.rep + '_vocab.txt'
max_dataset_len = 74
if cfg.max_len > max_dataset_len:
cfg.max_len = max_dataset_len
print('*** Changing the maximum length of sampled molecules because it was set to be greater than the maximum length seen during training ***')
elif cfg.dataset == 'zinc10m':
saved_path = 'saved/' + cfg.dataset + '/' + cfg.model_name + '_' + cfg.rep + '/' + cfg.saved_name
vocab_path = 'data/zinc10m/zinc_' + cfg.rep + '_vocab.txt'
if cfg.rep=='smiles':
max_dataset_len = 85
elif cfg.rep=='selfies':
max_dataset_len = 88
if cfg.max_len > max_dataset_len:
cfg.max_len = max_dataset_len
print('*** Changing the maximum length of sampled molecules because it was set to be greater than the maximum length seen during training ***')
elif cfg.dataset == 'zinc100m':
saved_path = 'saved/' + cfg.dataset + '/' + cfg.model_name + '_' + cfg.rep + '/' + cfg.saved_name
vocab_path = 'data/zinc100m/zinc_' + cfg.rep + '_vocab.txt'
if cfg.rep=='smiles':
max_dataset_len = 85
elif cfg.rep=='selfies':
max_dataset_len = 88
if cfg.max_len > max_dataset_len:
cfg.max_len = max_dataset_len
print('*** Changing the maximum length of sampled molecules because it was set to be greater than the maximum length seen during training ***')
else:
raise NotImplementedError
#get data
if cfg.rep == 'smiles':
self.vocab = smiles_vocabulary(vocab_path=os.path.join(path_here, vocab_path))
elif cfg.rep == 'selfies':
self.vocab = selfies_vocabulary(vocab_path=os.path.join(path_here, vocab_path))
else:
raise NotImplementedError
print('Vocab assigned')
self.target_entropy = - 0.98 * torch.log(1 / torch.tensor(len(self.vocab)))
self.log_alpha = torch.zeros(1, requires_grad=True, device=self.device)
self.alpha = self.log_alpha.exp().item()
self.a_optimizer = optim.Adam([self.log_alpha], lr=3e-4, eps=1e-4)
assert cfg.model_name == 'char_trans'
#get prior
prior_saved_dict = torch.load(os.path.join(path_here, saved_path))
print('Prior loaded')
# get agent
self.agent = TransPolicy(self.vocab, max_dataset_len, cfg.n_heads, cfg.n_embed, cfg.n_layers, dropout=cfg.dropout)
print('Agent class initialised')
self.agent.to(self.device)
print('Agent class transferred to cuda memory')
self.agent.load_save_dict(prior_saved_dict)
print('Prior weights initialised')
# get optimizers
self.optimizer = torch.optim.Adam(get_params(self.agent), lr=cfg['learning_rate'])
print('Initialisation of optimizer is done!')
def update(self, obs, rewards, nonterms, episode_lens, cfg, metrics, log):
rev_returns = torch.cumsum(rewards, dim=0)
advantages = rewards - rev_returns + rev_returns[-1:]
logprobs, log_of_probs, action_probs = self.agent.get_likelihood(obs, nonterms)
# print(logprobs)
# print(act_probs)
# print(logprobs.shape)
# print(act_probs.shape)
# exit()
loss_pg = -advantages * logprobs
loss_pg = loss_pg.sum(0, keepdim=True).mean()
#loss_p = - (1 / logprobs.sum(0, keepdim=True)).mean()
loss = loss_pg #+ cfg.lp_coef * loss_p
loss = loss_pg + self.alpha * logprobs.sum(0, keepdim=True).mean()
# Calculate gradients and make an update to the network weights
self.optimizer.zero_grad()
loss.backward()
grad_norm = torch.nn.utils.clip_grad_norm_(self.agent.parameters(), 0.5)
self.optimizer.step()
alpha_loss = (action_probs.detach() * (-self.log_alpha.exp() * (log_of_probs + self.target_entropy).detach())).mean()
self.a_optimizer.zero_grad()
alpha_loss.backward()
self.a_optimizer.step()
self.alpha = self.log_alpha.exp().item()
if log:
metrics['pg_loss'] = loss_pg.item()
metrics['agent_likelihood'] = logprobs.sum(0).mean().item()
metrics['grad_norm'] = grad_norm.item()
metrics['smiles_len'] = episode_lens.float().mean().item()
# metrics['loss_p'] = loss_p.item()
metrics['alpha'] = self.alpha
metrics['alpha_loss'] = alpha_loss.detach().item()
print('logging!')
wandb.log(metrics)
def optimize(self, cfg):
if cfg.wandb_log:
self.define_wandb_metrics()
#set device
self.device = torch.device(cfg.device)
self._init(cfg)
train_steps = 0
eval_strings = 0
metrics = dict()
print('Start training ... ')
while eval_strings < cfg.max_strings:
with torch.no_grad():
# sample experience
obs, rewards, nonterms, episode_lens = self.agent.get_data(cfg.batch_size, cfg.max_len, self.device)
smiles_list = []
for en_sms in obs.cpu().numpy().T:
sms = self.vocab.decode_padded(en_sms)
smiles_list.append(sms)
score = np.array(self.predict(smiles_list))
scores = torch.tensor(score, dtype=torch.float32, device=self.device).unsqueeze(0)
if self.finish:
print('max oracle hit')
wandb.finish()
sys.exit(0)
train_steps += 1
eval_strings += cfg.batch_size
log = False
if cfg.wandb_log and train_steps % cfg.train_log_interval == 0:
log = True
metrics = dict()
metrics['eval_strings'] = eval_strings
metrics['mean_score'] = np.mean(score)
metrics['max_score'] = np.max(score)
metrics['min_score'] = np.min(score)
metrics['mean_episode_lens'] = np.mean(episode_lens.tolist())
metrics['max_episode_lens'] = np.max(episode_lens.tolist())
metrics['min_episode_lens'] = np.min(episode_lens.tolist())
wandb.log(metrics)
rewards = rewards * scores
self.update(obs, rewards, nonterms, episode_lens, cfg, metrics, log)
print('max training string hit')
wandb.finish()
sys.exit(0)
@hydra.main(config_path='cfgs', config_name='reinforce_trans', version_base=None)
def main(cfg: DictConfig):
hydra_cfg = hydra.core.hydra_config.HydraConfig.get()
if cfg.wandb_log:
project_name = cfg.task + '_' + cfg.target
if cfg.wandb_dir is not None:
cfg.wandb_dir = path_here
else:
cfg.wandb_dir = hydra_cfg['runtime']['output_dir']
wandb.init(project=project_name, entity=cfg.wandb_entity, config=dict(cfg), dir=cfg.wandb_dir)
wandb.run.name = cfg.wandb_run_name
set_seed(cfg.seed)
cfg.output_dir = hydra_cfg['runtime']['output_dir']
optimizer = reinforce_optimizer(cfg)
optimizer.optimize(cfg)
sys.exit(0)
if __name__ == '__main__':
main()
sys.exit(0)
exit()