-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmain.py
123 lines (94 loc) · 3.79 KB
/
main.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
from torch_geometric.datasets import Planetoid
import torch
import torch.nn.functional as F
from torch_geometric.nn import MessagePassing
from torch_geometric.utils import add_self_loops, degree
import networkx as nx
import numpy as np
import matplotlib.pyplot as plt
class GCNConv(MessagePassing):
def __init__(self, in_channels, out_channels):
super(GCNConv, self).__init__(aggr='add') # "Add" aggregation
self.lin = torch.nn.Linear(in_channels, out_channels)
def forward(self, x, edge_index):
# Step 1: Add self-loops
edge_index, _ = add_self_loops(edge_index, num_nodes=x.size(0))
# Step 2: Multiply with weights
x = self.lin(x)
# Step 3: Calculate the normalization
row, col = edge_index
deg = degree(row, x.size(0), dtype=x.dtype)
deg_inv_sqrt = deg.pow(-0.5)
norm = deg_inv_sqrt[row] * deg_inv_sqrt[col]
# Step 4: Propagate the embeddings to the next layer
return self.propagate(edge_index, size=(x.size(0), x.size(0)), x=x,
norm=norm)
def message(self, x_j, norm):
# Normalize node features.
return norm.view(-1, 1) * x_j
class Net(torch.nn.Module):
def __init__(self, dataset):
super(Net, self).__init__()
self.conv1 = GCNConv(dataset.num_node_features, 16)
self.conv2 = GCNConv(16, dataset.num_classes)
def forward(self, data):
x, edge_index = data.x, data.edge_index
x = self.conv1(x, edge_index)
x = F.relu(x)
x = F.dropout(x, training=self.training)
x = self.conv2(x, edge_index)
return F.log_softmax(x, dim=1)
def plot_dataset(dataset):
edges_raw = dataset.data.edge_index.numpy()
edges = [(x, y) for x, y in zip(edges_raw[0, :], edges_raw[1, :])]
labels = dataset.data.y.numpy()
G = nx.Graph()
G.add_nodes_from(list(range(np.max(edges_raw))))
G.add_edges_from(edges)
plt.subplot(111)
options = {
'node_size': 30,
'width': 0.2,
}
nx.draw(G, with_labels=False, node_color=labels.tolist(), cmap=plt.cm.tab10, font_weight='bold', **options)
plt.show()
def test(data, train=True):
model.eval()
correct = 0
pred = model(data).max(dim=1)[1]
if train:
correct += pred[data.train_mask].eq(data.y[data.train_mask]).sum().item()
return correct / (len(data.y[data.train_mask]))
else:
correct += pred[data.test_mask].eq(data.y[data.test_mask]).sum().item()
return correct / (len(data.y[data.test_mask]))
def train(data, plot=False):
train_accuracies, test_accuracies = list(), list()
for epoch in range(100):
model.train()
optimizer.zero_grad()
out = model(data)
loss = F.nll_loss(out[data.train_mask], data.y[data.train_mask])
loss.backward()
optimizer.step()
train_acc = test(data)
test_acc = test(data, train=False)
train_accuracies.append(train_acc)
test_accuracies.append(test_acc)
print('Epoch: {:03d}, Loss: {:.5f}, Train Acc: {:.5f}, Test Acc: {:.5f}'.
format(epoch, loss, train_acc, test_acc))
if plot:
plt.plot(train_accuracies, label="Train accuracy")
plt.plot(test_accuracies, label="Validation accuracy")
plt.xlabel("# Epoch")
plt.ylabel("Accuracy")
plt.legend(loc='upper right')
plt.show()
if __name__ == "__main__":
dataset = Planetoid(root='/tmp/Cora', name='Cora')
plot_dataset(dataset)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = Net(dataset).to(device)
data = dataset[0].to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.01, weight_decay=5e-4)
train(data, plot=True)