-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_meg.py
330 lines (274 loc) · 10.1 KB
/
train_meg.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
import torch
import utils
import numpy as np
import torchvision
import json
import os
import utils
import networkx as nx
import typer
import random
from models.explainer import CF_Tox21, NCF_Tox21, Agent, CF_Esol, NCF_Esol
from torch.utils.tensorboard import SummaryWriter
from utils import SortedQueue, morgan_bit_fingerprint, get_split, get_dgn, mol_to_smiles, x_map_tox21, pyg_to_mol_tox21, mol_from_smiles, mol_to_tox21_pyg
from torch.nn import functional as F
from torch_geometric.utils import to_networkx
def tox21(general_params,
base_path,
writer,
num_counterfactuals,
original_molecule,
model_to_explain,
**args):
out, (_, original_encoding) = model_to_explain(original_molecule.x,
original_molecule.edge_index)
logits = F.softmax(out, dim=-1).detach().squeeze()
pred_class = logits.argmax().item()
assert pred_class == original_molecule.y.item()
original_molecule.smiles = mol_to_smiles(pyg_to_mol_tox21(original_molecule))
print(f'Molecule: {original_molecule.smiles}')
atoms_ = [
x_map_tox21(e).name
for e in np.unique(
[x.tolist().index(1) for x in original_molecule.x.numpy()]
)
]
params = {
# General-purpose params
**general_params,
'init_mol': original_molecule.smiles,
'atom_types': set(atoms_),
# Task-specific params
'original_molecule': original_molecule,
'model_to_explain': model_to_explain,
'weight_sim': 0.2,
'similarity_measure': 'combined'
}
cf_queue = SortedQueue(num_counterfactuals, sort_predicate=lambda mol: mol['reward'])
cf_env = CF_Tox21(**params)
cf_env.initialize()
def action_encoder(action):
return morgan_bit_fingerprint(action, args['fp_length'], args['fp_radius']).numpy()
meg_train(writer,
action_encoder,
args['fp_length'],
cf_env,
cf_queue,
marker="cf",
tb_name="tox21",
id_function=lambda action: action,
args=args)
overall_queue = []
overall_queue.append({
'pyg': original_molecule,
'marker': 'og',
'smiles': original_molecule.smiles,
'encoding': original_encoding.numpy(),
'prediction': {
'type': 'bin_classification',
'output': logits.numpy().tolist(),
'for_explanation': original_molecule.y.item(),
'class': original_molecule.y.item()
}
})
overall_queue.extend(cf_queue.data_)
save_results(base_path, overall_queue, args)
def esol(general_params,
base_path,
writer,
num_counterfactuals,
original_molecule,
model_to_explain,
**args):
original_molecule.x = original_molecule.x.float()
og_prediction, original_encoding = model_to_explain(original_molecule.x, original_molecule.edge_index)
print(f'Molecule: {original_molecule.smiles}')
atoms_ = np.unique(
[x.GetSymbol() for x in mol_from_smiles(original_molecule.smiles).GetAtoms()]
)
params = {
# General-purpose params
**general_params,
'init_mol': original_molecule.smiles,
'atom_types': set(atoms_),
# Task-specific params
'model_to_explain': model_to_explain,
'original_molecule': original_molecule,
'weight_sim': 0.2,
'similarity_measure': 'combined',
}
cf_queue = SortedQueue(num_counterfactuals, sort_predicate=lambda mol: mol['reward'])
cf_env = CF_Esol(**params)
cf_env.initialize()
def action_encoder(action):
return morgan_bit_fingerprint(action, args['fp_length'], args['fp_radius']).numpy()
meg_train(writer,
action_encoder,
args['fp_length'],
cf_env,
cf_queue,
marker="cf",
tb_name="esol",
id_function=lambda action: action,
args=args)
overall_queue = []
overall_queue.append({
'pyg': original_molecule,
'marker': 'og',
'smiles': original_molecule.smiles,
'encoding': original_encoding.numpy(),
'prediction': {
'type': 'regression',
'output': og_prediction.squeeze().detach().numpy().tolist(),
'for_explanation': og_prediction.squeeze().detach().numpy().tolist()
}
})
overall_queue.extend(cf_queue.data_)
save_results(base_path, overall_queue, args)
def meg_train(writer,
action_encoder,
n_input,
environment,
queue,
marker,
tb_name,
id_function,
args):
device = torch.device("cpu")
agent = Agent(n_input + 1, 1, device, args['lr'], args['replay_buffer_size'])
eps = 1.0
batch_losses = []
episode = 0
it = 0
while episode < args['epochs']:
steps_left = args['max_steps_per_episode'] - environment.num_steps_taken
valid_actions = list(environment.get_valid_actions())
observations = np.vstack(
[
np.append(action_encoder(action), steps_left)
for action in valid_actions
]
)
observations = torch.as_tensor(observations).float()
a = agent.action_step(observations, eps)
action = valid_actions[a]
result = environment.step(action)
action_embedding = np.append(
action_encoder(action),
steps_left
)
_, out, done = result
writer.add_scalar(f'{tb_name}/reward', out['reward'], it)
writer.add_scalar(f'{tb_name}/prediction', out['reward_pred'], it)
writer.add_scalar(f'{tb_name}/similarity', out['reward_sim'], it)
steps_left = args['max_steps_per_episode'] - environment.num_steps_taken
action_embeddings = np.vstack(
[
np.append(action_encoder(action), steps_left)
for action in environment.get_valid_actions()
]
)
agent.replay_buffer.push(
torch.as_tensor(action_embedding).float(),
torch.as_tensor(out['reward']).float(),
torch.as_tensor(action_embeddings).float(),
float(result.terminated)
)
if it % args['update_interval'] == 0 and len(agent.replay_buffer) >= args['batch_size']:
loss = agent.train_step(
args['batch_size'],
args['gamma'],
args['polyak']
)
loss = loss.item()
batch_losses.append(loss)
it += 1
if done:
episode += 1
print(f'({args["sample"]}) Episode {episode}> Reward = {out["reward"]:.4f} (pred: {out["reward_pred"]:.4f}, sim: {out["reward_sim"]:.4f})')
queue.insert({
'marker': marker,
'id': id_function(action),
**out
})
eps *= 0.9987
# eps = max(eps, 0.05)
batch_losses = []
environment.initialize()
def save_results(base_path, queue, args):
output_dir = base_path + f"/meg_output/{args['sample']}"
embedding_dir = output_dir + "/embeddings"
if not os.path.exists(output_dir):
os.makedirs(output_dir)
os.makedirs(embedding_dir)
for i, molecule in enumerate(queue):
np.save(embedding_dir + f"/{i}", molecule.pop('encoding'))
pyg = molecule.pop('pyg')
with open(output_dir + "/seed", "w") as outf:
json.dump(args['seed'], outf)
with open(output_dir + "/data.json", "w") as outf:
json.dump(queue, outf, indent=2)
def main(dataset: str,
experiment_name: str = typer.Argument("test"),
sample: int = typer.Option(0),
epochs: int = typer.Option(5000),
max_steps_per_episode: int = typer.Option(1),
num_counterfactuals: int = typer.Option(10),
fp_length: int = typer.Option(1024),
fp_radius: int = typer.Option(2),
lr: float = typer.Option(1e-4),
polyak: float = typer.Option(0.995),
gamma: float = typer.Option(0.95),
discount: float = typer.Option(0.9),
replay_buffer_size: int = typer.Option(10000),
batch_size: int = typer.Option(1),
update_interval: int = typer.Option(1),
allow_no_modification: bool = typer.Option(False),
allow_removal: bool = typer.Option(True),
allow_node_addition: bool = typer.Option(True),
allow_edge_addition: bool = typer.Option(True),
allow_bonds_between_rings: bool = typer.Option(True),
seed: int = typer.Option(random.randint(0, 2**12))
):
general_params = {
# General-purpose params
'discount_factor': discount,
'allow_removal': allow_removal,
'allow_no_modification': allow_no_modification,
'allow_bonds_between_rings': allow_bonds_between_rings,
'allow_node_addition': allow_node_addition,
'allow_edge_addition': allow_edge_addition,
'allowed_ring_sizes': set([5, 6]),
'max_steps': max_steps_per_episode,
'fp_len': fp_length,
'fp_rad': fp_radius
}
dataset = dataset.lower()
if dataset == 'tox21':
meg = tox21
elif dataset == 'esol':
meg = esol
torch.manual_seed(seed)
base_path = f'./runs/{dataset.lower()}/{experiment_name}'
meg(general_params,
base_path,
SummaryWriter(f'{base_path}/plots'),
num_counterfactuals,
get_split(dataset.lower(), 'test', experiment_name)[sample],
model_to_explain=get_dgn(dataset.lower(), experiment_name),
experiment_name=experiment_name,
sample=sample,
epochs=epochs,
max_steps_per_episode=max_steps_per_episode,
fp_length=fp_length,
fp_radius=fp_radius,
lr=lr,
polyak=polyak,
gamma=gamma,
discount=discount,
replay_buffer_size=replay_buffer_size,
batch_size=batch_size,
update_interval=update_interval,
seed=seed)
if __name__ == '__main__':
typer.run(main)