-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtrain_zinc_subset.py
94 lines (72 loc) · 2.98 KB
/
train_zinc_subset.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
import argparse
import torch
from torch.optim import Adam
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch_geometric.datasets import ZINC
from torch_geometric.data import DataLoader
from transform import JunctionTree
from model import Net
parser = argparse.ArgumentParser()
parser.add_argument('--device', type=int, default=0)
parser.add_argument('--hidden_channels', type=int, default=128)
parser.add_argument('--num_layers', type=int, default=3)
parser.add_argument('--dropout', type=float, default=0.0)
parser.add_argument('--epochs', type=int, default=300)
parser.add_argument('--no_inter_message_passing', action='store_true')
args = parser.parse_args()
print(args)
root = 'data/ZINC'
transform = JunctionTree()
train_dataset = ZINC(root, subset=True, split='train', pre_transform=transform)
val_dataset = ZINC(root, subset=True, split='val', pre_transform=transform)
test_dataset = ZINC(root, subset=True, split='test', pre_transform=transform)
train_loader = DataLoader(train_dataset, 128, shuffle=True, num_workers=12)
val_loader = DataLoader(val_dataset, 1000, shuffle=False, num_workers=12)
test_loader = DataLoader(test_dataset, 1000, shuffle=False, num_workers=12)
device = f'cuda:{args.device}' if torch.cuda.is_available() else 'cpu'
model = Net(hidden_channels=args.hidden_channels, out_channels=1,
num_layers=args.num_layers, dropout=args.dropout,
inter_message_passing=not args.no_inter_message_passing).to(device)
def train(epoch):
model.train()
total_loss = 0
for data in train_loader:
data = data.to(device)
optimizer.zero_grad()
loss = (model(data).squeeze() - data.y).abs().mean()
loss.backward()
total_loss += loss.item() * data.num_graphs
optimizer.step()
return total_loss / len(train_loader.dataset)
@torch.no_grad()
def test(loader):
model.eval()
total_error = 0
for data in loader:
data = data.to(device)
total_error += (model(data).squeeze() - data.y).abs().sum().item()
return total_error / len(loader.dataset)
test_maes = []
for run in range(1, 5):
print()
print(f'Run {run}:')
print()
model.reset_parameters()
optimizer = Adam(model.parameters(), lr=0.001)
scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.5,
patience=10, min_lr=0.00001)
best_val_mae = test_mae = float('inf')
for epoch in range(1, args.epochs + 1):
lr = scheduler.optimizer.param_groups[0]['lr']
loss = train(epoch)
val_mae = test(val_loader)
scheduler.step(val_mae)
if val_mae < best_val_mae:
best_val_mae = val_mae
test_mae = test(test_loader)
print(f'Epoch: {epoch:03d}, LR: {lr:.5f}, Loss: {loss:.4f}, '
f'Val: {val_mae:.4f}, Test: {test_mae:.4f}')
test_maes.append(test_mae)
test_mae = torch.tensor(test_maes)
print('===========================')
print(f'Final Test: {test_mae.mean():.4f} ± {test_mae.std():.4f}')