-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_main.py
183 lines (144 loc) · 6.4 KB
/
train_main.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
from torch import nn
import torch
import numpy as np
import argparse
import os
from utils.train_utils import EarlyStopping, evaluate_score, \
save_results, load_config, data2iter, get_logger
from dataset.data import MolDataset
from model.GATv2_base import GATv2
from model.GIN_base import GIN
def seed_all(seed):
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def train(config, logger):
# Initialize logger
desc = config.desc
seed = config.data.seed
data_name = config.data.data_name
save_folder = config.train.save_folder
model_type = config.model.readout.name + '_' + data_name.split('\\')[-1].split('.')[0] + f'[{str(seed)}]'
logger.info(f'############{desc}############')
logger.info(vars(config))
# Load train config
num_epochs = config.train.num_epochs
lr = config.train.lr
patience = config.train.patience
task = config.train.task
device = config.train.device
log_interval = config.train.log_interval
# Load data
logger.info("-----------Dataset Loading-----------")
batch_size = config.data.batch_size
train_ratio = config.data.train_ratio
dataset = MolDataset(data_name)
dataset.transform(depth=config.model.readout.num_layers)
train_loader, eval_loader, test_loader = data2iter(dataset, seed, batch_size, train_ratio)
# training init
seed_all(42)
early_stopping = EarlyStopping(patience=patience,
path=os.path.join(save_folder, 'checkpoints/'),
model_type=model_type)
assert task in ['regression', 'binary', 'multiclass']
# define loss function
if task == 'regression':
criterion = nn.MSELoss()
elif task == 'binary':
criterion = nn.BCEWithLogitsLoss()
elif task == 'multiclass':
criterion = nn.CrossEntropyLoss()
logger.info("------------Model Creating-----------")
model = GATv2(config=config).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=lr)
# training loop
logger.info("------------Train Running------------")
for epoch in range(num_epochs):
model.train()
loss_sum = 0
num_examples = 0
for i, batch in enumerate(train_loader):
# forward
model.to(device)
batch = batch.to(device)
y = batch.y.reshape((-1, 1))
outputs = model(batch)
if task == 'multiclass':
loss = criterion(outputs, y.flatten())
else:
loss = criterion(outputs, y)
# backward
optimizer.zero_grad()
loss.backward()
optimizer.step()
num_examples += y.shape[0]
loss_sum += loss.item() * y.shape[0]
val_metric = evaluate_score(model, eval_loader, task)
metric = config.train.metric
if epoch % log_interval == 0:
if task == 'regression':
logger.info(f'epoch:{epoch}, loss = {loss_sum / num_examples: .4f}, '
f'val loss = {val_metric["loss"]:.4f}, '
f'val {metric} = {np.round(val_metric[metric][0], decimals=4)}')
else:
logger.info(f'epoch:{epoch}, loss ={loss_sum / num_examples: .4f}, '
f'val loss = {val_metric["loss"]:.4f}, '
f'val {metric} = {np.round(val_metric[metric], decimals=4)}')
# early stopping
min_metrics = np.array(val_metric[metric]).mean()
if task == 'binary':
min_metrics = -val_metric[metric]
early_stopping(min_metrics, model)
if early_stopping.early_stop:
logger.info('------------Early stopping------------')
break
model.load_state_dict(torch.load(os.path.join(save_folder, 'checkpoints', model_type + '.pt')))
test_metric = evaluate_score(model, test_loader, task)
val_metric = evaluate_score(model, eval_loader, task)
if task == 'regression':
logger.info(f'test {metric} = {np.round(test_metric[metric][0], decimals=4)}, '
f'val {metric} = {np.round(val_metric[metric][0], decimals=4)}')
else:
logger.info(f'test {metric} = {np.round(test_metric[metric], decimals=4)}, '
f'val {metric} = {np.round(val_metric[metric], decimals=4)}')
save_results(model, test_loader, os.path.join(save_folder, 'pred_results'), model_type)
return (test_metric[metric][0], val_metric[metric][0]) if task=='regression' else (test_metric[metric], val_metric[metric])
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Parser For Arguments', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-model', default='Add')
parser.add_argument('-desc', default='Testing')
parser.add_argument('-dataset', default='freesolv')
parser.add_argument('-config_name', default='train.yml')
args = parser.parse_args()
# create folders to save results if it not exists
log_path = './results/logs'
if not os.path.exists(log_path):
os.makedirs(log_path, exist_ok=True)
ckpt_path = './results/checkpoints'
if not os.path.exists(ckpt_path):
os.makedirs(ckpt_path, exist_ok=True)
processed_path = './dataset/processed_files'
if not os.path.exists(processed_path):
os.makedirs(processed_path, exist_ok=True)
# dataset-task
dataset_task = {
'freesolv': 'regression',
'delaney': 'regression',
'lipo': 'regression',
'bbbp': 'binary',
'bace': 'binary'
}
# load config
config_path = os.path.join('.\configs', args.config_name)
config = load_config(config_path)
config.data.data_name = args.dataset
config.train.task = dataset_task[args.dataset]
config.model.readout.name = args.model
config.desc = args.desc
config.train.metric = 'RMSE' if config.train.task == 'regression' else 'ROC-AUC'
# logger
seed = config.data.seed
save_folder = config.train.save_folder
model_type = config.model.readout.name + '_' + args.dataset.split('\\')[-1].split('.')[0] + f'[{str(seed)}]'
logger = get_logger(model_type+f'({config.desc})'+'.log', os.path.join(save_folder, 'logs/'))
train(config, logger)