-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgcn.py
179 lines (135 loc) · 5.34 KB
/
gcn.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
import argparse
import copy
import logging
import pdb
import pickle
import random
from collections import namedtuple
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import scipy
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import yaml
logger = logging.getLogger(__name__)
Data = namedtuple('Data', ['x', 'a', 'y', 'num_classes', 'train_index', 'dev_index', 'test_index'])
class GraphConv(nn.Module):
def __init__(self, input_size, output_size):
super().__init__()
self.weight = nn.Parameter(torch.empty(input_size, output_size))
self.bias = nn.Parameter(torch.empty(output_size))
self.reset_parameters()
def reset_parameters(self):
nn.init.xavier_uniform_(self.weight)
self.bias.data.fill_(0)
def forward(self, x, a):
x = torch.mm(x, self.weight)
x = torch.spmm(a, x)
x = x + self.bias
return x
class GCN(nn.Module):
def __init__(self, input_size, output_size, hidden_size, dropout_rate):
super().__init__()
self.conv1 = GraphConv(input_size, hidden_size)
self.conv2 = GraphConv(hidden_size, output_size)
self.dropout_rate = dropout_rate
def forward(self, data):
x, a = data.x, data.a
x = self.conv1(x, a)
x = F.relu(x)
x = F.dropout(x, p=self.dropout_rate, training=self.training)
x = self.conv2(x, a)
return F.log_softmax(x, dim=1)
def to_sparse_tensor(x):
x = x.tocoo()
i = torch.tensor(np.vstack((x.row, x.col)), dtype=torch.long)
v = torch.tensor(x.data, dtype=torch.float)
return torch.sparse_coo_tensor(i, v, torch.Size(x.shape))
def normalize(x):
return scipy.sparse.diags(np.array(x.sum(1)).flatten() ** -1).dot(x)
def read_file(name):
filename = f'data/ind.cora.{name}'
if name == 'test.index':
return np.loadtxt(filename, dtype=np.long)
else:
with open(filename, 'rb') as f:
return pickle.load(f, encoding='latin1')
def load_data(device):
names = ['x', 'y', 'tx', 'ty', 'allx', 'ally', 'graph', 'test.index']
x, y, tx, ty, allx, ally, graph, test_index = [read_file(name) for name in names]
num_classes = y.shape[1]
train_index = torch.arange(y.shape[0]).to(device)
dev_index = torch.arange(y.shape[0], y.shape[0] + 500).to(device)
test_index_sorted = torch.tensor(np.sort(test_index)).to(device)
test_index = torch.tensor(test_index).to(device)
x = torch.tensor(normalize(scipy.sparse.vstack([allx, tx])).todense()).to(device)
y = torch.tensor(np.vstack([ally, ty]).argmax(axis=1)).to(device)
x[test_index] = x[test_index_sorted]
y[test_index] = y[test_index_sorted]
adj = nx.adjacency_matrix(nx.from_dict_of_lists(graph))
a = to_sparse_tensor(normalize(adj + scipy.sparse.eye(adj.shape[0]))).to(device)
return Data(x, a, y, num_classes, train_index, dev_index, test_index)
def setup():
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--config_path', type=str, required=True)
parser.add_argument('-g', '--gpu', type=int, default=None)
args = parser.parse_args()
with open(args.config_path) as f:
config_data = f.read()
config = yaml.load(config_data)
logging.basicConfig(
handlers=[logging.FileHandler(config['log_file'], mode='w'), logging.StreamHandler()],
format='%(asctime)s %(levelname)s %(message)s',
datefmt='%H:%M:%S',
)
logger.setLevel(getattr(logging, config['log_level'].upper()))
logger.info('Configuration:\n' + config_data)
if config['seed']:
random.seed(config['seed'])
np.random.seed(config['seed'])
torch.manual_seed(config['seed'])
use_gpu = args.gpu is not None and torch.cuda.is_available()
device = torch.device(f'cuda:{args.gpu}' if use_gpu else 'cpu')
logger.info(f'Device: {device}')
return config, device
def evaluate(gcn, data, split_index):
z = gcn(data)[split_index]
y = data.y[split_index]
loss = F.nll_loss(z, y)
acc = torch.sum(torch.argmax(z, dim=1) == y).item() / y.shape[0]
return loss, acc
def main():
config, device = setup()
logger.setLevel(getattr(logging, config['log_level'].upper()))
data = load_data(device)
gcn = GCN(data.x.shape[1], data.num_classes, config['hidden_size'], config['dropout_rate']).to(device)
optimizer = optim.Adam(gcn.parameters(), lr=config['lr'], weight_decay=config['weight_decay'])
best_dev_acc = 0
for i in range(1, config['iterations'] + 1):
gcn.train()
loss, acc = evaluate(gcn, data, data.train_index)
optimizer.zero_grad()
loss.backward()
optimizer.step()
gcn.eval()
with torch.no_grad():
dev_loss, dev_acc = evaluate(gcn, data, data.dev_index)
if dev_acc > best_dev_acc:
best_dev_acc = dev_acc
best_model = copy.deepcopy(gcn)
logger.info(
'Iter: {:6d} '
'Train loss: {:.4f} '
'Train acc: {:.4f} '
'Dev loss: {:.4f} '
'Dev acc: {:.4f}'.format(i, loss.item(), acc, dev_loss.item(), dev_acc)
)
best_model.eval()
with torch.no_grad():
_, test_acc = evaluate(best_model, data, data.test_index)
logger.info(f'Test acc: {test_acc:.4f}')
if __name__ == '__main__':
main()