forked from kkteru/grail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_classifier.py
executable file
·49 lines (38 loc) · 1.91 KB
/
graph_classifier.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
from .rgcn_model import RGCN
from dgl import mean_nodes
import torch.nn as nn
import torch
"""
File based off of dgl tutorial on RGCN
Source: https://github.com/dmlc/dgl/tree/master/examples/pytorch/rgcn
"""
class GraphClassifier(nn.Module):
def __init__(self, params, relation2id): # in_dim, h_dim, rel_emb_dim, out_dim, num_rels, num_bases):
super().__init__()
self.params = params
self.relation2id = relation2id
self.gnn = RGCN(params) # in_dim, h_dim, h_dim, num_rels, num_bases)
self.rel_emb = nn.Embedding(self.params.num_rels, self.params.rel_emb_dim, sparse=False)
if self.params.add_ht_emb:
self.fc_layer = nn.Linear(3 * self.params.num_gcn_layers * self.params.emb_dim + self.params.rel_emb_dim, 1)
else:
self.fc_layer = nn.Linear(self.params.num_gcn_layers * self.params.emb_dim + self.params.rel_emb_dim, 1)
def forward(self, data):
g, rel_labels = data
g.ndata['h'] = self.gnn(g)
g_out = mean_nodes(g, 'repr')
head_ids = (g.ndata['id'] == 1).nonzero().squeeze(1)
head_embs = g.ndata['repr'][head_ids]
#print(head_embs)
tail_ids = (g.ndata['id'] == 2).nonzero().squeeze(1)
tail_embs = g.ndata['repr'][tail_ids]
#print(tail_embs)
if self.params.add_ht_emb:
g_rep = torch.cat([g_out.view(-1, self.params.num_gcn_layers * self.params.emb_dim),
head_embs.view(-1, self.params.num_gcn_layers * self.params.emb_dim),
tail_embs.view(-1, self.params.num_gcn_layers * self.params.emb_dim),
self.rel_emb(rel_labels)], dim=1)
else:
g_rep = torch.cat([g_out.view(-1, self.params.num_gcn_layers * self.params.emb_dim), self.rel_emb(rel_labels)], dim=1)
return self.fc_layer(g_rep), head_embs, tail_embs, head_ids, tail_ids
#return output