-
Notifications
You must be signed in to change notification settings - Fork 7
/
generator_recsys_grec_nce.py
184 lines (129 loc) · 8.33 KB
/
generator_recsys_grec_nce.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
import tensorflow as tf
import ops
import modeling
class GRec_Archi:
def __init__(self, model_para):
self.model_para = model_para
self.is_negsample = model_para['is_negsample']
self.embedding_width = model_para['dilated_channels']
self.allitem_embeddings_en = tf.get_variable('allitem_embeddings_en',
[model_para['item_size'], self.embedding_width],
initializer=tf.truncated_normal_initializer(stddev=0.02))
self.allitem_embeddings_de = tf.get_variable('allitem_embeddings_de',
[model_para['item_size'], self.embedding_width],
initializer=tf.truncated_normal_initializer(stddev=0.02))
self.itemseq_input_en = tf.placeholder('int32',
[None, None], name='itemseq_input_en')
self.itemseq_input_de = tf.placeholder('int32',
[None, None], name='itemseq_input_de')
self.softmax_w = tf.get_variable("softmax_w", [self.model_para['item_size'], self.embedding_width], tf.float32,
tf.random_normal_initializer(0.0, 0.01))
self.softmax_b = tf.get_variable(
"softmax_b",
shape=[self.model_para['item_size']],
initializer=tf.constant_initializer(0.1))
def train_graph(self):
self.masked_position = tf.placeholder('int32',
[None, None], name='masked_position')
self.itemseq_output = tf.placeholder('int32',
[None, None], name='itemseq_output')
self.masked_items = tf.placeholder('int32',
[None, None], name='masked_items')
self.label_weights = tf.placeholder(tf.float32,
[None, None], name='label_weights')
context_seq_en = self.itemseq_input_en
context_seq_de = self.itemseq_input_de
label_seq = self.label_weights
dilate_input = self.model_graph(context_seq_en, context_seq_de, train=True)
self.loss = self.get_masked_lm_output(self.model_para, dilate_input,
self.masked_position,
self.masked_items, label_seq, trainable=True)
def model_graph(self, itemseq_input_en, itemseq_input_de, train=True):
model_para = self.model_para
context_embedding_en = tf.nn.embedding_lookup(self.allitem_embeddings_en,
itemseq_input_en)
context_embedding_de = tf.nn.embedding_lookup(self.allitem_embeddings_de,
itemseq_input_de)
# print model_para['max_position']
# dilate_input_en = context_embedding_en[:,0:-1,:]
# dilate_input_de = context_embedding_de[:,0:-1,:]
dilate_input_en = context_embedding_en
dilate_input_de = context_embedding_de
# residual_channels=dilate_input.get_shape()[-1]
residual_channels = dilate_input_en.get_shape().as_list()[-1]
for layer_id, dilation in enumerate(model_para['dilations']):
dilate_input_en = ops.nextitnet_residual_block_ED(dilate_input_en, dilation,
layer_id, residual_channels,
model_para['kernel_size'], causal=False, train=train,
encoder=True)
#add residual connection
# dilate_input_en=dilate_input_en+context_embedding_en
dilate_input_de = tf.add(dilate_input_en, dilate_input_de)
dilate_input_de = ops.get_adapter(dilate_input_de, 2 * residual_channels)
for layer_id, dilation in enumerate(model_para['dilations']):
dilate_input_de = ops.nextitnet_residual_block_ED(dilate_input_de, dilation,
layer_id, residual_channels,
model_para['kernel_size'], causal=True, train=train,
encoder=False)
return dilate_input_de
def predict_graph(self, reuse=False):
if reuse:
tf.get_variable_scope().reuse_variables()
context_seq_en = self.itemseq_input_en
context_seq_de = self.itemseq_input_de
dilate_input = self.model_graph(context_seq_en, context_seq_de, train=False)
model_para = self.model_para
if self.is_negsample:
logits_2D = tf.reshape(dilate_input[:, -1:, :], [-1, self.embedding_width])
logits_2D = tf.matmul(logits_2D, self.softmax_w, transpose_b=True)
logits_2D = tf.nn.bias_add(logits_2D, self.softmax_b)
else:
logits = ops.conv1d(tf.nn.relu(dilate_input)[:, -1:, :], model_para['item_size'], name='logits')
logits_2D = tf.reshape(logits, [-1, model_para['item_size']])
probs_flat = tf.nn.softmax(logits_2D)
# self.g_probs = tf.reshape(probs_flat, [-1, tf.shape(self.input_predict)[1], model_para['item_size']])
self.log_probs = probs_flat
# self.top_k = tf.nn.top_k(probs_flat, k=model_para['top_k'], name='top-k')
def gather_indexes(self, sequence_tensor, positions):
"""Gathers the vectors at the specific positions over a minibatch."""
sequence_shape = modeling.get_shape_list(sequence_tensor, expected_rank=3)
batch_size = sequence_shape[0]
seq_length = sequence_shape[1]
width = sequence_shape[2]
flat_offsets = tf.reshape(
tf.range(0, batch_size, dtype=tf.int32) * seq_length, [-1, 1])
flat_positions = tf.reshape(positions + flat_offsets, [-1])
flat_sequence_tensor = tf.reshape(sequence_tensor,
[batch_size * seq_length, width])
output_tensor = tf.gather(flat_sequence_tensor, flat_positions)
return output_tensor
def get_masked_lm_output(self, bert_config, input_tensor, positions,
label_ids, label_weights, trainable=True):
"""Get loss and log probs for the masked LM."""
input_tensor = self.gather_indexes(input_tensor, positions)
sequence_shape = modeling.get_shape_list(positions)
batch_size = sequence_shape[0]
seq_length = sequence_shape[1]
if self.is_negsample:
logits_2D = input_tensor
label_flat = tf.reshape(label_ids, [-1, 1]) # 1 is the number of positive example
# num_sampled = int(0.2 * self.model_para['item_size']) # sample 20% as negatives
# loss = tf.nn.sampled_softmax_loss(self.softmax_w, self.softmax_b, label_flat, logits_2D,
# num_sampled,
# self.model_para['item_size'])
#Be careful if you use nce loss, you need make sure your labels are sorted in order of decreasing frequency
#num_sampled=int(self.model_para['batch_size']*self.model_para['seq_len']*self.model_para['mask_per']*10)#each positive has 10 negative
num_sampled=int(self.model_para['batch_size']*self.model_para['neg_num'])
loss = tf.nn.nce_loss(self.softmax_w, self.softmax_b, label_flat, logits_2D,num_sampled,self.model_para['item_size'])
else:
residual_channels = input_tensor.get_shape().as_list()[-1]
input_tensor = tf.reshape(input_tensor, [-1, seq_length, residual_channels])
logits = ops.conv1d(tf.nn.relu(input_tensor), self.model_para['item_size'], name='logits')
logits_2D = tf.reshape(logits, [-1, self.model_para['item_size']])
label_flat = tf.reshape(label_ids, [-1])
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=label_flat, logits=logits_2D)
loss = tf.reduce_mean(loss)
# not sure the impact, 0.001 is empirical value, for large session data it is not very userful
regularization = 0.001 * tf.reduce_mean([tf.nn.l2_loss(v) for v in tf.trainable_variables()])
loss=loss+regularization
return loss