-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
71 lines (62 loc) · 2.2 KB
/
model.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
import torch.nn as nn
import torch
class BaseRGCN(nn.Module):
def __init__(self, num_nodes, h_dim, out_dim, num_rels, num_bases=-1, num_basis=-1,
num_hidden_layers=1, dropout=0, self_loop=False, skip_connect=False, encoder_name="", opn="sub", rel_emb=None, use_cuda=False, analysis=False, pe_init="rw", pe_dim=3):
super(BaseRGCN, self).__init__()
self.pe_init = pe_init
self.pe_dim = pe_dim
self.num_nodes = num_nodes
self.h_dim = h_dim
self.out_dim = out_dim
self.num_rels = num_rels
self.num_bases = num_bases
self.num_basis = num_basis
self.num_hidden_layers = num_hidden_layers
self.dropout = dropout
self.skip_connect = skip_connect
self.self_loop = self_loop
self.encoder_name = encoder_name
self.use_cuda = use_cuda
self.run_analysis = analysis
self.skip_connect = skip_connect
print("use layer :{}".format(encoder_name))
self.rel_emb = rel_emb
self.opn = opn
# create rgcn layers
self.build_model()
# create initial features
self.features = self.create_features()
def build_model(self):
self.layers = nn.ModuleList()
# i2h
i2h = self.build_input_layer()
if i2h is not None:
self.layers.append(i2h)
# h2h
for idx in range(self.num_hidden_layers):
h2h = self.build_hidden_layer(idx)
self.layers.append(h2h)
# h2o
h2o = self.build_output_layer()
if h2o is not None:
self.layers.append(h2o)
# initialize feature for each node
def create_features(self):
return None
def build_input_layer(self):
return None
def build_hidden_layer(self, idx):
raise NotImplementedError
def build_output_layer(self):
return None
def forward(self, g):
if self.features is not None:
g.ndata['id'] = self.features
print("h before GCN message passing")
print(g.ndata['h'])
print("h behind GCN message passing")
for layer in self.layers:
layer(g)
print(g.ndata['h'])
return g.ndata.pop('h')