-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
60 lines (46 loc) · 1.88 KB
/
train.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
import json
import torch
from rdkit import RDLogger
from utils.datasets import MOLECULAR_DATASETS, load_dataset
from utils.train import train, evaluate
from utils.evaluate import count_parameters
from models import graphspn_prel
from models import graphspn_zero
MODELS = {
**graphspn_prel.MODELS,
**graphspn_zero.MODELS
}
CHECKPOINT_DIR = 'results/training/model_checkpoint/'
EVALUATION_DIR = 'results/training/model_evaluation/'
if __name__ == '__main__':
torch.set_float32_matmul_precision('medium')
RDLogger.DisableLog('rdApp.*')
dataset = 'qm9'
names = [
'graphspn_zero_sort',
# 'graphspn_zero_none',
# 'graphspn_zero_rand',
# 'graphspn_zero_free',
# 'graphspn_zero_kary'
]
# names = MODELS.keys()
for name in names:
with open(f'config/{dataset}/{name}.json', 'r') as f:
hyperpars = json.load(f)
hyperpars['atom_list'] = MOLECULAR_DATASETS[dataset]['atom_list']
hyperpars['max_atoms'] = MOLECULAR_DATASETS[dataset]['max_atoms']
model = MODELS[name](**hyperpars['model_hyperpars'])
print(dataset)
print(json.dumps(hyperpars, indent=4))
print(model)
print(f'The number of parameters is {count_parameters(model)}.')
if 'sort' in name:
canonical = True
else:
canonical = False
loader_trn, loader_val = load_dataset(hyperpars['dataset'], hyperpars['batch_size'], split=[0.8, 0.2], canonical=canonical)
smiles_trn = [x['s'] for x in loader_trn.dataset]
path = train(model, loader_trn, loader_val, smiles_trn, hyperpars, CHECKPOINT_DIR)
model = torch.load(path)
metrics = evaluate(model, loader_trn, loader_val, smiles_trn, hyperpars, EVALUATION_DIR, compute_nll=False, canonical=canonical)
print("\n".join(f'{key:<16}{value:>10.4f}' for key, value in metrics.items()))