-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathtrain.py
184 lines (165 loc) · 7.61 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
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
import os
import shutil
import argparse
import yaml
from easydict import EasyDict
from tqdm.auto import tqdm
from glob import glob
import torch
import torch.utils.tensorboard
from torch.nn.utils import clip_grad_norm_
from torch_geometric.data import DataLoader
from models.epsnet import get_model
from utils.datasets import ConformationDataset
from utils.transforms import *
from utils.misc import *
from utils.common import get_optimizer, get_scheduler
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('config', type=str)
parser.add_argument('--device', type=str, default='cuda')
parser.add_argument('--resume_iter', type=int, default=None)
parser.add_argument('--logdir', type=str, default='./logs')
args = parser.parse_args()
resume = os.path.isdir(args.config)
if resume:
config_path = glob(os.path.join(args.config, '*.yml'))[0]
resume_from = args.config
else:
config_path = args.config
with open(config_path, 'r') as f:
config = EasyDict(yaml.safe_load(f))
config_name = os.path.basename(config_path)[:os.path.basename(config_path).rfind('.')]
seed_all(config.train.seed)
# Logging
if resume:
log_dir = get_new_log_dir(args.logdir, prefix=config_name, tag='resume')
os.symlink(os.path.realpath(resume_from), os.path.join(log_dir, os.path.basename(resume_from.rstrip("/"))))
else:
log_dir = get_new_log_dir(args.logdir, prefix=config_name)
shutil.copytree('./models', os.path.join(log_dir, 'models'))
ckpt_dir = os.path.join(log_dir, 'checkpoints')
os.makedirs(ckpt_dir, exist_ok=True)
logger = get_logger('train', log_dir)
writer = torch.utils.tensorboard.SummaryWriter(log_dir)
logger.info(args)
logger.info(config)
shutil.copyfile(config_path, os.path.join(log_dir, os.path.basename(config_path)))
# Datasets and loaders
logger.info('Loading datasets...')
transforms = CountNodesPerGraph()
train_set = ConformationDataset(config.dataset.train, transform=transforms)
val_set = ConformationDataset(config.dataset.val, transform=transforms)
train_iterator = inf_iterator(DataLoader(train_set, config.train.batch_size, shuffle=True))
val_loader = DataLoader(val_set, config.train.batch_size, shuffle=False)
# Model
logger.info('Building model...')
model = get_model(config.model).to(args.device)
# Optimizer
optimizer_global = get_optimizer(config.train.optimizer, model.model_global)
optimizer_local = get_optimizer(config.train.optimizer, model.model_local)
scheduler_global = get_scheduler(config.train.scheduler, optimizer_global)
scheduler_local = get_scheduler(config.train.scheduler, optimizer_local)
start_iter = 1
# Resume from checkpoint
if resume:
ckpt_path, start_iter = get_checkpoint_path(os.path.join(resume_from, 'checkpoints'), it=args.resume_iter)
logger.info('Resuming from: %s' % ckpt_path)
logger.info('Iteration: %d' % start_iter)
ckpt = torch.load(ckpt_path)
model.load_state_dict(ckpt['model'])
optimizer_global.load_state_dict(ckpt['optimizer_global'])
optimizer_local.load_state_dict(ckpt['optimizer_local'])
scheduler_global.load_state_dict(ckpt['scheduler_global'])
scheduler_local.load_state_dict(ckpt['scheduler_local'])
def train(it):
model.train()
optimizer_global.zero_grad()
optimizer_local.zero_grad()
batch = next(train_iterator).to(args.device)
loss, loss_global, loss_local = model.get_loss(
atom_type=batch.atom_type,
pos=batch.pos,
bond_index=batch.edge_index,
bond_type=batch.edge_type,
batch=batch.batch,
num_nodes_per_graph=batch.num_nodes_per_graph,
num_graphs=batch.num_graphs,
anneal_power=config.train.anneal_power,
return_unreduced_loss=True
)
loss = loss.mean()
loss.backward()
orig_grad_norm = clip_grad_norm_(model.parameters(), config.train.max_grad_norm)
optimizer_global.step()
optimizer_local.step()
logger.info('[Train] Iter %05d | Loss %.2f | Loss(Global) %.2f | Loss(Local) %.2f | Grad %.2f | LR(Global) %.6f | LR(Local) %.6f' % (
it, loss.item(), loss_global.mean().item(), loss_local.mean().item(), orig_grad_norm, optimizer_global.param_groups[0]['lr'], optimizer_local.param_groups[0]['lr'],
))
writer.add_scalar('train/loss', loss, it)
writer.add_scalar('train/loss_global', loss_global.mean(), it)
writer.add_scalar('train/loss_local', loss_local.mean(), it)
writer.add_scalar('train/lr_global', optimizer_global.param_groups[0]['lr'], it)
writer.add_scalar('train/lr_local', optimizer_local.param_groups[0]['lr'], it)
writer.add_scalar('train/grad_norm', orig_grad_norm, it)
writer.flush()
def validate(it):
sum_loss, sum_n = 0, 0
sum_loss_global, sum_n_global = 0, 0
sum_loss_local, sum_n_local = 0, 0
with torch.no_grad():
model.eval()
for i, batch in enumerate(tqdm(val_loader, desc='Validation')):
batch = batch.to(args.device)
loss, loss_global, loss_local = model.get_loss(
atom_type=batch.atom_type,
pos=batch.pos,
bond_index=batch.edge_index,
bond_type=batch.edge_type,
batch=batch.batch,
num_nodes_per_graph=batch.num_nodes_per_graph,
num_graphs=batch.num_graphs,
anneal_power=config.train.anneal_power,
return_unreduced_loss=True
)
sum_loss += loss.sum().item()
sum_n += loss.size(0)
sum_loss_global += loss_global.sum().item()
sum_n_global += loss_global.size(0)
sum_loss_local += loss_local.sum().item()
sum_n_local += loss_local.size(0)
avg_loss = sum_loss / sum_n
avg_loss_global = sum_loss_global / sum_n_global
avg_loss_local = sum_loss_local / sum_n_local
if config.train.scheduler.type == 'plateau':
scheduler_global.step(avg_loss_global)
scheduler_local.step(avg_loss_local)
else:
scheduler_global.step()
scheduler_local.step()
logger.info('[Validate] Iter %05d | Loss %.6f | Loss(Global) %.6f | Loss(Local) %.6f' % (
it, avg_loss, avg_loss_global, avg_loss_local,
))
writer.add_scalar('val/loss', avg_loss, it)
writer.add_scalar('val/loss_global', avg_loss_global, it)
writer.add_scalar('val/loss_local', avg_loss_local, it)
writer.flush()
return avg_loss
try:
for it in range(start_iter, config.train.max_iters + 1):
train(it)
if it % config.train.val_freq == 0 or it == config.train.max_iters:
avg_val_loss = validate(it)
ckpt_path = os.path.join(ckpt_dir, '%d.pt' % it)
torch.save({
'config': config,
'model': model.state_dict(),
'optimizer_global': optimizer_global.state_dict(),
'scheduler_global': scheduler_global.state_dict(),
'optimizer_local': optimizer_local.state_dict(),
'scheduler_local': scheduler_local.state_dict(),
'iteration': it,
'avg_val_loss': avg_val_loss,
}, ckpt_path)
except KeyboardInterrupt:
logger.info('Terminating...')