-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_tam.py
291 lines (213 loc) · 8.18 KB
/
model_tam.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# -*- coding: utf-8 -*-
# @Author : qiaohezhe
# @github : https://github.com/fengduqianhe
# @Date : 9/25/2023
# version: Python 3.7.8
# @File : model.py
# @Software: PyCharm
import torch.nn as nn
import torch.nn.functional as F
from utils_tam import *
from torch.nn.modules.module import Module
class GCN(nn.Module):
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, 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):
def __init__(self):
super(AvgReadout, self).__init__()
def forward(self, seq):
return torch.mean(seq, 1)
class MaxReadout(nn.Module):
def __init__(self):
super(MaxReadout, self).__init__()
def forward(self, seq):
return torch.max(seq, 1).values
class MinReadout(nn.Module):
def __init__(self):
super(MinReadout, self).__init__()
def forward(self, seq):
return torch.min(seq, 1).values
class WSReadout(nn.Module):
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 Discriminator(nn.Module):
def __init__(self, n_h, negsamp_round):
super(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):
scs = []
# positive
scs.append(self.f_k(h_pl, c))
# negative
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
def get_cos(feature):
feature = feature / (torch.norm(feature, dim=-1, keepdim=True))
sim_matrix = torch.mm(feature, feature.T)
return sim_matrix
def min_max_norm(feature):
feature = (feature - feature.min()) / (feature.max() - feature.min())
return feature
class Model(nn.Module):
def __init__(self, n_in, n_h, activation, negsamp_round, readout):
super(Model, self).__init__()
self.read_mode = readout
self.gcn1 = GCN(n_in, 2 * n_h, activation)
self.gcn2 = GCN(2 * n_h, n_h, activation)
self.act = nn.PReLU()
self.fc1 = nn.Linear(n_h, 2 * n_h, bias=False)
self.fc2 = nn.Linear(n_h, 2 * n_h, bias=False)
self.ReLU = nn.ReLU()
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()
def forward(self, seq, adj, sparse=False):
feat = self.gcn1(seq, adj)
feat = self.gcn2(feat, adj)
feat1 = self.fc1(feat)
feat2 = self.fc2(feat)
return feat, feat1, feat2
# Graphsage layer
class SageConv(Module):
"""
Simple Graphsage layer
"""
def __init__(self, in_features, out_features, bias=False):
super(SageConv, self).__init__()
self.proj = nn.Linear(in_features * 2, out_features, bias=bias)
self.reset_parameters()
# print("note: for dense graph in graphsage, require it normalized.")
def reset_parameters(self):
nn.init.normal_(self.proj.weight)
if self.proj.bias is not None:
nn.init.constant_(self.proj.bias, 0.)
def forward(self, features, adj):
"""
Args:
adj: can be sparse or dense matrix.
"""
# fuse info from neighbors. to be added:
if adj.layout != torch.sparse_coo:
if len(adj.shape) == 3:
neigh_feature = torch.bmm(adj, features) / (
adj.sum(dim=1).reshape((adj.shape[0], adj.shape[1], -1)) + 1)
else:
neigh_feature = torch.mm(adj, features) / (adj.sum(dim=1).reshape(adj.shape[0], -1) + 1)
else:
# print("spmm not implemented for batch training. Note!")
neigh_feature = torch.spmm(adj, features) / (adj.to_dense().sum(dim=1).reshape(adj.shape[0], -1) + 1)
# perform conv
data = torch.cat([features, neigh_feature], dim=-1)
combined = self.proj(data)
return combined
class Sage_En(nn.Module):
def __init__(self, nfeat, nhid, nembed, dropout):
super(Sage_En, self).__init__()
self.sage1 = SageConv(nfeat, nembed)
self.dropout = dropout
def forward(self, x, adj):
x = F.relu(self.sage1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
return x
class Sage_En2(nn.Module):
def __init__(self, nfeat, nhid, nembed, dropout):
super(Sage_En2, self).__init__()
self.sage1 = SageConv(nfeat, nhid)
self.sage2 = SageConv(nhid, nembed)
self.dropout = dropout
def forward(self, x, adj):
x = F.relu(self.sage1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = F.relu(self.sage2(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
return x
class Sage_Classifier(nn.Module):
def __init__(self, nembed, nhid, nclass, dropout):
super(Sage_Classifier, self).__init__()
self.sage1 = SageConv(nembed, nhid)
self.mlp = nn.Linear(nhid, nclass)
self.dropout = dropout
self.reset_parameters()
def reset_parameters(self):
nn.init.normal_(self.mlp.weight, std=0.05)
def forward(self, x, adj):
x = F.relu(self.sage1(x, adj))
x = F.dropout(x, self.dropout, training=self.training)
x = self.mlp(x)
return x
def neighList_to_edgeList(adj):
edge_list = []
for i in range(adj.shape[0]):
for j in torch.argwhere(adj[i, :] > 0):
edge_list.append((int(i), int(j)))
return edge_list
from torch_geometric.nn import GINConv
class GIN(torch.nn.Module):
def __init__(self, ft_size, hidden_dim, num_layers):
super(GIN, self).__init__()
self.conv1 = GINConv(nn.Sequential(nn.Linear(ft_size, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim)))
self.convs = nn.ModuleList()
for _ in range(num_layers - 1):
self.convs.append(GINConv(nn.Sequential(nn.Linear(hidden_dim, hidden_dim),
nn.ReLU(),
nn.Linear(hidden_dim, hidden_dim))))
def forward(self, feat, adj):
# x, edge_index, batch = data.x, data.edge_index, data.batch
adj = torch.squeeze(adj)
feat = torch.squeeze(feat)
edge_index = neighList_to_edgeList(adj)
edge_index = torch.tensor(np.array(edge_index)).T.cuda()
x = F.relu(self.conv1(feat, edge_index))
for conv in self.convs:
x = F.relu(conv(x, edge_index))
return torch.unsqueeze(x, 0)