-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDiscrimiator.py
298 lines (250 loc) · 16.5 KB
/
Discrimiator.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 division
import tensorflow as tf
import pickle
import numpy as np
class Dis(object):
def __init__(self, itm_cnt, usr_cnt, rnn_size,mf_emb_dim,fc_feat_size,image_emb_size,
input_emb_dim,n_time_step, mixture,use_cnn,mixture_score,
learning_rate, beta1, grad_clip, lamda=0.2, initdelta=0.05,MF_paras=None,
model_type="rnn",use_sparse_tensor=True, update_rule="sgd",pairwise=False):
"""
Args:
dim_itm_embed: (optional) Dimension of item embedding.
dim_usr_embed: (optional) Dimension of user embedding.
dim_hidden: (optional) Dimension of all hidden state.
n_time_step: (optional) Time step size of LSTM.
usr_cnt: (optional) The size of all users.
itm_cnt: (optional) The size of all items.
"""
tf.set_random_seed(123)
self.V_M = itm_cnt
self.V_U = usr_cnt
self.T = n_time_step
self.rnn_size = rnn_size
self.fc_feat_size = fc_feat_size
self.image_emb_size = image_emb_size
self.mf_emb_dim = mf_emb_dim
self.input_emb_dim = input_emb_dim
self.param_mf = MF_paras
self.paras_rnn = []
self.model_type = model_type
self.mixture = mixture
self.mixture_score = mixture_score
self.user_sequence = tf.placeholder(tf.float32, [None, self.T, self.V_M])
self.item_sequence = tf.placeholder(tf.float32, [None, self.T, self.V_U])
self.u = tf.placeholder(tf.int32,[None,])
self.i = tf.placeholder(tf.int32,[None,])
if self.mixture == 'soft_V3':
self.rating = tf.placeholder(tf.float32, [None,])
else:
self.rating = tf.placeholder(tf.float32, [None,])
self.use_cnn = use_cnn
if self.use_cnn:
self.imgs_feats = tf.placeholder(tf.float32, [None, self.fc_feat_size])
self.encode_img_W = tf.get_variable('encode_img_W', [self.fc_feat_size, self.image_emb_size], initializer=self.emb_initializer)
self.encode_img_b = tf.get_variable('encode_img_b', [self.image_emb_size], initializer=self.const_initializer)
self.item_bias_rnn = tf.Variable(tf.zeros([self.V_M]))
self.user_bias_rnn = tf.Variable(tf.zeros([self.V_U]))
self.learning_rate = tf.placeholder(tf.float32)
self.beta1 = beta1
self.lamda = lamda # regularization parameters
self.initdelta = initdelta
self.grad_clip = grad_clip
self.update_rule = update_rule
self.weight_initializer = tf.random_uniform_initializer(minval=-self.initdelta, maxval=self.initdelta,dtype=tf.float32)
self.emb_initializer = tf.random_uniform_initializer(minval=-self.initdelta, maxval=self.initdelta,dtype=tf.float32)
self.const_initializer = tf.constant_initializer(0.0)
def _init_MF(self):
with tf.variable_scope('MF'):
if self.param_mf is None:
self.mf_user_embeddings = tf.get_variable('mf_user_emb', [self.V_U, self.mf_emb_dim], initializer=self.emb_initializer)
self.mf_item_embeddings = tf.get_variable('mf_item_emb', [self.V_M, self.mf_emb_dim], initializer=self.emb_initializer)
self.mf_item_bias = tf.Variable(tf.zeros([self.V_M]))
self.mf_user_bias = tf.Variable(tf.zeros([self.V_U]))
else:
self.mf_user_embeddings = tf.Variable(self.param[0])
self.mf_item_embeddings = tf.Variable(self.param[1])
self.mf_user_bias = tf.Variable(self.param[2])
self.mf_item_bias = tf.Variable(self.param[3])
def _decode_lstm(self, h_usr, h_itm, reuse=False):
if False:
with tf.variable_scope('D_rating', reuse=reuse):
w_usr = tf.get_variable('w_usr', [self.rnn_size, self.rnn_size], initializer=self.weight_initializer)
w_itm = tf.get_variable('w_itm', [self.rnn_size, self.rnn_size], initializer=self.weight_initializer)
# bias = tf.get_variable('bias', [self.rnn_size], initializer=self.const_initializer)
usr_vec = tf.matmul(h_usr, w_usr)
itm_vec = tf.matmul(h_itm, w_itm)
logits_RNN = tf.reduce_sum(tf.multiply(usr_vec, itm_vec), 1)
self.paras_rnn.extend([w_usr,w_itm])
return logits_RNN
else:
i_bias_rnn = tf.gather(self.item_bias_rnn, self.i)
u_bias_rnn = tf.gather(self.user_bias_rnn, self.u)
logits_RNN = tf.reduce_sum(tf.multiply(h_usr, h_itm), 1) + i_bias_rnn + u_bias_rnn
print("Do not use a fully-connectted layer at the time of output decoding.")
return logits_RNN
def _get_initial_lstm(self, batch_size):
with tf.variable_scope('D_initial_lstm'):
c_itm = tf.zeros([batch_size, self.rnn_size], tf.float32)
h_itm = tf.zeros([batch_size, self.rnn_size], tf.float32)
c_usr = tf.zeros([batch_size, self.rnn_size], tf.float32)
h_usr = tf.zeros([batch_size, self.rnn_size], tf.float32)
# self.paras_rnn.extend([c_itm, h_itm, c_usr, h_usr]) # these variable should be trainable or not
return c_itm, h_itm, c_usr, h_usr
def _rnn_item_embedding(self, inputs, reuse=False):
with tf.variable_scope('D_item_embedding', reuse=reuse):
w = tf.get_variable('w', [self.V_U, self.input_emb_dim], initializer=self.emb_initializer)
x_flat = tf.reshape(inputs, [-1, self.V_U]) #(N * T, U)
x = tf.matmul(x_flat, w) #(N * T, H)
x = tf.reshape(x, [-1, self.T, self.input_emb_dim]) #(N, T, H)
self.paras_rnn.extend([w])
return x
def _rnn_user_embedding(self, inputs, reuse=False):
with tf.variable_scope('D_user_embedding', reuse=reuse):
w = tf.get_variable('w', [self.V_M, self.input_emb_dim], initializer=self.emb_initializer)
x_flat = tf.reshape(inputs, [-1, self.V_M]) #(N * T, M)
x = tf.matmul(x_flat, w) #(N * T, H)
x = tf.reshape(x, [-1, self.T, self.input_emb_dim]) #(N, T, H)
self.paras_rnn.extend([w])
return x
def all_logits(self,u):
u_embedding = tf.nn.embedding_lookup(self.mf_user_embeddings, u)
return tf.matmul(u_embedding, self.mf_item_embeddings, transpose_a=False,transpose_b=True)+ self.mf_item_bias #+u_bias
def get_mf_logists(self,u,i):
mf_u_embedding = tf.nn.embedding_lookup(self.mf_user_embeddings, u)
mf_i_embedding = tf.nn.embedding_lookup(self.mf_item_embeddings, i)
mf_i_bias = tf.gather(self.mf_item_bias, i)
mf_u_bias = tf.gather(self.mf_user_bias, u)
logits_MF = tf.reduce_sum(tf.multiply(mf_i_embedding, mf_u_embedding), 1) + mf_i_bias + mf_u_bias
return logits_MF
def _attention_layer(self, features, features_proj, h, L,name, reuse=False):
with tf.variable_scope(name+'_attention_layer', reuse=reuse):
w = tf.get_variable('w', [self.mf_emb_dim, self.mf_emb_dim], initializer=self.weight_initializer)
b = tf.get_variable('b', [self.mf_emb_dim], initializer=self.const_initializer)
w_att = tf.get_variable('w_att', [self.mf_emb_dim, 1], initializer=self.weight_initializer)
h_att = tf.nn.relu(features_proj + tf.expand_dims(tf.matmul(h, w), 1) + b) # (N, L, D)
out_att = tf.reshape(tf.matmul(tf.reshape(h_att, [-1, self.mf_emb_dim]), w_att), [-1, L]) # (N, L)
alpha = tf.nn.softmax(out_att)
context = tf.reduce_sum(features * tf.expand_dims(alpha, 2), 1, name='context') #(N, D)
return context, alpha
def _project_features(self, features, L, name):
with tf.variable_scope(name + '_project_features'):
w = tf.get_variable('w', [self.mf_emb_dim, self.mf_emb_dim], initializer=self.weight_initializer)
features_flat = tf.reshape(features, [-1, self.mf_emb_dim])
features_proj = tf.matmul(features_flat, w)
features_proj = tf.reshape(features_proj, [-1, L, self.mf_emb_dim])
return features_proj
def build_pretrain(self):
self._init_MF()
batch_size = tf.cast(tf.shape(self.item_sequence)[0], tf.int32)
c_itm, h_itm, c_usr, h_usr = self._get_initial_lstm(batch_size)
itm_lstm_cell = tf.contrib.rnn.LSTMCell(num_units=self.rnn_size)
usr_lstm_cell = tf.contrib.rnn.LSTMCell(num_units=self.rnn_size)
input_itms = self._rnn_item_embedding(inputs=self.item_sequence)
input_usrs = self._rnn_user_embedding(inputs=self.user_sequence)
mf_u_embedding = tf.nn.embedding_lookup(self.mf_user_embeddings, self.u)
mf_i_embedding = tf.nn.embedding_lookup(self.mf_item_embeddings, self.i)
if self.use_cnn:
image_emb = tf.matmul(self.imgs_feats, self.encode_img_W) + self.encode_img_b
nsteps = self.T + 1
else:
nsteps = self.T
for t in range(nsteps):
with tf.variable_scope('G_itm_lstm', reuse=(t!=0)):
if t == 0 and self.use_cnn and True:
current_emb = image_emb
else:
if self.use_cnn:
n_t = t - 1
else:
n_t = t
if self.mixture == 'soft_V1':
current_emb = input_itms[:,n_t,:]
elif self.mixture == 'soft_V2':
current_emb = tf.concat( [input_itms[:,n_t,:], mf_i_embedding],axis=1)
elif self.mixture == 'soft_V3':
item_context,alpha = self._attention_layer(item_features,item_features_proj, h_itm, self.V_M, name='item', reuse=(t!=0))
current_emb = tf.concat([x_itm[:,n_t,:], item_context],axis=1)
elif self.mixture == 'hard':
current_emb = input_itms[:,n_t,:]
else:
assert False
_, (c_itm, h_itm) = itm_lstm_cell(inputs=current_emb, state=[c_itm, h_itm])
for t in range(self.T):
with tf.variable_scope('G_usr-lstm', reuse=(t!=0)):
if self.mixture == 'soft_V1':
current_emb = input_usrs[:,t,:]
elif self.mixture == 'soft_V2':
current_emb = tf.concat( [input_usrs[:,t,:], mf_u_embedding],axis=1)
elif self.mixture == 'soft_V3':
user_context,alpha = self._attention_layer(user_features, user_features_proj, h_usr, self.V_U, name='user', reuse=(t!=0))
current_emb = tf.concat([x_usr[:,t,:], user_context],axis=1)
elif self.mixture == 'hard':
current_emb = input_usrs[:,t,:]
else:
assert False
_, (c_usr, h_usr) = usr_lstm_cell(inputs=current_emb, state=[c_usr, h_usr])
if self.mixture == 'soft_V3':
logits_RNN = self._decode_lstm(h_usr, h_itm, reuse=False)
joint_loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=self.rating[:,t], logits=logits_RNN)
loss += tf.reduce_sum(joint_loss)
self.logits_RNN = tf.nn.relu(self._decode_lstm(h_usr, h_itm, reuse=False)) #+self.i_bias_rnn
self.logits_MF = tf.nn.relu(self.get_mf_logists(self.u,self.i))
if self.mixture == 'soft_V3':
self.loss_RNN = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=self.rating[:,self.T-1], logits=self.logits_RNN))
self.loss_MF = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels=self.rating[:,self.T-1], logits=self.logits_MF))
self.joint_loss = loss / tf.to_float(batch_size)
self.joint_loss += self.lamda * tf.reduce_sum([ tf.nn.l2_loss(v) for v in tf.trainable_variables() ])
if self.update_rule == 'adam':
self.optimizer = tf.train.AdamOptimizer
elif self.update_rule == 'momentum':
self.optimizer = tf.train.MomentumOptimizer
elif self.update_rule == 'rmsprop':
self.optimizer = tf.train.RMSPropOptimizer
else:
self.optimizer = tf.train.GradientDescentOptimizer
global_step = tf.train.get_global_step()
self.pretrain_updates = tf.train.AdamOptimizer(self.learning_rate, beta1 = self.beta1)\
.minimize(self.joint_loss, var_list=tf.trainable_variables())
self.all_logits = self.all_logits(self.u)
def pretrain_step(self, sess, learning_rate, rating, u, i,user_sequence=None, item_sequence=None, img_feats=None):
if user_sequence is not None:
if self.use_cnn:
outputs = sess.run([self.pretrain_updates, self.loss_MF ,self.loss_RNN,self.joint_loss,self.logits_MF,self.logits_RNN ], feed_dict = {self.user_sequence: user_sequence,
self.item_sequence: item_sequence, self.rating: rating, self.u: u, self.i: i, self.imgs_feats:img_feats})
else:
outputs = sess.run([self.pretrain_updates, self.loss_MF ,self.loss_RNN,
self.joint_loss,self.logits_MF,self.logits_RNN ],
feed_dict = {self.user_sequence: user_sequence,
self.item_sequence: item_sequence,
self.rating: rating,
self.u: u,
self.i: i,
self.learning_rate: learning_rate})
else:
outputs = sess.run([self.pretrain_updates, self.joint_loss,self.pre_logits_MF], feed_dict = {self.rating: rating, self.u: u, self.i: i})
return outputs
def prediction(self, sess, user_sequence, item_sequence, u, i,sparse=True, use_sparse_tensor = None,img_feats=None):
if sparse:
user_sequence,item_sequence=[ii.toarray() for ii in user_sequence],[ii.toarray() for ii in item_sequence]
if self.use_cnn:
outputs = sess.run(self.joint_logits, feed_dict = {self.user_sequence: user_sequence,
self.item_sequence: item_sequence, self.u: u, self.i: i, self.imgs_feats:img_feats})
else:
outputs = sess.run(self.joint_logits, feed_dict = {self.user_sequence: user_sequence,
self.item_sequence: item_sequence, self.u: u, self.i: i})
return outputs
def predictionItems(self, sess, u):
outputs = sess.run(self.all_logits, feed_dict = {self.u: [u]})
return outputs
def getRewards(self,sess,gen, samples,sparse=False):
u_seq,i_seq = [[ sample[i].toarray() for sample in samples ] for i in range(2)]
u,i = [[ sample[i] for sample in samples ] for i in range(2,4)]
labeled_rewards = np.zeros(len(samples))
unlabeled_rewards = self.prediction(sess,u_seq,i_seq,u,i)
rewards = labeled_rewards + unlabeled_rewards
return 2 * (self.sigmoid(rewards) - 0.5)
def saveMFModel(self, sess, filename):
self.paras_mf = [self.mf_user_embeddings,self.mf_item_embeddings,self.mf_user_bias,self.mf_item_bias]
param = sess.run(self.paras_mf)
pickle.dump(param, open(filename, 'wb'))