forked from Niuchx/HimNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCN_embedding.py
221 lines (189 loc) · 8.64 KB
/
GCN_embedding.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
import torch
import torch.nn as nn
from torch.nn import init
import torch.nn.functional as F
import numpy as np
from torch.nn.parameter import Parameter
import math
class GraphConv(nn.Module):
def __init__(self, input_dim, output_dim, add_self=False, normalize_embedding=False, dropout=0.0, bias=True):
super(GraphConv, self).__init__()
self.add_self = add_self
self.dropout = dropout
if dropout > 0.001:
self.dropout_layer = nn.Dropout(p=dropout)
self.normalize_embedding = normalize_embedding
self.input_dim = input_dim
self.output_dim = output_dim
self.weight = nn.Parameter(torch.FloatTensor(input_dim, output_dim).cuda())
if bias:
self.bias = nn.Parameter(torch.FloatTensor(output_dim).cuda())
else:
self.bias = None
def forward(self, x, adj):
if self.dropout > 0.001:
x = self.dropout_layer(x)
y = torch.matmul(adj, x)
if self.add_self:
y += x
y = torch.matmul(y,self.weight)
if self.bias is not None:
y = y + self.bias
if self.normalize_embedding:
y = F.normalize(y, p=2, dim=2)
return y
class GraphEncoder(nn.Module):
def __init__(self, input_dim, hidden_dim, embedding_dim, num_layers,
pred_hidden_dims=[], concat=False, bn=True, dropout=0.0, args=None):
super(GraphEncoder, self).__init__()
self.concat = concat
add_self = not concat
self.bn = bn
self.num_layers = num_layers
self.num_aggs=1
self.bias = True
if args is not None:
self.bias = args.bias
self.conv_first, self.conv_block, self.conv_last = self.build_conv_layers(input_dim, hidden_dim, embedding_dim, num_layers, add_self, normalize=True, dropout=dropout)
self.act = nn.ReLU()
if concat:
self.pred_input_dim = hidden_dim * (num_layers - 1) + embedding_dim
else:
self.pred_input_dim = embedding_dim
for m in self.modules():
if isinstance(m, GraphConv):
m.weight.data = init.kaiming_uniform_(m.weight.data, mode='fan_in', nonlinearity='relu')
if m.bias is not None:
m.bias.data = init.constant(m.bias.data, 0.0)
def build_conv_layers(self, input_dim, hidden_dim, embedding_dim, num_layers, add_self, normalize=False, dropout=0.0):
conv_first = GraphConv(input_dim=input_dim, output_dim=hidden_dim, add_self=add_self, normalize_embedding=normalize, bias=self.bias)
conv_block = nn.ModuleList([GraphConv(input_dim=hidden_dim, output_dim=hidden_dim, add_self=add_self, normalize_embedding=normalize, dropout=dropout, bias=self.bias) for i in range(num_layers-2)])
conv_last = GraphConv(input_dim=hidden_dim, output_dim=embedding_dim, add_self=add_self, normalize_embedding=normalize, bias=self.bias)
return conv_first, conv_block, conv_last
def apply_bn(self, x):
''' Batch normalization of 3D tensor x
'''
bn_module = nn.BatchNorm1d(x.size()[1]).cuda()
return bn_module(x)
def gcn_forward(self, x, adj, conv_first, conv_block, conv_last, embedding_mask=None):
''' Perform forward prop with graph convolution.
Returns:
Embedding matrix with dimension [batch_size x num_nodes x embedding]
'''
x = conv_first(x, adj)
x = self.act(x)#relu
if self.bn:
x = self.apply_bn(x)
x_all = [x]
for i in range(len(conv_block)):
x = conv_block[i](x,adj)
x = self.act(x)
if self.bn:
x = self.apply_bn(x)
x_all.append(x)
x = conv_last(x,adj)
x_all.append(x)
x_tensor = torch.cat(x_all, dim=2)
if embedding_mask is not None:
x_tensor = x_tensor * embedding_mask
return x_tensor
def forward(self, x, adj, batch_num_nodes=None, **kwargs):
# conv
x = self.conv_first(x, adj)
x = self.act(x)
if self.bn:
x = self.apply_bn(x)
out_all = []
out = torch.mean(x, dim=1)
out_all.append(out)
for i in range(self.num_layers-2):
x = self.conv_block[i](x,adj)
x = self.act(x)
if self.bn:
x = self.apply_bn(x)
out = torch.mean(x, dim=1)
out_all.append(out)
if self.num_aggs == 2:
out = torch.sum(x, dim=1)
out_all.append(out)
x = self.conv_last(x,adj)
out = torch.mean(x, dim=1)
out_all.append(out)
if self.num_aggs == 2:
out = torch.sum(x, dim=1)
out_all.append(out)
if self.concat:
output = torch.cat(out_all, dim=1)
else:
output = out
return x, output
class attr_Decoder(nn.Module):
def __init__(self, feat_size,hiddendim,outputdim,dropout):
super(attr_Decoder, self).__init__()
self.gc1 = nn.Linear(outputdim, hiddendim, bias=False)
self.gc2 = nn.Linear(hiddendim, feat_size, bias=False)
self.leaky_relu = nn.LeakyReLU()
self.dropout = nn.Dropout(dropout)
def forward(self, x, adj):
x = self.leaky_relu(self.gc1(torch.matmul(adj, x)))
x = self.gc2(torch.matmul(adj, x))
return x
class stru_Decoder(nn.Module):
def __init__(self, embedding_dim):
super(stru_Decoder, self).__init__()
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x1=x.permute(0, 2, 1)
x = torch.matmul(x,x1)
x = nn.ReLU()(x)
return x
class MemModule(nn.Module):
def __init__(self, mem_num_node, mem_num_graph, node_num, feat_num):
super(MemModule, self).__init__()
self.weight_node = Parameter(torch.Tensor(mem_num_node, node_num, feat_num))
self.weight_graph = Parameter(torch.Tensor(mem_num_graph, feat_num))
self.bias = None
self.shrink_thres = 0.2 / mem_num_node
self.shrink_thres_graph = 0.2 / mem_num_graph
self.reset_parameters_node()
self.reset_parameters_graph()
def reset_parameters_node(self):
stdv = 1. / math.sqrt(self.weight_node.size(1))
self.weight_node.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def reset_parameters_graph(self):
stdv = 1. / math.sqrt(self.weight_graph.size(1))
self.weight_graph.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, node_embed, graph_embed):
att_weight_node = torch.einsum('bnd,mnd->bm', [node_embed, self.weight_node])
att_weight_node = F.softmax(att_weight_node, dim=1)
if(self.shrink_thres>0):
att_weight_node = hard_shrink_relu(att_weight_node, lambd=self.shrink_thres)
att_weight_node = F.normalize(att_weight_node, p=1, dim=1)
output_node = torch.einsum('bm,mnd->bnd', [att_weight_node, self.weight_node])
att_weight_graph = torch.einsum('bd,md->bm', [graph_embed, self.weight_graph])
att_weight_graph = F.softmax(att_weight_graph, dim=1)
if(self.shrink_thres_graph>0):
att_weight_graph = hard_shrink_relu(att_weight_graph, lambd=self.shrink_thres_graph)
att_weight_graph = F.normalize(att_weight_graph, p=1, dim=1)
output_graph = torch.einsum('bm,md->bd', [att_weight_graph, self.weight_graph])
return output_node, output_graph, att_weight_node, att_weight_graph
def hard_shrink_relu(input, lambd=0, epsilon=1e-12):
output = (F.relu(input-lambd) * input) / (torch.abs(input - lambd) + epsilon)
return output
class GNNet(nn.Module):
def __init__(self, input_dim, hidden_dim, embedding_dim, num_layers, mem_num_node, mem_num_graph, node_num, dropout=0.1, args=None):
super(GNNet, self).__init__()
self.encoder = GraphEncoder(input_dim, hidden_dim, embedding_dim, num_layers, bn=args.bn, args=args)
self.memory = MemModule(mem_num_node, mem_num_graph, node_num, embedding_dim)
self.feat_dec = attr_Decoder(input_dim, hidden_dim, embedding_dim, dropout)
self.adj_dec = stru_Decoder(embedding_dim)
def forward(self, x, adj):
node_embed, graph_embed = self.encoder(x, adj)
output_node, output_graph, att_weight_node, att_weight_graph = self.memory(node_embed, graph_embed)
recon_node = self.feat_dec(output_node, adj)
recon_adj = self.adj_dec(output_node)
return recon_node, recon_adj, att_weight_node, att_weight_graph, graph_embed, output_graph