-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
105 lines (91 loc) · 4.13 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
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
import json
import datetime
import argparse
import torch
from datasets import ReactionSmilesDataset, BERTFpsReactionSmilesDataset
from model import fit_model, NeuralMapper
from utils.early_stopping import split_train_val
from config import Config, get_conf_dict
parser = argparse.ArgumentParser()
parser.add_argument('--config', '-c', type=str, default='config.yaml',
help='path to configuration file')
parser.add_argument('--perplexity', '-p', type=int, default=None,
help='perplexity to use instead of one in the config')
parser.add_argument('--epochs', '-e', type=int, default=None,
help='perplexity to use instead of one in the config')
parser.add_argument('--batchsize', '-b', type=int, default=None,
help='batch size to use instead of one in the config')
args = parser.parse_args()
cfg_path = get_conf_dict(args.config)
config = Config(cfg_path)
if __name__ == '__main__':
# Defining instruments
if config.seed:
torch.manual_seed(config.seed)
dev = torch.device(config.dev)
print(dev, flush=True)
settings = config.problem_settings["reactions"]
train_path = f"data/{settings['train_filename']}"
val_path = f"data/{settings['val_filename']}" if settings['val_filename'] is not None else None
val_points_ds = None
print("Train:", train_path, flush=True)
if val_path is not None:
print("Validation:", val_path, flush=True)
fp_method = settings["fp_method"]
if fp_method == "transformer":
dim_input = 256
no_agents = settings["no_agents"]
points_ds = BERTFpsReactionSmilesDataset(train_path, no_agents, dev)
if val_path is not None:
val_points_ds = BERTFpsReactionSmilesDataset(val_path, no_agents, dev)
else:
params = {"n_bits": settings["n_bits"],
"fp_type": settings["fp_type"],
"include_agents": settings["include_agents"],
"agent_weight": settings["agent_weight"],
"non_agent_weight": settings["non_agent_weight"],
"bit_ratio_agents": settings["bit_ratio_agents"]
}
dim_input = settings["n_bits"]
points_ds = ReactionSmilesDataset(train_path, dev, fp_method, params)
if val_path is not None:
val_points_ds = ReactionSmilesDataset(val_path, dev, fp_method, params)
net = NeuralMapper
ffnn = net(dim_input=dim_input).to(dev)
opt = torch.optim.Adam(ffnn.parameters(), **config.optimization_conf)
# Training and evaluating
start = datetime.datetime.now()
if args.perplexity is not None:
config.training_params["perplexity"] = args.perplexity
if args.epochs is not None:
config.training_params["n_epochs"] = args.epochs
if args.batchsize is not None:
config.training_params["batch_size"] = args.batchsize
report_config = json.dumps({"settings": settings,
"optimization": config.optimization_conf,
"training": config.training_params})
bsize = config.training_params["batch_size"]
if val_path is None:
train_dl, val_dl = split_train_val(points_ds,
val_size=150000,
batch_size=bsize,
seed=config.seed)
else:
train_dl, _ = split_train_val(points_ds,
val_size=0,
batch_size=bsize,
seed=config.seed)
val_dl, _ = split_train_val(val_points_ds,
val_size=0,
batch_size=bsize,
seed=config.seed)
fit_model(ffnn,
train_dl,
val_dl,
opt,
**config.training_params,
epochs_to_save_after=config.epochs_to_save_after,
save_dir_path=config.save_dir_path,
configuration_report=report_config)
fin = datetime.datetime.now()
print("Training time:", fin - start, flush=True)