-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
linkx.py
52 lines (41 loc) · 1.62 KB
/
linkx.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
import os.path as osp
import torch
import torch.nn.functional as F
from torch_geometric.datasets import LINKXDataset
from torch_geometric.nn import LINKX
if torch.cuda.is_available():
device = torch.device('cuda')
elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
device = torch.device('mps')
else:
device = torch.device('cpu')
path = osp.join(osp.dirname(osp.realpath(__file__)), '..', 'data', 'LINKX')
dataset = LINKXDataset(path, name='Penn94')
data = dataset[0].to(device)
model = LINKX(data.num_nodes, data.num_features, hidden_channels=32,
out_channels=dataset.num_classes, num_layers=1,
num_edge_layers=1, num_node_layers=1, dropout=0.5).to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=1e-3)
def train():
model.train()
optimizer.zero_grad()
out = model(data.x, data.edge_index)
mask = data.train_mask[:, 0] # Use the first set of the five masks.
loss = F.cross_entropy(out[mask], data.y[mask])
loss.backward()
optimizer.step()
return float(loss)
@torch.no_grad()
def test():
accs = []
model.eval()
pred = model(data.x, data.edge_index).argmax(dim=-1)
for _, mask in data('train_mask', 'val_mask', 'test_mask'):
mask = mask[:, 0] # Use the first set of the five masks.
accs.append(int((pred[mask] == data.y[mask]).sum()) / int(mask.sum()))
return accs
for epoch in range(1, 201):
loss = train()
train_acc, val_acc, test_acc = test()
print(f'Epoch: {epoch:03d}, Loss: {loss:.4f}, Train: {train_acc:.4f}, '
f'Val: {val_acc:.4f}, Test: {test_acc:.4f}')