-
Notifications
You must be signed in to change notification settings - Fork 13
/
aggregators.py
169 lines (126 loc) · 5.95 KB
/
aggregators.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
import tensorflow as tf
from .utils import *
class MultiAttentionAggregator(Layer):
def __init__(self, input_dim, output_dim, neigh_input_dim=None,
dropout=0., bias=False, act=tf.nn.relu, name=None, concat=True, num_heads=1, sample_num=1, **kwargs):
super(MultiAttentionAggregator, self).__init__(**kwargs)
self.dropout = dropout
self.bias = bias
self.act = act
self.concat = concat
self.num_heads = num_heads
if neigh_input_dim is None:
neigh_input_dim = input_dim
if name is not None:
name = '/' + name
else:
name = ''
with tf.variable_scope(self.name + name + '_vars'):
self.vars['weights'] = glorot([neigh_input_dim, output_dim],
name='neigh_weights')
self.vars['neigh_weights'] = glorot([neigh_input_dim, output_dim],
name='neigh_weights')
self.vars['inter_weights'] = glorot([neigh_input_dim, output_dim],
name='inter_weights')
self.vars['inter_weights_mul'] = glorot([neigh_input_dim, output_dim],
name='inter_weights_mul')
self.vars['self_weights'] = glorot([input_dim, output_dim],
name='self_weights')
self.vars['output_weights'] = glorot([5 * output_dim, output_dim],
name='output_weights')
if self.bias:
self.vars['bias'] = zeros([self.output_dim], name='neigh_bias')
if self.logging:
self._log_vars()
self.input_dim = input_dim
self.output_dim = output_dim
self.sample_num = sample_num
def _call(self, inputs):
self_vecs, neigh_vecs = inputs
neigh_vecs = tf.nn.dropout(neigh_vecs, 1-self.dropout)
self_vecs = tf.nn.dropout(self_vecs, 1-self.dropout)
from_self = tf.matmul(self_vecs, self.vars["self_weights"])
#line part
neigh_mean = tf.reduce_mean(neigh_vecs, axis=1)
neigh_line = tf.matmul(neigh_mean, self.vars['neigh_weights'])
#interaction_part
pair_interactions = 0.5 *tf.subtract(
tf.pow(
tf.matmul(neigh_mean,self.vars['inter_weights']),2),
tf.matmul(tf.pow(neigh_mean,2),tf.pow(self.vars['inter_weights'],2)))
# Reshape from [batch_size, depth] to [batch_size, 1, depth] for matmul.
query = tf.expand_dims(self_vecs, 1)
neigh_self_vecs = tf.concat([neigh_vecs, query], axis=1)
#attention multi
neigh_self_vecs = split_heads(neigh_self_vecs, self.num_heads)
query = split_heads(query, self.num_heads)
logits = tf.matmul(query, neigh_self_vecs, transpose_b=True)
score = tf.nn.softmax(logits, name="attention_weights")
score = tf.nn.dropout(score, 1-self.dropout)
#[batch_size,feature_size,node_nums,dims_fea]
context = tf.matmul(score, neigh_self_vecs)
context = combine_heads(context)
context = tf.squeeze(context, [1])
#interaction mul part
pair_interactions_mul = 0.5 *tf.subtract(
tf.pow(
tf.matmul(context,self.vars['inter_weights_mul']),2),
tf.matmul(tf.pow(context,2),tf.pow(self.vars['inter_weights_mul'],2)))
# [nodes] x [out_dim]
from_neighs = tf.matmul(context, self.vars['weights'])
if self.concat:
# fully project
output = tf.concat([from_self, from_neighs, neigh_line, pair_interactions, pair_interactions_mul], axis=1)
output = tf.matmul(output, self.vars['output_weights'])
else:
# average project
output = tf.add_n([from_self, from_neighs, neigh_line, pair_interactions, pair_interactions_mul])
# bias
if self.bias:
output += self.vars['bias']
return self.act(output)
class MeanAggregator(Layer):
"""
Aggregates via mean followed by matmul and non-linearity.
"""
def __init__(self, input_dim, output_dim, neigh_input_dim=None,
dropout=0., bias=False, act=tf.nn.relu,
name=None, concat=False, **kwargs):
super(MeanAggregator, self).__init__(**kwargs)
self.dropout = dropout
self.bias = bias
self.act = act
self.concat = concat
if neigh_input_dim is None:
neigh_input_dim = input_dim
if name is not None:
name = '/' + name
else:
name = ''
with tf.variable_scope(self.name + name + '_vars'):
self.vars['neigh_weights'] = glorot([neigh_input_dim, output_dim],
name='neigh_weights')
self.vars['self_weights'] = glorot([input_dim, output_dim],
name='self_weights')
if self.bias:
self.vars['bias'] = zeros([self.output_dim], name='bias')
if self.logging:
self._log_vars()
self.input_dim = input_dim
self.output_dim = output_dim
def _call(self, inputs):
self_vecs, neigh_vecs = inputs
neigh_vecs = tf.nn.dropout(neigh_vecs, 1-self.dropout)
self_vecs = tf.nn.dropout(self_vecs, 1-self.dropout)
neigh_means = tf.reduce_mean(neigh_vecs, axis=1)
# [nodes] x [out_dim]
from_neighs = tf.matmul(neigh_means, self.vars['neigh_weights'])
from_self = tf.matmul(self_vecs, self.vars["self_weights"])
if not self.concat:
output = tf.add_n([from_self, from_neighs])
else:
output = tf.concat([from_self, from_neighs], axis=1)
# bias
if self.bias:
output += self.vars['bias']
return self.act(output)