This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 163
/
models.py
193 lines (160 loc) · 6.84 KB
/
models.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
import torch
import utils as u
from argparse import Namespace
from torch.nn.parameter import Parameter
from torch.nn import functional as F
import torch.nn as nn
import math
class Sp_GCN(torch.nn.Module):
def __init__(self,args,activation):
super().__init__()
self.activation = activation
self.num_layers = args.num_layers
self.w_list = nn.ParameterList()
for i in range(self.num_layers):
if i==0:
w_i = Parameter(torch.Tensor(args.feats_per_node, args.layer_1_feats))
u.reset_param(w_i)
else:
w_i = Parameter(torch.Tensor(args.layer_1_feats, args.layer_2_feats))
u.reset_param(w_i)
self.w_list.append(w_i)
def forward(self,A_list, Nodes_list, nodes_mask_list):
node_feats = Nodes_list[-1]
#A_list: T, each element sparse tensor
#take only last adj matrix in time
Ahat = A_list[-1]
#Ahat: NxN ~ 30k
#sparse multiplication
# Ahat NxN
# self.node_embs = Nxk
#
# note(bwheatman, tfk): change order of matrix multiply
last_l = self.activation(Ahat.matmul(node_feats.matmul(self.w_list[0])))
for i in range(1, self.num_layers):
last_l = self.activation(Ahat.matmul(last_l.matmul(self.w_list[i])))
return last_l
class Sp_Skip_GCN(Sp_GCN):
def __init__(self,args,activation):
super().__init__(args,activation)
self.W_feat = Parameter(torch.Tensor(args.feats_per_node, args.layer_1_feats))
def forward(self,A_list, Nodes_list = None):
node_feats = Nodes_list[-1]
#A_list: T, each element sparse tensor
#take only last adj matrix in time
Ahat = A_list[-1]
#Ahat: NxN ~ 30k
#sparse multiplication
# Ahat NxN
# self.node_feats = Nxk
#
# note(bwheatman, tfk): change order of matrix multiply
l1 = self.activation(Ahat.matmul(node_feats.matmul(self.W1)))
l2 = self.activation(Ahat.matmul(l1.matmul(self.W2)) + (node_feats.matmul(self.W3)))
return l2
class Sp_Skip_NodeFeats_GCN(Sp_GCN):
def __init__(self,args,activation):
super().__init__(args,activation)
def forward(self,A_list, Nodes_list = None):
node_feats = Nodes_list[-1]
Ahat = A_list[-1]
last_l = self.activation(Ahat.matmul(node_feats.matmul(self.w_list[0])))
for i in range(1, self.num_layers):
last_l = self.activation(Ahat.matmul(last_l.matmul(self.w_list[i])))
skip_last_l = torch.cat((last_l,node_feats), dim=1) # use node_feats.to_dense() if 2hot encoded input
return skip_last_l
class Sp_GCN_LSTM_A(Sp_GCN):
def __init__(self,args,activation):
super().__init__(args,activation)
self.rnn = nn.LSTM(
input_size=args.layer_2_feats,
hidden_size=args.lstm_l2_feats,
num_layers=args.lstm_l2_layers
)
def forward(self,A_list, Nodes_list = None, nodes_mask_list = None):
last_l_seq=[]
for t,Ahat in enumerate(A_list):
node_feats = Nodes_list[t]
#A_list: T, each element sparse tensor
#note(bwheatman, tfk): change order of matrix multiply
last_l = self.activation(Ahat.matmul(node_feats.matmul(self.w_list[0])))
for i in range(1, self.num_layers):
last_l = self.activation(Ahat.matmul(last_l.matmul(self.w_list[i])))
last_l_seq.append(last_l)
last_l_seq = torch.stack(last_l_seq)
out, _ = self.rnn(last_l_seq, None)
return out[-1]
class Sp_GCN_GRU_A(Sp_GCN_LSTM_A):
def __init__(self,args,activation):
super().__init__(args,activation)
self.rnn = nn.GRU(
input_size=args.layer_2_feats,
hidden_size=args.lstm_l2_feats,
num_layers=args.lstm_l2_layers
)
class Sp_GCN_LSTM_B(Sp_GCN):
def __init__(self,args,activation):
super().__init__(args,activation)
assert args.num_layers == 2, 'GCN-LSTM and GCN-GRU requires 2 conv layers.'
self.rnn_l1 = nn.LSTM(
input_size=args.layer_1_feats,
hidden_size=args.lstm_l1_feats,
num_layers=args.lstm_l1_layers
)
self.rnn_l2 = nn.LSTM(
input_size=args.layer_2_feats,
hidden_size=args.lstm_l2_feats,
num_layers=args.lstm_l2_layers
)
self.W2 = Parameter(torch.Tensor(args.lstm_l1_feats, args.layer_2_feats))
u.reset_param(self.W2)
def forward(self,A_list, Nodes_list = None, nodes_mask_list = None):
l1_seq=[]
l2_seq=[]
for t,Ahat in enumerate(A_list):
node_feats = Nodes_list[t]
l1 = self.activation(Ahat.matmul(node_feats.matmul(self.w_list[0])))
l1_seq.append(l1)
l1_seq = torch.stack(l1_seq)
out_l1, _ = self.rnn_l1(l1_seq, None)
for i in range(len(A_list)):
Ahat = A_list[i]
out_t_l1 = out_l1[i]
#A_list: T, each element sparse tensor
l2 = self.activation(Ahat.matmul(out_t_l1).matmul(self.w_list[1]))
l2_seq.append(l2)
l2_seq = torch.stack(l2_seq)
out, _ = self.rnn_l2(l2_seq, None)
return out[-1]
class Sp_GCN_GRU_B(Sp_GCN_LSTM_B):
def __init__(self,args,activation):
super().__init__(args,activation)
self.rnn_l1 = nn.GRU(
input_size=args.layer_1_feats,
hidden_size=args.lstm_l1_feats,
num_layers=args.lstm_l1_layers
)
self.rnn_l2 = nn.GRU(
input_size=args.layer_2_feats,
hidden_size=args.lstm_l2_feats,
num_layers=args.lstm_l2_layers
)
class Classifier(torch.nn.Module):
def __init__(self,args,out_features=2, in_features = None):
super(Classifier,self).__init__()
activation = torch.nn.ReLU()
if in_features is not None:
num_feats = in_features
elif args.experiment_type in ['sp_lstm_A_trainer', 'sp_lstm_B_trainer',
'sp_weighted_lstm_A', 'sp_weighted_lstm_B'] :
num_feats = args.gcn_parameters['lstm_l2_feats'] * 2
else:
num_feats = args.gcn_parameters['layer_2_feats'] * 2
print ('CLS num_feats',num_feats)
self.mlp = torch.nn.Sequential(torch.nn.Linear(in_features = num_feats,
out_features =args.gcn_parameters['cls_feats']),
activation,
torch.nn.Linear(in_features = args.gcn_parameters['cls_feats'],
out_features = out_features))
def forward(self,x):
return self.mlp(x)