forked from pbloem/gated-rgcn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
236 lines (156 loc) · 6.92 KB
/
train.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import torch
from torch import nn
import torch.nn.functional as F
from torch.autograd import Variable
import util
import data
import rdflib as rdf
import pandas as pd
import numpy as np
import random, sys, tqdm
from tqdm import trange
from argparse import ArgumentParser
def go(arg):
dev = 'cuda' if torch.cuda.is_available() else 'cpu'
edges, (n2i, i2n), (r2i, i2r), train, test = data.load(arg.name, final=arg.final, limit=arg.limit)
# Convert test and train to tensors
train_idx = [n2i[name] for name, _ in train.items()]
train_lbl = [cls for _, cls in train.items()]
train_idx = torch.tensor(train_idx, dtype=torch.long, device=dev)
train_lbl = torch.tensor(train_lbl, dtype=torch.long, device=dev)
test_idx = [n2i[name] for name, _ in test.items()]
test_lbl = [cls for _, cls in test.items()]
test_idx = torch.tensor(test_idx, dtype=torch.long, device=dev)
test_lbl = torch.tensor(test_lbl, dtype=torch.long, device=dev)
# count nr of classes
cls = set([int(l) for l in test_lbl] + [int(l) for l in train_lbl])
"""
Define model
"""
num_cls = len(cls)
class GATLayer(nn.Module):
def __init__(self, graph):
super().__init__()
self.i2n, self.i2r, self.edges = graph
froms, tos = [], []
for p in edges.keys():
froms.extend(edges[p][0])
tos.extend(edges[p][1])
self.register_buffer('froms', torch.tensor(froms, dtype=torch.long))
self.register_buffer('tos', torch.tensor(tos, dtype=torch.long))
def forward(self, nodes, rels, sample=None):
n, k = nodes.size()
k, k, r = rels.size()
if arg.dense:
froms = nodes[None, :, :].expand(r, n, k)
rels = rels.permute(2, 0, 1)
froms = torch.bmm(froms, rels)
froms = froms.view(r*n, k)
adj = torch.mm(froms, nodes.t()) # stacked adjacencies
adj = F.softmax(adj, dim=0)
nwnodes = torch.mm(adj, nodes)
nwnodes = nwnodes.view(r, n, k)
nwnodes = nwnodes.mean(dim=0)
return nwnodes
else:
rels = [rels[None, :, :, p].expand(len(self.edges[p][0]), k, k) for p in range(r)]
rels = torch.cat(rels, dim=0)
assert len(self.froms) == rels.size(0)
froms = nodes[self.froms, :]
tos = nodes[self.tos, :]
froms, tos = froms[:, None, :], tos[:, :, None]
# unnormalized attention weights
att = torch.bmm(torch.bmm(froms, rels), tos).squeeze()
if sample is None:
indices = torch.cat([self.froms[:, None], self.tos[:, None]], dim=1)
values = att
else:
pass
self.values = values
self.values.retain_grad()
# normalize the values (TODO try sparsemax)
values = util.logsoftmax(indices, values, (n, n), p=10, row=True)
values = torch.exp(values)
mm = util.sparsemm(torch.cuda.is_available())
return mm(indices.t(), values, (n, n), nodes)
class Model(nn.Module):
def __init__(self, k, num_classes, graph, depth=3):
super().__init__()
self.i2n, self.i2r, self.edges = graph
self.num_classes = num_classes
n = len(self.i2n)
# relation embeddings
self.rels = nn.Parameter(torch.randn(k, k, len(self.i2r) + 1)) # TODO initialize properly (like distmult?)
# node embeddings (layer 0)
self.nodes = nn.Parameter(torch.randn(n, k)) # TODO initialize properly (like embedding?)
self.layers = nn.ModuleList()
for _ in range(depth):
self.layers.append(GATLayer(graph))
self.toclass = nn.Sequential(
nn.Linear(k, num_classes), nn.Softmax(dim=-1)
)
def forward(self, sample=None):
nodes = self.nodes
for layer in self.layers:
nodes = layer(nodes, self.rels, sample=sample)
return self.toclass(nodes)
model = Model(k=arg.emb_size, depth=arg.depth, num_classes=num_cls, graph=(i2n, i2r, edges))
if torch.cuda.is_available():
model.cuda()
train_lbl = train_lbl.cuda()
test_lbl = test_lbl.cuda()
opt = torch.optim.Adam(model.parameters(), lr=arg.lr)
for e in tqdm.trange(arg.epochs):
opt.zero_grad()
cls = model()[train_idx, :]
loss = F.cross_entropy(cls, train_lbl)
loss.backward()
opt.step()
print(e, loss.item(), e)
# Evaluate
with torch.no_grad():
cls = model()[train_idx, :]
agreement = cls.argmax(dim=1) == train_lbl
accuracy = float(agreement.sum()) / agreement.size(0)
print(' train accuracy ', float(accuracy))
cls = model()[test_idx, :]
agreement = cls.argmax(dim=1) == test_lbl
accuracy = float(agreement.sum()) / agreement.size(0)
print(' test accuracy ', float(accuracy))
print('training finished.')
if __name__ == "__main__":
## Parse the command line options
parser = ArgumentParser()
parser.add_argument("-e", "--epochs",
dest="epochs",
help="Size (nr of dimensions) of the input.",
default=150, type=int)
parser.add_argument("-d", "--depth",
dest="depth",
help="Nr of attention layers.",
default=4, type=int)
parser.add_argument("-E", "--embedding-size",
dest="emb_size",
help="Size (nr of dimensions) of the node embeddings.",
default=16, type=int)
parser.add_argument("-l", "--learn-rate",
dest="lr",
help="Learning rate",
default=0.001, type=float)
parser.add_argument("-D", "--dataset-name",
dest="name",
help="Name of dataset to use [aifb, am]",
default='aifb', type=str)
parser.add_argument("-F", "--final", dest="final",
help="Use the canonical test set instead of a validation split.",
action="store_true")
parser.add_argument("--dense", dest="dense",
help="Use a dense adjacency matrix with the canonical softmax.",
action="store_true")
parser.add_argument("--limit",
dest="limit",
help="Limit the number of relations.",
default=None, type=int)
options = parser.parse_args()
print('OPTIONS ', options)
go(options)