-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.py
300 lines (221 loc) · 11 KB
/
layer.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
292
293
294
295
296
297
298
from __future__ import absolute_import
from keras import activations, constraints, initializers, regularizers
from keras import backend as K
from keras.layers import Layer, Dropout, LeakyReLU
import tensorflow as tf
import numpy as np
from keras.layers import *
import numpy
from keras.layers.merge import concatenate, add, maximum, subtract, multiply, dot
class RAAttention(Layer):
def __init__(self,
node_size,
rel_size,
triple_size,
depth = 1,
use_w = False,
attn_heads=1,
attn_heads_reduction='concat',
activation=None,
use_bias=False,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
attn_kernel_initializer='glorot_uniform',
kernel_regularizer=None,
bias_regularizer=None,
attn_kernel_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
attn_kernel_constraint=None,
**kwargs):
self.node_size = node_size
self.rel_size = rel_size
self.triple_size = triple_size
self.attn_heads = attn_heads
self.attn_heads_reduction = attn_heads_reduction
self.activation = activations.get(activation)
self.use_bias = use_bias
self.use_w = use_w
self.depth = depth
self.kernel_initializer = initializers.get(kernel_initializer)
self.bias_initializer = initializers.get(bias_initializer)
self.attn_kernel_initializer = initializers.get(attn_kernel_initializer)
self.kernel_regularizer = regularizers.get(kernel_regularizer)
self.bias_regularizer = regularizers.get(bias_regularizer)
self.attn_kernel_regularizer = regularizers.get(attn_kernel_regularizer)
self.activity_regularizer = regularizers.get(activity_regularizer)
self.kernel_constraint = constraints.get(kernel_constraint)
self.bias_constraint = constraints.get(bias_constraint)
self.attn_kernel_constraint = constraints.get(attn_kernel_constraint)
self.supports_masking = False
self.biases = []
self.attn_kernels = []
self.gat_kernels = []
self.gate_kernels = []
super(RAAttention, self).__init__(**kwargs)
def build(self, input_shape):
assert len(input_shape) >= 2
if self.use_w:
node_F = input_shape[0][-1]//2 #########3
self.node_F = node_F
else:
node_F = input_shape[0][-1]
self.node_F = node_F
rel_F = input_shape[1][-1]
self.ent_F = node_F
ent_F = self.ent_F
if self.use_w:
self.gcn_kernel =self.add_weight(shape=(ent_F*2,ent_F),
initializer=self.bias_initializer,
regularizer=self.bias_regularizer,
constraint=self.bias_constraint,
name='use_w')
for l in range(self.depth):
self.attn_kernels.append([])
for head in range(self.attn_heads):
attn_kernel = self.add_weight(shape=(1*node_F ,1),
initializer=self.attn_kernel_initializer,
regularizer=self.attn_kernel_regularizer,
constraint=self.attn_kernel_constraint,
name='attn_kernel_self_{}'.format(head))
self.attn_kernels[l].append(attn_kernel)
self.built = True
def call(self, inputs):
outputs = []
features = inputs[0]
rel_emb = inputs[1]
adj = tf.SparseTensor(K.cast(K.squeeze(inputs[2],axis = 0),dtype = "int64"),
K.ones_like(inputs[2][0,:,0]),(self.node_size,self.node_size))
sparse_indices = tf.squeeze(inputs[3],axis = 0)
sparse_val = tf.squeeze(inputs[4],axis = 0)
if self.use_w:
features = K.dot(features,self.gcn_kernel)
features = self.activation(features)
outputs.append(features)
for l in range(self.depth):
features_list = []
for head in range(self.attn_heads):
attention_kernel = self.attn_kernels[l][head]
rels_sum = tf.SparseTensor(indices=sparse_indices,values=sparse_val,dense_shape=(self.triple_size,self.rel_size))
rels_sum = tf.sparse_tensor_dense_matmul(rels_sum,rel_emb)
neighs = K.gather(features,adj.indices[:,1])
selfs = K.gather(features,adj.indices[:,0])
rels_sum = tf.nn.l2_normalize(rels_sum, 1)
neighs = neighs - 2 * tf.reduce_sum(neighs * rels_sum, 1, keepdims=True) * rels_sum
att = K.squeeze(K.dot(rels_sum,attention_kernel),axis = -1) #K.concatenate([selfs,neighs,rels_sum])
att = tf.SparseTensor(indices=adj.indices, values=att, dense_shape=adj.dense_shape)
att = tf.sparse_softmax(att)
new_features = tf.segment_sum (neighs*K.expand_dims(att.values,axis = -1),adj.indices[:,0])
features_list.append(new_features)
if self.attn_heads_reduction == 'concat':
features = K.concatenate(features_list) # (N x KF')
else:
features = K.mean(K.stack(features_list), axis=0)
features = self.activation(features)
outputs.append(features)
outputs = K.concatenate(outputs)
return outputs
class POSAttention(Layer):
def __init__(self,
node_size,
rel_size,
triple_size,
depth = 1,
use_w = False,
attn_heads=1,
attn_heads_reduction='concat',
activation=None,
use_bias=False,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
attn_kernel_initializer='glorot_uniform',
kernel_regularizer=None,
bias_regularizer=None,
attn_kernel_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
attn_kernel_constraint=None,
**kwargs):
self.node_size = node_size
self.rel_size = rel_size
self.triple_size = triple_size
self.attn_heads = attn_heads
self.attn_heads_reduction = attn_heads_reduction
self.activation = activations.get(activation)
self.use_bias = use_bias
self.use_w = use_w
self.depth = depth
self.kernel_initializer = initializers.get(kernel_initializer)
self.bias_initializer = initializers.get(bias_initializer)
self.attn_kernel_initializer = initializers.get(attn_kernel_initializer)
self.kernel_regularizer = regularizers.get(kernel_regularizer)
self.bias_regularizer = regularizers.get(bias_regularizer)
self.attn_kernel_regularizer = regularizers.get(attn_kernel_regularizer)
self.activity_regularizer = regularizers.get(activity_regularizer)
self.kernel_constraint = constraints.get(kernel_constraint)
self.bias_constraint = constraints.get(bias_constraint)
self.attn_kernel_constraint = constraints.get(attn_kernel_constraint)
self.supports_masking = False
self.biases = []
self.attn_kernels = []
self.gat_kernels = []
self.gate_kernels = []
super(POSAttention, self).__init__(**kwargs)
def build(self, input_shape):
assert len(input_shape) >= 2
if self.use_w:
node_F = input_shape[0][-1]//2 #########3
self.node_F = node_F
else:
node_F = input_shape[0][-1]
self.node_F = node_F
rel_F = input_shape[1][-1]
self.ent_F = node_F
ent_F = self.ent_F
if self.use_w:
self.gcn_kernel =self.add_weight(shape=(ent_F*2,ent_F),
initializer=self.bias_initializer,
regularizer=self.bias_regularizer,
constraint=self.bias_constraint,
name='use_w')
for l in range(self.depth):
self.attn_kernels.append([])
for head in range(self.attn_heads):
attn_kernel = self.add_weight(shape=(2*node_F ,1),
initializer=self.attn_kernel_initializer,
regularizer=self.attn_kernel_regularizer,
constraint=self.attn_kernel_constraint,
name='attn_kernel_self_{}'.format(head))
self.attn_kernels[l].append(attn_kernel)
self.built = True
def call(self, inputs):
outputs = []
features = inputs[0]
rel_emb = inputs[1]
adj = tf.SparseTensor(K.cast(K.squeeze(inputs[2],axis = 0),dtype = "int64"),
K.ones_like(inputs[2][0,:,0]),(self.node_size,self.node_size))
sparse_indices = tf.squeeze(inputs[3],axis = 0)
sparse_val = tf.squeeze(inputs[4],axis = 0)
features = self.activation(features)
outputs.append(features)
for l in range(self.depth):
features_list = []
for head in range(self.attn_heads):
attention_kernel = self.attn_kernels[l][head]
neighs = K.gather(features,adj.indices[:,1])
selfs = K.gather(features,adj.indices[:,0])
att = K.squeeze(K.dot(K.concatenate([selfs,neighs]),attention_kernel),axis = -1)
att = tf.SparseTensor(indices=adj.indices, values=att, dense_shape=adj.dense_shape)
att = tf.sparse_softmax(att)
new_features = tf.segment_sum (neighs*K.expand_dims(att.values,axis = -1),adj.indices[:,0])
features_list.append(new_features)
if self.attn_heads_reduction == 'concat':
features = K.concatenate(features_list) # (N x KF')
else:
features = K.mean(K.stack(features_list), axis=0)
features = self.activation(features)
outputs.append(features)
outputs = K.concatenate(outputs)
return outputs