-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer.py
475 lines (388 loc) · 14.3 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import numpy as np
import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras import layers
from tensorflow.keras import backend as K
from os.path import join
import abc
import time
from tqdm import tqdm
from tensorflow.keras import layers
from tensorflow.keras import backend as K
from sklearn.metrics import (
roc_auc_score,
log_loss,
mean_squared_error,
accuracy_score,
f1_score,
)
# from models.deeprec_utils import cal_metric
# from models.layers import AttLayer2, SelfAttention
# __all__ = ["NRMSModel"]
class ComputeMasking(layers.Layer):
"""Compute if inputs contains zero value.
Returns:
bool tensor: True for values not equal to zero.
"""
def __init__(self, **kwargs):
super(ComputeMasking, self).__init__(**kwargs)
def call(self, inputs, **kwargs):
mask = K.not_equal(inputs, 0)
return K.cast(mask, K.floatx())
def compute_output_shape(self, input_shape):
return input_shape
class OverwriteMasking(layers.Layer):
"""Set values at spasific positions to zero.
Args:
inputs (list): value tensor and mask tensor.
Returns:
obj: tensor after setting values to zero.
"""
def __init__(self, **kwargs):
super(OverwriteMasking, self).__init__(**kwargs)
def build(self, input_shape):
super(OverwriteMasking, self).build(input_shape)
def call(self, inputs, **kwargs):
return inputs[0] * K.expand_dims(inputs[1])
def compute_output_shape(self, input_shape):
return input_shape[0]
class AttLayer2(layers.Layer):
"""Soft alignment attention implement.
Attributes:
dim (int): attention hidden dim
"""
def __init__(self, dim=200, seed=0, **kwargs):
"""Initialization steps for AttLayer2.
Args:
dim (int): attention hidden dim
"""
self.dim = dim
self.seed = seed
super(AttLayer2, self).__init__(**kwargs)
def build(self, input_shape):
"""Initialization for variables in AttLayer2
There are there variables in AttLayer2, i.e. W, b and q.
Args:
input_shape (obj): shape of input tensor.
"""
assert len(input_shape) == 3
dim = self.dim
self.W = self.add_weight(
name="W",
shape=(int(input_shape[-1]), dim),
initializer=keras.initializers.glorot_uniform(seed=self.seed),
trainable=True,
)
self.b = self.add_weight(
name="b",
shape=(dim,),
initializer=keras.initializers.Zeros(),
trainable=True,
)
self.q = self.add_weight(
name="q",
shape=(dim, 1),
initializer=keras.initializers.glorot_uniform(seed=self.seed),
trainable=True,
)
super(AttLayer2, self).build(input_shape) # be sure you call this somewhere!
def call(self, inputs, mask=None, **kwargs):
"""Core implemention of soft attention
Args:
inputs (obj): input tensor.
Returns:
obj: weighted sum of input tensors.
"""
attention = K.tanh(K.dot(inputs, self.W) + self.b)
attention = K.dot(attention, self.q)
attention = K.squeeze(attention, axis=2)
if mask == None:
attention = K.exp(attention)
else:
attention = K.exp(attention) * K.cast(mask, dtype="float32")
attention_weight = attention / (
K.sum(attention, axis=-1, keepdims=True) + K.epsilon()
)
attention_weight = K.expand_dims(attention_weight)
weighted_input = inputs * attention_weight
return K.sum(weighted_input, axis=1)
def compute_mask(self, input, input_mask=None):
"""Compte output mask value
Args:
input (obj): input tensor.
input_mask: input mask
Returns:
obj: output mask.
"""
return None
def compute_output_shape(self, input_shape):
"""Compute shape of output tensor
Args:
input_shape (tuple): shape of input tensor.
Returns:
tuple: shape of output tensor.
"""
return input_shape[0], input_shape[-1]
class SelfAttention(layers.Layer):
"""Multi-head self attention implement.
Args:
multiheads (int): The number of heads.
head_dim(obj): Dimention of each head.
mask_right(boolean): whether to mask right words.
Returns:
obj: Weighted sum after attention.
"""
def __init__(self, multiheads, head_dim, seed=0, mask_right=False, **kwargs):
"""Initialization steps for AttLayer2.
Args:
multiheads (int): The number of heads.
head_dim(obj): Dimention of each head.
mask_right(boolean): whether to mask right words.
"""
self.multiheads = multiheads
self.head_dim = head_dim
self.output_dim = multiheads * head_dim
self.mask_right = mask_right
self.seed = seed
super(SelfAttention, self).__init__(**kwargs)
def compute_output_shape(self, input_shape):
"""Compute shape of output tensor.
Returns:
tuple: output shape tuple.
"""
return (input_shape[0][0], input_shape[0][1], self.output_dim)
def build(self, input_shape):
"""Initialization for variables in SelfAttention.
There are three variables in SelfAttention, i.e. WQ, WK ans WV.
WQ is used for linear transformation of query.
WK is used for linear transformation of key.
WV is used for linear transformation of value.
Args:
input_shape (obj): shape of input tensor.
"""
self.WQ = self.add_weight(
name="WQ",
shape=(int(input_shape[0][-1]), self.output_dim),
initializer=keras.initializers.glorot_uniform(seed=self.seed),
trainable=True,
)
self.WK = self.add_weight(
name="WK",
shape=(int(input_shape[1][-1]), self.output_dim),
initializer=keras.initializers.glorot_uniform(seed=self.seed),
trainable=True,
)
self.WV = self.add_weight(
name="WV",
shape=(int(input_shape[2][-1]), self.output_dim),
initializer=keras.initializers.glorot_uniform(seed=self.seed),
trainable=True,
)
super(SelfAttention, self).build(input_shape)
def Mask(self, inputs, seq_len, mode="add"):
"""Mask operation used in multi-head self attention
Args:
seq_len (obj): sequence length of inputs.
mode (str): mode of mask.
Returns:
obj: tensors after masking.
"""
if seq_len == None:
return inputs
else:
mask = K.one_hot(indices=seq_len[:, 0], num_classes=K.shape(inputs)[1])
mask = 1 - K.cumsum(mask, axis=1)
for _ in range(len(inputs.shape) - 2):
mask = K.expand_dims(mask, 2)
if mode == "mul":
return inputs * mask
elif mode == "add":
return inputs - (1 - mask) * 1e12
def call(self, QKVs):
"""Core logic of multi-head self attention.
Args:
QKVs (list): inputs of multi-head self attention i.e. qeury, key and value.
Returns:
obj: ouput tensors.
"""
if len(QKVs) == 3:
Q_seq, K_seq, V_seq = QKVs
Q_len, V_len = None, None
elif len(QKVs) == 5:
Q_seq, K_seq, V_seq, Q_len, V_len = QKVs
Q_seq = K.dot(Q_seq, self.WQ)
Q_seq = K.reshape(
Q_seq, shape=(-1, K.shape(Q_seq)[1], self.multiheads, self.head_dim)
)
Q_seq = K.permute_dimensions(Q_seq, pattern=(0, 2, 1, 3))
K_seq = K.dot(K_seq, self.WK)
K_seq = K.reshape(
K_seq, shape=(-1, K.shape(K_seq)[1], self.multiheads, self.head_dim)
)
K_seq = K.permute_dimensions(K_seq, pattern=(0, 2, 1, 3))
V_seq = K.dot(V_seq, self.WV)
V_seq = K.reshape(
V_seq, shape=(-1, K.shape(V_seq)[1], self.multiheads, self.head_dim)
)
V_seq = K.permute_dimensions(V_seq, pattern=(0, 2, 1, 3))
A = K.batch_dot(Q_seq, K_seq, axes=[3, 3]) / K.sqrt(
K.cast(self.head_dim, dtype="float32")
)
A = K.permute_dimensions(
A, pattern=(0, 3, 2, 1)
) # A.shape=[batch_size,K_sequence_length,Q_sequence_length,self.multiheads]
A = self.Mask(A, V_len, "add")
A = K.permute_dimensions(A, pattern=(0, 3, 2, 1))
if self.mask_right:
ones = K.ones_like(A[:1, :1])
lower_triangular = K.tf.matrix_band_part(ones, num_lower=-1, num_upper=0)
mask = (ones - lower_triangular) * 1e12
A = A - mask
A = K.softmax(A)
O_seq = K.batch_dot(A, V_seq, axes=[3, 2])
O_seq = K.permute_dimensions(O_seq, pattern=(0, 2, 1, 3))
O_seq = K.reshape(O_seq, shape=(-1, K.shape(O_seq)[1], self.output_dim))
O_seq = self.Mask(O_seq, Q_len, "mul")
return O_seq
def get_config(self):
""" add multiheads, multiheads and mask_right into layer config.
Returns:
dict: config of SelfAttention layer.
"""
config = super(SelfAttention, self).get_config()
config.update(
{
"multiheads": self.multiheads,
"head_dim": self.head_dim,
"mask_right": self.mask_right,
}
)
return config
def cal_metric(labels, preds, metrics):
"""Calculate metrics,such as auc, logloss.
FIXME:
refactor this with the reco metrics and make it explicit.
"""
res = {}
for metric in metrics:
if metric == "auc":
auc = roc_auc_score(np.asarray(labels), np.asarray(preds))
res["auc"] = round(auc, 4)
elif metric == "rmse":
rmse = mean_squared_error(np.asarray(labels), np.asarray(preds))
res["rmse"] = np.sqrt(round(rmse, 4))
elif metric == "logloss":
# avoid logloss nan
preds = [max(min(p, 1.0 - 10e-12), 10e-12) for p in preds]
logloss = log_loss(np.asarray(labels), np.asarray(preds))
res["logloss"] = round(logloss, 4)
elif metric == "acc":
pred = np.asarray(preds)
pred[pred >= 0.5] = 1
pred[pred < 0.5] = 0
acc = accuracy_score(np.asarray(labels), pred)
res["acc"] = round(acc, 4)
elif metric == "f1":
pred = np.asarray(preds)
pred[pred >= 0.5] = 1
pred[pred < 0.5] = 0
f1 = f1_score(np.asarray(labels), pred)
res["f1"] = round(f1, 4)
elif metric == "mean_mrr":
mean_mrr = np.mean(
[
mrr_score(each_labels, each_preds)
for each_labels, each_preds in zip(labels, preds)
]
)
res["mean_mrr"] = round(mean_mrr, 4)
elif metric.startswith("ndcg"): # format like: ndcg@2;4;6;8
ndcg_list = [1, 2]
ks = metric.split("@")
if len(ks) > 1:
ndcg_list = [int(token) for token in ks[1].split(";")]
for k in ndcg_list:
ndcg_temp = np.mean(
[
ndcg_score(each_labels, each_preds, k)
for each_labels, each_preds in zip(labels, preds)
]
)
res["ndcg@{0}".format(k)] = round(ndcg_temp, 4)
elif metric.startswith("hit"): # format like: hit@2;4;6;8
hit_list = [1, 2]
ks = metric.split("@")
if len(ks) > 1:
hit_list = [int(token) for token in ks[1].split(";")]
for k in hit_list:
hit_temp = np.mean(
[
hit_score(each_labels, each_preds, k)
for each_labels, each_preds in zip(labels, preds)
]
)
res["hit@{0}".format(k)] = round(hit_temp, 4)
elif metric == "group_auc":
group_auc = np.mean(
[
roc_auc_score(each_labels, each_preds)
for each_labels, each_preds in zip(labels, preds)
]
)
res["group_auc"] = round(group_auc, 4)
else:
raise ValueError("not define this metric {0}".format(metric))
return res
def mrr_score(y_true, y_score):
"""Computing mrr score metric.
Args:
y_true (np.ndarray): ground-truth labels.
y_score (np.ndarray): predicted labels.
Returns:
np.ndarray: mrr scores.
"""
order = np.argsort(y_score)[::-1]
y_true = np.take(y_true, order)
rr_score = y_true / (np.arange(len(y_true)) + 1)
return np.sum(rr_score) / np.sum(y_true)
def ndcg_score(y_true, y_score, k=10):
"""Computing ndcg score metric at k.
Args:
y_true (np.ndarray): ground-truth labels.
y_score (np.ndarray): predicted labels.
Returns:
np.ndarray: ndcg scores.
"""
best = dcg_score(y_true, y_true, k)
actual = dcg_score(y_true, y_score, k)
return actual / best
def hit_score(y_true, y_score, k=10):
"""Computing hit score metric at k.
Args:
y_true (np.ndarray): ground-truth labels.
y_score (np.ndarray): predicted labels.
Returns:
np.ndarray: hit score.
"""
ground_truth = np.where(y_true == 1)[0]
argsort = np.argsort(y_score)[::-1][:k]
for idx in argsort:
if idx in ground_truth:
return 1
return 0
def dcg_score(y_true, y_score, k=10):
"""Computing dcg score metric at k.
Args:
y_true (np.ndarray): ground-truth labels.
y_score (np.ndarray): predicted labels.
Returns:
np.ndarray: dcg scores.
"""
k = min(np.shape(y_true)[-1], k)
order = np.argsort(y_score)[::-1]
y_true = np.take(y_true, order[:k])
gains = 2 ** y_true - 1
discounts = np.log2(np.arange(len(y_true)) + 2)
return np.sum(gains / discounts)