-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
correct_and_smooth.py
89 lines (74 loc) · 2.87 KB
/
correct_and_smooth.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
import os.path as osp
import torch
from ogb.nodeproppred import Evaluator, PygNodePropPredDataset
import torch_geometric.transforms as T
from torch_geometric.nn import MLP, CorrectAndSmooth
from torch_geometric.typing import WITH_TORCH_SPARSE
if not WITH_TORCH_SPARSE:
quit("This example requires 'torch-sparse'")
root = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', 'OGB')
dataset = PygNodePropPredDataset('ogbn-products', root,
transform=T.ToSparseTensor())
evaluator = Evaluator(name='ogbn-products')
split_idx = dataset.get_idx_split()
data = dataset[0]
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = MLP([dataset.num_features, 200, 200, dataset.num_classes], dropout=0.5,
norm="batch_norm", act_first=True).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)
criterion = torch.nn.CrossEntropyLoss()
x, y = data.x.to(device), data.y.to(device)
train_idx = split_idx['train'].to(device)
val_idx = split_idx['valid'].to(device)
test_idx = split_idx['test'].to(device)
x_train, y_train = x[train_idx], y[train_idx]
def train():
model.train()
optimizer.zero_grad()
out = model(x_train)
loss = criterion(out, y_train.view(-1))
loss.backward()
optimizer.step()
return float(loss)
@torch.no_grad()
def test(out=None):
model.eval()
out = model(x) if out is None else out
pred = out.argmax(dim=-1, keepdim=True)
train_acc = evaluator.eval({
'y_true': y[train_idx],
'y_pred': pred[train_idx]
})['acc']
val_acc = evaluator.eval({
'y_true': y[val_idx],
'y_pred': pred[val_idx]
})['acc']
test_acc = evaluator.eval({
'y_true': y[test_idx],
'y_pred': pred[test_idx]
})['acc']
return train_acc, val_acc, test_acc, out
best_val_acc = 0
for epoch in range(1, 301):
loss = train()
train_acc, val_acc, test_acc, out = test()
if val_acc > best_val_acc:
best_val_acc = val_acc
y_soft = out.softmax(dim=-1)
print(f'Epoch: {epoch:03d}, Loss: {loss:.4f}, '
f'Train: {train_acc:.4f}, Val: {val_acc:.4f}, Test: {test_acc:.4f}')
adj_t = data.adj_t.to(device)
deg = adj_t.sum(dim=1).to(torch.float)
deg_inv_sqrt = deg.pow_(-0.5)
deg_inv_sqrt[deg_inv_sqrt == float('inf')] = 0
DAD = deg_inv_sqrt.view(-1, 1) * adj_t * deg_inv_sqrt.view(1, -1)
DA = deg_inv_sqrt.view(-1, 1) * deg_inv_sqrt.view(-1, 1) * adj_t
post = CorrectAndSmooth(num_correction_layers=50, correction_alpha=1.0,
num_smoothing_layers=50, smoothing_alpha=0.8,
autoscale=False, scale=20.)
print('Correct and smooth...')
y_soft = post.correct(y_soft, y_train, train_idx, DAD)
y_soft = post.smooth(y_soft, y_train, train_idx, DA)
print('Done!')
train_acc, val_acc, test_acc, _ = test(y_soft)
print(f'Train: {train_acc:.4f}, Val: {val_acc:.4f}, Test: {test_acc:.4f}')