-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
183 lines (138 loc) · 8.04 KB
/
trainer.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 torch
from torch import optim
import torch.nn.functional as F
from torch.utils.data import DataLoader
from model import GNN, EGNN
from data import NBodyDataset, QM9Dataset, prepare_batch
class Trainer():
def __init__(self, config):
self.config = config
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
print(f'Device is {self.device}')
if config.task == 'nbody':
train_dataset = NBodyDataset(files_path=config.data.root, split='train',
num_samples=config.data.max_num_samples, time_start=config.data.time_start,
time_end=config.data.time_end)
self.train_dataloader = DataLoader(train_dataset, batch_size=config.training.batch_size, shuffle=True)
val_dataset = NBodyDataset(files_path=config.data.root, split='val',
num_samples=config.data.max_num_samples, time_start=config.data.time_start,
time_end=config.data.time_end)
self.val_dataloader = DataLoader(val_dataset, batch_size=config.training.batch_size, shuffle=False)
test_dataset = NBodyDataset(files_path=config.data.root, split='test',
num_samples=config.data.max_num_samples, time_start=config.data.time_start,
time_end=config.data.time_end)
self.test_dataloader = DataLoader(test_dataset, batch_size=config.training.batch_size, shuffle=False)
self.loss_function = F.mse_loss
elif config.task == 'qm9':
train_dataset = QM9Dataset(files_path=config.data.root, split='train',
target_property=config.data.target_property)
# train_dataset = torch.utils.data.Subset(train_dataset, list(range(100)))
self.train_dataloader = DataLoader(train_dataset, batch_size=config.training.batch_size, shuffle=True)
val_dataset = QM9Dataset(files_path=config.data.root, split='valid',
target_property=config.data.target_property)
self.val_dataloader = DataLoader(val_dataset, batch_size=config.training.batch_size, shuffle=False)
test_dataset = QM9Dataset(files_path=config.data.root, split='test',
target_property=config.data.target_property)
self.test_dataloader = DataLoader(test_dataset, batch_size=config.training.batch_size, shuffle=False)
self.loss_function = F.l1_loss
else:
raise NotImplementedError
if config.model.type == 'gnn':
self.model = GNN(node_dim=config.model.node_dim, edge_dim=config.model.edge_dim,
hidden_dim=config.model.hidden_dim, num_nodes=config.training.num_nodes,
output_dim=config.model.output_dim, num_blocks=config.model.num_blocks,
infer_edges=config.model.infer_edges, prediction_type=config.model.prediction_type).to(self.device)
elif config.model.type == 'egnn':
self.model = EGNN(node_dim=config.model.node_dim, coords_dim=config.model.coords_dim,
vel_dim=config.model.vel_dim, edge_dim=config.model.edge_dim, hidden_dim=config.model.hidden_dim,
num_nodes=config.training.num_nodes, output_dim=config.model.output_dim,
num_blocks=config.model.num_blocks, prediction_type=config.model.prediction_type,
infer_edges=config.model.infer_edges, use_velocity=config.model.use_velocity,
update_coords=config.model.update_coords).to(self.device)
else:
raise ValueError('Unknown model type')
self.optimizer = optim.Adam(self.model.parameters(), lr=config.training.lr,
weight_decay=config.training.weight_decay)
if config.task == 'qm9':
self.scheduler = optim.lr_scheduler.CosineAnnealingLR(self.optimizer, T_max=config.training.epochs)
else:
self.scheduler = None
def run(self):
best_val_loss = float('inf')
for epoch in range(self.config.training.epochs):
train_loss = self.train()
val_loss = self.validate()
print(f'Epoch {epoch + 1}: train loss {train_loss}, val loss {val_loss}')
if val_loss <= best_val_loss:
best_val_loss = val_loss
torch.save({
'epoch': epoch,
'model_state_dict': self.model.state_dict(),
'optimizer_state_dict': self.optimizer.state_dict(),
'loss': val_loss,
'learning_rate': self.optimizer.param_groups[0]['lr']
}, 'best.ckpt')
# Test here
test_loss = self.test()
print(f'Test loss {test_loss}')
def train(self):
self.model.train()
total_loss = 0.0
total_norm = 0.0
for step, batch in enumerate(self.train_dataloader):
self.optimizer.zero_grad()
batch = prepare_batch(batch)
sample, label = batch
sample, label = [x.to(self.device) for x in sample], label.to(self.device)
if label.ndim == 3:
label = label.reshape(-1, label.shape[1])
nodes, edge_indices, edge_features = sample
out = self.model(nodes.float(), edge_indices, edge_features.float())
train_loss = self.loss_function(out, label)
train_loss.backward()
batch_grad_norm = self.get_grad_norm()
total_norm += batch_grad_norm
self.optimizer.step()
total_loss += train_loss.item()
if self.scheduler is not None:
self.scheduler.step()
print(f'Grad norm {total_norm / len(self.train_dataloader)}')
return train_loss / len(self.train_dataloader)
def validate(self):
self.model.eval()
total_val_loss = 0.0
for step, batch in enumerate(self.val_dataloader):
batch = prepare_batch(batch)
sample, label = batch
sample, label = [x.to(self.device) for x in sample], label.to(self.device)
if label.ndim == 3:
label = label.reshape(-1, label.shape[1])
nodes, edge_indices, edge_features = sample
with torch.no_grad():
out = self.model(nodes.float(), edge_indices, edge_features.float())
val_loss = self.loss_function(out, label)
total_val_loss += val_loss.item()
return total_val_loss / len(self.val_dataloader)
def test(self):
self.model.eval()
total_test_loss = 0.0
for step, batch in enumerate(self.test_dataloader):
batch = prepare_batch(batch)
sample, label = batch
sample, label = [x.to(self.device) for x in sample], label.to(self.device)
if label.ndim == 3:
label = label.reshape(-1, label.shape[1])
nodes, edge_indices, edge_features = sample
with torch.no_grad():
out = self.model(nodes.float(), edge_indices, edge_features.float())
test_loss = self.loss_function(out, label)
total_test_loss += test_loss.item()
return total_test_loss / len(self.test_dataloader)
def get_grad_norm(self):
batch_grad_norm = 0
for p in self.model.parameters():
if p.grad is not None:
param_norm = p.grad.detach().data.norm(2)
batch_grad_norm += param_norm.item() ** 2
batch_grad_norm = batch_grad_norm ** 0.5
return batch_grad_norm