forked from HShokaku/AttFpPost_PyG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_model.py
186 lines (167 loc) · 4.53 KB
/
train_model.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
import argparse
from types import SimpleNamespace
import pytorch_lightning as pl
import torch
torch.set_float32_matmul_precision('high')
import wandb
from _util import get_callbacks, get_datamodule, get_logger, get_model
from src.models.enum import Architecture, DensityType
from src.util import disable_rdkit_logging
def str_to_bool(v):
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
else:
raise argparse.ArgumentTypeError("Boolean value expected.")
def train(config, accelerator="gpu" if torch.cuda.is_available() else None, devices=[1]):
run = wandb.init(
project="AttFp-PyG",
name="debug",
)
pl.seed_everything(config.seed)
data_module = get_datamodule(
config.dataset_name, batch_size=config.batch_size // len(devices)
)
model = get_model(
lr=config.lr,
architecture=config.architecture,
density_type=config.density_type,
n_ffn_layers=config.n_ffn_layers,
latent_dim=config.latent_dim,
n_density=config.n_density,
hidden_features=config.hidden_features,
num_layers=config.num_layers,
num_timesteps=config.num_timesteps,
dropout=config.dropout,
N=data_module.get_train_labels_ratio(),
)
model.setup_metrics()
wandb_logger = get_logger(run)
wandb_logger.watch(model)
callbacks = get_callbacks()
trainer = pl.Trainer(
max_epochs=config.num_epochs,
accelerator=accelerator,
devices=devices,
num_sanity_val_steps=0,
logger=wandb_logger,
callbacks=callbacks,
)
trainer.fit(model, data_module)
def parse_args():
default_config = SimpleNamespace(
seed=1,
num_epochs=500,
batch_size=16,
lr=5e-4,
dataset_name="debug",
architecture=Architecture.AttFpPost,
density_type=DensityType.IAF,
n_ffn_layers=3,
latent_dim=6,
n_density=6,
hidden_features=256,
num_layers=3,
num_timesteps=2,
dropout=0.2,
)
parser = argparse.ArgumentParser(
prog="train_model.py",
description="Train model",
epilog="Example: python train_model.py",
)
parser.add_argument(
"--architecture",
type=Architecture,
help="Architecture",
default=default_config.architecture,
)
parser.add_argument(
"--density_type",
type=DensityType,
help="DensityType",
default=default_config.density_type,
)
parser.add_argument(
"--seed",
type=int,
help="Random seed",
default=default_config.seed,
)
parser.add_argument(
"--dataset_name",
type=str,
help="Dataset name",
default=default_config.dataset_name,
)
parser.add_argument(
"--num_epochs",
type=int,
help="Number of epochs",
default=default_config.num_epochs,
)
parser.add_argument(
"--batch_size",
type=int,
help="Batch size",
default=default_config.batch_size,
)
parser.add_argument(
"--lr",
type=float,
help="Learning rate",
default=default_config.lr,
)
parser.add_argument(
"--num_layers",
type=int,
help="Number of layers",
default=default_config.num_layers,
)
parser.add_argument(
"--hidden_features",
type=int,
help="Number of hidden features",
default=default_config.hidden_features,
)
parser.add_argument(
"--n_ffn_layers",
type=int,
help="Number of ffn layers",
default=default_config.n_ffn_layers,
)
parser.add_argument(
"--latent_dim",
type=int,
help="Number of latent dimensions",
default=default_config.n_ffn_layers,
)
parser.add_argument(
"--n_density",
type=int,
help="Number of density",
default=default_config.n_density,
)
parser.add_argument(
"--num_timesteps",
type=int,
help="Number of timestep",
default=default_config.num_timesteps,
)
parser.add_argument(
"--dropout",
type=float,
help="Dropout rate",
default=default_config.dropout,
)
config = parser.parse_args()
return config
def main():
disable_rdkit_logging()
config = parse_args()
train(config)
if __name__ == "__main__":
main()