-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathgraphsage.py
226 lines (168 loc) · 7.66 KB
/
graphsage.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
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
Author:
Weichen Shen,wcshen1994@163.com
Reference:
[1] Hamilton W, Ying Z, Leskovec J. Inductive representation learning on large graphs[C]//Advances in Neural Information Processing Systems. 2017: 1024-1034.(https://papers.nips.cc/paper/6703-inductive-representation-learning-on-large-graphs.pdf)
"""
import numpy as np
import tensorflow as tf
from tensorflow.python.keras.initializers import glorot_uniform, Zeros
from tensorflow.python.keras.layers import Input, Dense, Dropout, Layer
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.regularizers import l2
class MeanAggregator(Layer):
def __init__(self, units, input_dim, neigh_max, concat=True, dropout_rate=0.0, activation=tf.nn.relu, l2_reg=0,
use_bias=False,
seed=1024, **kwargs):
super(MeanAggregator, self).__init__()
self.units = units
self.neigh_max = neigh_max
self.concat = concat
self.dropout_rate = dropout_rate
self.l2_reg = l2_reg
self.use_bias = use_bias
self.activation = activation
self.seed = seed
self.input_dim = input_dim
def build(self, input_shapes):
self.neigh_weights = self.add_weight(shape=(self.input_dim, self.units),
initializer=glorot_uniform(
seed=self.seed),
regularizer=l2(self.l2_reg),
name="neigh_weights")
if self.use_bias:
self.bias = self.add_weight(shape=(self.units), initializer=Zeros(),
name='bias_weight')
self.dropout = Dropout(self.dropout_rate)
self.built = True
def call(self, inputs, training=None):
features, node, neighbours = inputs
node_feat = tf.nn.embedding_lookup(features, node)
neigh_feat = tf.nn.embedding_lookup(features, neighbours)
node_feat = self.dropout(node_feat, training=training)
neigh_feat = self.dropout(neigh_feat, training=training)
concat_feat = tf.concat([neigh_feat, node_feat], axis=1)
try:
concat_mean = tf.reduce_mean(concat_feat, axis=1, keep_dims=False)
except TypeError:
concat_mean = tf.reduce_mean(concat_feat, axis=1, keepdims=False)
output = tf.matmul(concat_mean, self.neigh_weights)
if self.use_bias:
output += self.bias
if self.activation:
output = self.activation(output)
# output = tf.nn.l2_normalize(output,dim=-1)
output._uses_learning_phase = True
return output
def get_config(self):
config = {'units': self.units,
'concat': self.concat,
'seed': self.seed
}
base_config = super(MeanAggregator, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
class PoolingAggregator(Layer):
def __init__(self, units, input_dim, neigh_max, aggregator='meanpooling', concat=True,
dropout_rate=0.0,
activation=tf.nn.relu, l2_reg=0, use_bias=False,
seed=1024, ):
super(PoolingAggregator, self).__init__()
self.output_dim = units
self.input_dim = input_dim
self.concat = concat
self.pooling = aggregator
self.dropout_rate = dropout_rate
self.l2_reg = l2_reg
self.use_bias = use_bias
self.activation = activation
self.neigh_max = neigh_max
self.seed = seed
# if neigh_input_dim is None:
def build(self, input_shapes):
self.dense_layers = [Dense(
self.input_dim, activation='relu', use_bias=True, kernel_regularizer=l2(self.l2_reg))]
self.neigh_weights = self.add_weight(
shape=(self.input_dim * 2, self.output_dim),
initializer=glorot_uniform(
seed=self.seed),
regularizer=l2(self.l2_reg),
name="neigh_weights")
if self.use_bias:
self.bias = self.add_weight(shape=(self.output_dim,),
initializer=Zeros(),
name='bias_weight')
self.built = True
def call(self, inputs, mask=None):
features, node, neighbours = inputs
node_feat = tf.nn.embedding_lookup(features, node)
neigh_feat = tf.nn.embedding_lookup(features, neighbours)
dims = tf.shape(neigh_feat)
batch_size = dims[0]
num_neighbors = dims[1]
h_reshaped = tf.reshape(
neigh_feat, (batch_size * num_neighbors, self.input_dim))
for l in self.dense_layers:
h_reshaped = l(h_reshaped)
neigh_feat = tf.reshape(
h_reshaped, (batch_size, num_neighbors, int(h_reshaped.shape[-1])))
if self.pooling == "meanpooling":
neigh_feat = tf.reduce_mean(neigh_feat, axis=1, keep_dims=False)
else:
neigh_feat = tf.reduce_max(neigh_feat, axis=1)
output = tf.concat(
[tf.squeeze(node_feat, axis=1), neigh_feat], axis=-1)
output = tf.matmul(output, self.neigh_weights)
if self.use_bias:
output += self.bias
if self.activation:
output = self.activation(output)
# output = tf.nn.l2_normalize(output, dim=-1)
return output
def get_config(self):
config = {'output_dim': self.output_dim,
'concat': self.concat
}
base_config = super(PoolingAggregator, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def GraphSAGE(feature_dim, neighbor_num, n_hidden, n_classes, use_bias=True, activation=tf.nn.relu,
aggregator_type='mean', dropout_rate=0.0, l2_reg=0):
features = Input(shape=(feature_dim,))
node_input = Input(shape=(1,), dtype=tf.int32)
neighbor_input = [Input(shape=(l,), dtype=tf.int32) for l in neighbor_num]
if aggregator_type == 'mean':
aggregator = MeanAggregator
else:
aggregator = PoolingAggregator
h = features
for i in range(0, len(neighbor_num)):
if i > 0:
feature_dim = n_hidden
if i == len(neighbor_num) - 1:
activation = tf.nn.softmax
n_hidden = n_classes
h = aggregator(units=n_hidden, input_dim=feature_dim, activation=activation, l2_reg=l2_reg, use_bias=use_bias,
dropout_rate=dropout_rate, neigh_max=neighbor_num[i], aggregator=aggregator_type)(
[h, node_input, neighbor_input[i]]) #
output = h
input_list = [features, node_input] + neighbor_input
model = Model(input_list, outputs=output)
return model
def sample_neighs(G, nodes, sample_num=None, self_loop=False, shuffle=True): # 抽样邻居节点
_sample = np.random.choice
neighs = [list(G[int(node)]) for node in nodes] # nodes里每个节点的邻居
if sample_num:
if self_loop:
sample_num -= 1
samp_neighs = [
list(_sample(neigh, sample_num, replace=False)) if len(neigh) >= sample_num else list(
_sample(neigh, sample_num, replace=True)) for neigh in neighs] # 采样邻居
if self_loop:
samp_neighs = [
samp_neigh + list([nodes[i]]) for i, samp_neigh in enumerate(samp_neighs)] # gcn邻居要加上自己
if shuffle:
samp_neighs = [list(np.random.permutation(x)) for x in samp_neighs]
else:
samp_neighs = neighs
return np.asarray(samp_neighs, dtype=np.float32), np.asarray(list(map(len, samp_neighs)))