-
Notifications
You must be signed in to change notification settings - Fork 13
/
utility.py
466 lines (388 loc) · 16.7 KB
/
utility.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
import os
import copy
import math
import numpy as np
import pandas as pd
from collections import deque
import torch.nn as nn
import torch
import torch.nn.functional as F
# import tensorflow as tf
def extract_axis_1(data, indices):
res = []
for i in range(data.shape[0]):
res.append(data[i, indices[i], :])
res = torch.stack(res, dim=0).unsqueeze(1)
return res
def to_pickled_df(data_directory, **kwargs):
for name, df in kwargs.items():
df.to_pickle(os.path.join(data_directory, name + '.df'))
def pad_history(itemlist,length,pad_item):
if len(itemlist)>=length:
return itemlist[-length:]
if len(itemlist)<length:
temp = [pad_item] * (length-len(itemlist))
itemlist.extend(temp)
return itemlist
# def extract_axis_1(data, ind):
# """
# Get specified elements along the first axis of tensor.
# :param data: Tensorflow tensor that will be subsetted.
# :param ind: Indices to take (one for each element along axis 0 of data).
# :return: Subsetted tensor.
# """
# batch_range = tf.range(tf.shape(data)[0])
# indices = tf.stack([batch_range, ind], axis=1)
# res = tf.gather_nd(data, indices)
# return res
def normalize(inputs,
epsilon=1e-8,
scope="ln",
reuse=None):
'''Applies layer normalization.
Args:
inputs: A tensor with 2 or more dimensions, where the first dimension has
`batch_size`.
epsilon: A floating number. A very small number for preventing ZeroDivision Error.
scope: Optional scope for `variable_scope`.
reuse: Boolean, whether to reuse the weights of a previous layer
by the same name.
Returns:
A tensor with the same shape and data dtype as `inputs`.
'''
with tf.variable_scope(scope, reuse=reuse):
inputs_shape = inputs.get_shape()
params_shape = inputs_shape[-1:]
mean, variance = tf.nn.moments(inputs, [-1], keep_dims=True)
beta = tf.Variable(tf.zeros(params_shape))
gamma = tf.Variable(tf.ones(params_shape))
normalized = (inputs - mean) / ((variance + epsilon) ** (.5))
outputs = gamma * normalized + beta
return outputs
def calculate_hit(sorted_list,topk,true_items,hit_purchase,ndcg_purchase):
for i in range(len(topk)):
rec_list = sorted_list[:, -topk[i]:]
# print(rec_list)
# print(true_items)
# print('...........')
# break
for j in range(len(true_items)):
if true_items[j] in rec_list[j]:
rank = topk[i] - np.argwhere(rec_list[j] == true_items[j])
# total_reward[i] += rewards[j]
# if rewards[j] == r_click:
# hit_click[i] += 1.0
# ndcg_click[i] += 1.0 / np.log2(rank + 1)
# else:
hit_purchase[i] += 1.0
ndcg_purchase[i] += 1.0 / np.log2(rank + 1)
# class Memory():
# def __init__(self):
# self.buffer = deque()
#
# def add(self, experience):
# self.buffer.append(experience)
#
# def sample(self, batch_size):
# idx = np.random.choice(np.arange(len(self.buffer)),
# size=batch_size,
# replace=False)
# return [self.buffer[ii] for ii in idx]
class NeuProcessEncoder(nn.Module):
def __init__(self, input_size=64, hidden_size=64, output_size=64, dropout_prob=0.4, device=None):
super(NeuProcessEncoder, self).__init__()
self.device = device
# Encoder for item embeddings
layers = [nn.Linear(input_size, hidden_size),
torch.nn.Dropout(dropout_prob),
nn.ReLU(inplace=True),
nn.Linear(hidden_size, output_size)]
self.input_to_hidden = nn.Sequential(*layers)
# Encoder for latent vector z
self.z1_dim = input_size # 64
self.z2_dim = hidden_size # 64
self.z_dim = output_size # 64
self.z_to_hidden = nn.Linear(self.z1_dim, self.z2_dim)
self.hidden_to_mu = nn.Linear(self.z2_dim, self.z_dim)
self.hidden_to_logsigma = nn.Linear(self.z2_dim, self.z_dim)
def emb_encode(self, input_tensor):
hidden = self.input_to_hidden(input_tensor)
return hidden
def aggregate(self, input_tensor):
return torch.mean(input_tensor, dim=-2)
def z_encode(self, input_tensor):
hidden = torch.relu(self.z_to_hidden(input_tensor))
mu = self.hidden_to_mu(hidden)
log_sigma = self.hidden_to_logsigma(hidden)
std = torch.exp(0.5 * log_sigma)
eps = torch.randn_like(std)
z = eps.mul(std).add_(mu)
return z, mu, log_sigma
def encoder(self, input_tensor):
z_ = self.emb_encode(input_tensor)
z = self.aggregate(z_)
self.z, mu, log_sigma = self.z_encode(z)
return self.z, mu, log_sigma
def forward(self, input_tensor):
self.z, _, _ = self.encoder(input_tensor)
return self.z
class MemoryUnit(nn.Module):
# clusters_k is k keys
def __init__(self, input_size, output_size, emb_size, clusters_k=10):
super(MemoryUnit, self).__init__()
self.clusters_k = clusters_k
self.input_size = input_size
self.output_size = output_size
self.array = nn.Parameter(init.xavier_uniform_(torch.FloatTensor(self.clusters_k, input_size*output_size)))
self.index = nn.Parameter(init.xavier_uniform_(torch.FloatTensor(self.clusters_k, emb_size)))
self.softmax = nn.Softmax(dim=-1)
def forward(self, bias_emb):
"""
bias_emb: [batch_size, 1, emb_size]
"""
att_scores = torch.matmul(bias_emb, self.index.transpose(-1, -2)) # [batch_size, clusters_k]
att_scores = self.softmax(att_scores)
# [batch_size, input_size, output_size]
para_new = torch.matmul(att_scores, self.array) # [batch_size, input_size*output_size]
para_new = para_new.view(-1, self.output_size, self.input_size)
return para_new
def reg_loss(self, reg_weights=1e-2):
loss_1 = reg_weights * self.array.norm(2)
loss_2 = reg_weights * self.index.norm(2)
return loss_1 + loss_2
class FeedForward(nn.Module):
"""
Point-wise feed-forward layer is implemented by two dense layers.
Args:
input_tensor (torch.Tensor): the input of the point-wise feed-forward layer
Returns:
hidden_states (torch.Tensor): the output of the point-wise feed-forward layer
"""
def __init__(
self, hidden_size, inner_size, hidden_dropout_prob, hidden_act, layer_norm_eps
):
super(FeedForward, self).__init__()
self.dense_1 = nn.Linear(hidden_size, inner_size)
self.intermediate_act_fn = self.get_hidden_act(hidden_act)
self.dense_2 = nn.Linear(inner_size, hidden_size)
self.LayerNorm = nn.LayerNorm(hidden_size, eps=layer_norm_eps)
self.dropout = nn.Dropout(hidden_dropout_prob)
def get_hidden_act(self, act):
ACT2FN = {
"gelu": self.gelu,
"relu": F.relu,
"swish": self.swish,
"tanh": torch.tanh,
"sigmoid": torch.sigmoid,
}
return ACT2FN[act]
def gelu(self, x):
"""Implementation of the gelu activation function.
For information: OpenAI GPT's gelu is slightly different (and gives slightly different results)::
0.5 * x * (1 + torch.tanh(math.sqrt(2 / math.pi) * (x + 0.044715 * torch.pow(x, 3))))
Also see https://arxiv.org/abs/1606.08415
"""
return x * 0.5 * (1.0 + torch.erf(x / math.sqrt(2.0)))
def swish(self, x):
return x * torch.sigmoid(x)
def forward(self, input_tensor):
hidden_states = self.dense_1(input_tensor)
hidden_states = self.intermediate_act_fn(hidden_states)
hidden_states = self.dense_2(hidden_states)
hidden_states = self.dropout(hidden_states)
hidden_states = self.LayerNorm(hidden_states + input_tensor)
return hidden_states
class ItemToInterestAggregation(nn.Module):
def __init__(self, seq_len, hidden_size, k_interests=5):
super().__init__()
self.k_interests = k_interests # k latent interests
self.theta = nn.Parameter(torch.randn([hidden_size, k_interests]))
def forward(self, input_tensor): # [B, L, d] -> [B, k, d]
D_matrix = torch.matmul(input_tensor, self.theta) # [B, L, k]
D_matrix = nn.Softmax(dim=-2)(D_matrix)
result = torch.einsum("nij, nik -> nkj", input_tensor, D_matrix) # #[B, k, d]
return result
class LightMultiHeadAttention(nn.Module):
def __init__(
self,
n_heads,
k_interests,
hidden_size,
seq_len,
hidden_dropout_prob,
attn_dropout_prob,
layer_norm_eps,
):
super(LightMultiHeadAttention, self).__init__()
if hidden_size % n_heads != 0:
raise ValueError(
"The hidden size (%d) is not a multiple of the number of attention "
"heads (%d)" % (hidden_size, n_heads)
)
self.num_attention_heads = n_heads
self.attention_head_size = int(hidden_size / n_heads)
self.all_head_size = self.num_attention_heads * self.attention_head_size
# initialization for low-rank decomposed self-attention
self.query = nn.Linear(hidden_size, self.all_head_size)
self.key = nn.Linear(hidden_size, self.all_head_size)
self.value = nn.Linear(hidden_size, self.all_head_size)
self.attpooling_key = ItemToInterestAggregation(
seq_len, hidden_size, k_interests
)
self.attpooling_value = ItemToInterestAggregation(
seq_len, hidden_size, k_interests
)
# initialization for decoupled position encoding
self.attn_scale_factor = 2
self.pos_q_linear = nn.Linear(hidden_size, self.all_head_size)
self.pos_k_linear = nn.Linear(hidden_size, self.all_head_size)
self.pos_scaling = (
float(self.attention_head_size * self.attn_scale_factor) ** -0.5
)
self.pos_ln = nn.LayerNorm(hidden_size, eps=layer_norm_eps)
self.attn_dropout = nn.Dropout(attn_dropout_prob)
self.dense = nn.Linear(hidden_size, hidden_size)
self.LayerNorm = nn.LayerNorm(hidden_size, eps=layer_norm_eps)
self.out_dropout = nn.Dropout(hidden_dropout_prob)
def transpose_for_scores(self, x): # transfor to multihead
new_x_shape = x.size()[:-1] + (
self.num_attention_heads,
self.attention_head_size,
)
x = x.view(*new_x_shape)
return x.permute(0, 2, 1, 3)
def forward(self, input_tensor, pos_emb):
# linear map
mixed_query_layer = self.query(input_tensor)
mixed_key_layer = self.key(input_tensor)
mixed_value_layer = self.value(input_tensor)
# low-rank decomposed self-attention: relation of items
query_layer = self.transpose_for_scores(mixed_query_layer)
key_layer = self.transpose_for_scores(self.attpooling_key(mixed_key_layer))
value_layer = self.transpose_for_scores(
self.attpooling_value(mixed_value_layer)
)
attention_scores = torch.matmul(query_layer, key_layer.transpose(-1, -2))
attention_scores = attention_scores / math.sqrt(self.attention_head_size)
# normalize the attention scores to probabilities.
attention_probs = nn.Softmax(dim=-2)(attention_scores)
attention_probs = self.attn_dropout(attention_probs)
context_layer_item = torch.matmul(attention_probs, value_layer)
# decoupled position encoding: relation of positions
value_layer_pos = self.transpose_for_scores(mixed_value_layer)
pos_emb = self.pos_ln(pos_emb).unsqueeze(0)
pos_query_layer = (
self.transpose_for_scores(self.pos_q_linear(pos_emb)) * self.pos_scaling
)
pos_key_layer = self.transpose_for_scores(self.pos_k_linear(pos_emb))
abs_pos_bias = torch.matmul(pos_query_layer, pos_key_layer.transpose(-1, -2))
abs_pos_bias = abs_pos_bias / math.sqrt(self.attention_head_size)
abs_pos_bias = nn.Softmax(dim=-2)(abs_pos_bias)
context_layer_pos = torch.matmul(abs_pos_bias, value_layer_pos)
context_layer = context_layer_item + context_layer_pos
context_layer = context_layer.permute(0, 2, 1, 3).contiguous()
new_context_layer_shape = context_layer.size()[:-2] + (self.all_head_size,)
context_layer = context_layer.view(*new_context_layer_shape)
hidden_states = self.dense(context_layer)
hidden_states = self.out_dropout(hidden_states)
hidden_states = self.LayerNorm(hidden_states + input_tensor)
return hidden_states
class LightTransformerLayer(nn.Module):
"""
One transformer layer consists of a multi-head self-attention layer and a point-wise feed-forward layer.
Args:
hidden_states (torch.Tensor): the input of the multi-head self-attention sublayer
attention_mask (torch.Tensor): the attention mask for the multi-head self-attention sublayer
Returns:
feedforward_output (torch.Tensor): the output of the point-wise feed-forward sublayer, is the output of the transformer layer
"""
def __init__(
self,
n_heads,
k_interests,
hidden_size,
seq_len,
intermediate_size,
hidden_dropout_prob,
attn_dropout_prob,
hidden_act,
layer_norm_eps,
):
super(LightTransformerLayer, self).__init__()
self.multi_head_attention = LightMultiHeadAttention(
n_heads,
k_interests,
hidden_size,
seq_len,
hidden_dropout_prob,
attn_dropout_prob,
layer_norm_eps,
)
self.feed_forward = FeedForward(
hidden_size,
intermediate_size,
hidden_dropout_prob,
hidden_act,
layer_norm_eps,
)
def forward(self, hidden_states, pos_emb):
attention_output = self.multi_head_attention(hidden_states, pos_emb)
feedforward_output = self.feed_forward(attention_output)
return feedforward_output
class LightTransformerEncoder(nn.Module):
r"""One LightTransformerEncoder consists of several LightTransformerLayers.
Args:
n_layers(num): num of transformer layers in transformer encoder. Default: 2
n_heads(num): num of attention heads for multi-head attention layer. Default: 2
hidden_size(num): the input and output hidden size. Default: 64
inner_size(num): the dimensionality in feed-forward layer. Default: 256
hidden_dropout_prob(float): probability of an element to be zeroed. Default: 0.5
attn_dropout_prob(float): probability of an attention score to be zeroed. Default: 0.5
hidden_act(str): activation function in feed-forward layer. Default: 'gelu'.
candidates: 'gelu', 'relu', 'swish', 'tanh', 'sigmoid'
layer_norm_eps(float): a value added to the denominator for numerical stability. Default: 1e-12
"""
def __init__(
self,
n_layers=2,
n_heads=2,
k_interests=5,
hidden_size=64,
seq_len=50,
inner_size=256,
hidden_dropout_prob=0.5,
attn_dropout_prob=0.5,
hidden_act="gelu",
layer_norm_eps=1e-12,
):
super(LightTransformerEncoder, self).__init__()
layer = LightTransformerLayer(
n_heads,
k_interests,
hidden_size,
seq_len,
inner_size,
hidden_dropout_prob,
attn_dropout_prob,
hidden_act,
layer_norm_eps,
)
self.layer = nn.ModuleList([copy.deepcopy(layer) for _ in range(n_layers)])
def forward(self, hidden_states, pos_emb, output_all_encoded_layers=True):
"""
Args:
hidden_states (torch.Tensor): the input of the TrandformerEncoder
attention_mask (torch.Tensor): the attention mask for the input hidden_states
output_all_encoded_layers (Bool): whether output all transformer layers' output
Returns:
all_encoder_layers (list): if output_all_encoded_layers is True, return a list consists of all transformer layers' output,
otherwise return a list only consists of the output of last transformer layer.
"""
all_encoder_layers = []
for layer_module in self.layer:
hidden_states = layer_module(hidden_states, pos_emb)
if output_all_encoded_layers:
all_encoder_layers.append(hidden_states)
if not output_all_encoded_layers:
all_encoder_layers.append(hidden_states)
return all_encoder_layers