-
Notifications
You must be signed in to change notification settings - Fork 12
/
model.py
executable file
·170 lines (144 loc) · 5.19 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
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
import torch
import torch.nn as nn
import torch.nn.functional as F
class GCN(nn.Module):
"""
Forked from GRAND-Lab/CoLA
"""
def __init__(self, in_ft, out_ft, act, bias=True):
super(GCN, self).__init__()
self.fc = nn.Linear(in_ft, out_ft, bias=False)
self.act = nn.PReLU() if act == 'prelu' else act
if bias:
self.bias = nn.Parameter(torch.FloatTensor(out_ft))
self.bias.data.fill_(0.0)
else:
self.register_parameter('bias', None)
for m in self.modules():
self.weights_init(m)
def weights_init(self, m):
if isinstance(m, nn.Linear):
torch.nn.init.xavier_uniform_(m.weight.data)
if m.bias is not None:
m.bias.data.fill_(0.0)
def forward(self, seq, adj, sparse=False):
seq_fts = self.fc(seq)
if sparse:
out = torch.unsqueeze(torch.spmm(adj, torch.squeeze(seq_fts, 0)), 0)
else:
out = torch.bmm(adj, seq_fts)
if self.bias is not None:
out += self.bias
return self.act(out)
class AvgReadout(nn.Module):
"""
Forked from GRAND-Lab/CoLA
"""
def __init__(self):
super(AvgReadout, self).__init__()
def forward(self, seq):
return torch.mean(seq, 1)
class MaxReadout(nn.Module):
"""
Forked from GRAND-Lab/CoLA
"""
def __init__(self):
super(MaxReadout, self).__init__()
def forward(self, seq):
return torch.max(seq,1).values
class MinReadout(nn.Module):
"""
Forked from GRAND-Lab/CoLA
"""
def __init__(self):
super(MinReadout, self).__init__()
def forward(self, seq):
return torch.min(seq, 1).values
class WSReadout(nn.Module):
"""
Forked from GRAND-Lab/CoLA
"""
def __init__(self):
super(WSReadout, self).__init__()
def forward(self, seq, query):
query = query.permute(0,2,1)
sim = torch.matmul(seq,query)
sim = F.softmax(sim,dim=1)
sim = sim.repeat(1, 1, 64)
out = torch.mul(seq,sim)
out = torch.sum(out,1)
return out
class Contextual_Discriminator(nn.Module):
def __init__(self, n_h, negsamp_round):
super(Contextual_Discriminator, self).__init__()
self.f_k = nn.Bilinear(n_h, n_h, 1)
for m in self.modules():
self.weights_init(m)
self.negsamp_round = negsamp_round
def weights_init(self, m):
if isinstance(m, nn.Bilinear):
torch.nn.init.xavier_uniform_(m.weight.data)
if m.bias is not None:
m.bias.data.fill_(0.0)
def forward(self, c, h_pl, s_bias1=None, s_bias2=None):
scs = []
scs.append(self.f_k(h_pl, c))
c_mi = c
for _ in range(self.negsamp_round):
c_mi = torch.cat((c_mi[-2:-1,:], c_mi[:-1,:]),0)
scs.append(self.f_k(h_pl, c_mi))
logits = torch.cat(tuple(scs))
return logits
class Patch_Discriminator(nn.Module):
def __init__(self, n_h, negsamp_round):
super(Patch_Discriminator, self).__init__()
self.f_k = nn.Bilinear(n_h, n_h, 1)
for m in self.modules():
self.weights_init(m)
self.negsamp_round = negsamp_round
def weights_init(self, m):
if isinstance(m, nn.Bilinear):
torch.nn.init.xavier_uniform_(m.weight.data)
if m.bias is not None:
m.bias.data.fill_(0.0)
def forward(self, h_ano, h_unano, s_bias1=None, s_bias2=None):
scs = []
scs.append(self.f_k(h_unano, h_ano))
h_mi = h_ano
for _ in range(self.negsamp_round):
h_mi = torch.cat((h_mi[-2:-1, :], h_mi[:-1, :]), 0)
scs.append(self.f_k(h_unano, h_mi))
logits = torch.cat(tuple(scs))
return logits
class Model(nn.Module):
def __init__(self, n_in, n_h, activation, negsamp_round_patch, negsamp_round_context, readout):
super(Model, self).__init__()
self.read_mode = readout
self.gcn_context = GCN(n_in, n_h, activation)
self.gcn_patch = GCN(n_in, n_h, activation)
if readout == 'max':
self.read = MaxReadout()
elif readout == 'min':
self.read = MinReadout()
elif readout == 'avg':
self.read = AvgReadout()
elif readout == 'weighted_sum':
self.read = WSReadout()
self.c_disc = Contextual_Discriminator(n_h, negsamp_round_context)
self.p_disc = Patch_Discriminator(n_h, negsamp_round_patch)
def forward(self, seq1, adj, sparse=False, msk=None, samp_bias1=None, samp_bias2=None):
h_1 = self.gcn_context(seq1, adj, sparse)
h_2 = self.gcn_patch(seq1, adj, sparse)
if self.read_mode != 'weighted_sum':
c = self.read(h_1[:, :-1, :])
h_mv = h_1[:, -1, :]
h_unano = h_2[:, -1, :]
h_ano = h_2[:, -2, :]
else:
c = self.read(h_1[:, :-1, :], h_1[:, -2:-1, :])
h_mv = h_1[:, -1, :]
h_unano = h_2[:, -1, :]
h_ano = h_2[:, -2, :]
ret1 = self.c_disc(c, h_mv, samp_bias1, samp_bias2)
ret2 = self.p_disc(h_ano, h_unano, samp_bias1, samp_bias2)
return ret1, ret2