改完MRG后,花了些时间改读取数据那块儿,好应用在模型上。改完后跑出来的结果挺差的,又反过来改了一遍数据,发现结果一样的差,今天源盛告诉我他那个跑得也差不多差,看来应该不是我写错了。
师姐又布置了改NRT
花了些时间看论文和代码
改代码读取现在的数据格式
模型卡了很久,根本没给训练调用的代码,仔细看了些天,还在研究怎么跑起来它
self.input_y = tf.placeholder(tf.float32, [None, 1], name="input_y")
self.input_y_tip = tf.placeholder(tf.int32, [None, para_dict[‘max_tip_len‘]], name="input_y_tip")
self.review_node_idxs = tf.placeholder(tf.int32, [None, para_dict[‘review_len‘]])
self.review_attn_dist = tf.placeholder(tf.float32, [None, para_dict[‘review_len‘]])
self.input_uid = tf.placeholder(tf.int32, [None, 1], name="input_uid")
self.input_iid = tf.placeholder(tf.int32, [None, 1], name="input_iid")
Placeholder的中文意思就是占位符,用于在会话运行时动态提供输入数据。Placeholder相当于定义了一个位置,在这个位置上的数据在程序运行时再指定。
self.W_tip = tf.get_variable("W_tip", [para_dict[‘tip_vocab_size‘], para_dict[‘embedding_tip_size‘]], dtype=tf.float32)
self.iidW = tf.get_variable("iidW", [para_dict[‘item_num‘], para_dict[‘embedding_id_size‘]], dtype=tf.float32)
self.uidW = tf.get_variable("uidW", [para_dict[‘user_num‘], para_dict[‘embedding_id_size‘]], dtype=tf.float32)
tf.get_variable( ) 会返回现有的变量。如果变量不存在,会根据给定形状和初始值创建变量。name = "W_tip", shape = [3878, 300], dtype=tf.float32, 未初始化
self.embeded_uid = tf.nn.embedding_lookup(self.uidW, self.input_uid)
self.embeded_iid = tf.nn.embedding_lookup(self.iidW, self.input_iid)
tf.nn.embedding_lookup(params, ids, partition_strategy=‘mod‘, name=None)按照ids从params这个矩阵中拿向量(行)
([20408, 300], [None, 1]) => (?, 1, 300)
rating_prediction
self.r = self.rating_prediction(para_dict)
Wu = tf.get_variable(‘Wru‘, [para_dict[‘embedding_id_size‘], para_dict[‘n_latent‘]])
这些W啥get_variable的是不是要先初始化成随机然后跑跑跑?
为啥这里连个train的数据都没有?
对应论文公式(3)(5)(6)
self.review_prediction, self.hcL = self.review_frequency_prediction(para_dict)
对应论文公式(14)(15)
self.h = self.get_s0(para_dict)
对应论文公式(17)
self.captions_in = self.input_y_tip[:, :-1]
self.captions_out = self.input_y_tip[:, 1:]
这里没赋值就调用吗?QAQQAQ这到底要怎么搞
果然是没赋值的问题我……
placeholder这样赋值??
import tensorflow as tf
sess = tf.Session()
model = graph_build(para_dict)
sess.run(tf.initialize_all_variables()) #会初始化那些get_Variables!!就是那些一开始是随机的矩阵?
sess.run(model.rmse, feed_dict={
model.input_y: y_train,
model.input_y_tip: y_train_tip,
model.review_node_idxs:
})
完了,这俩变量都是啥,要赋什么值
self.review_node_idxs = tf.placeholder(tf.int32, [None, para_dict[‘review_len‘]])
self.review_attn_dist = tf.placeholder(tf.float32, [None, para_dict[‘review_len‘]])
好像也没看见test数据放到哪里,计算起来应该是不一样的啊QAQ,这所有的计算又都是写在init里的……到底怎么办才好
gru_cell = tf.contrib.rnn.GRUCell(num_units=para_dict[‘hidden_dimension‘], name = ‘gru_cell‘)
self.captions_in = self.input_y_tip[:, :-1]
self.captions_out = self.input_y_tip[:, 1:]
self.mask = tf.to_float(tf.not_equal(self.captions_out, para_dict[‘tip_vocab_size‘] - 1))
self.x = tf.nn.embedding_lookup(self.W_tip, self.captions_in, name=‘word_vector‘)
self.batch_size = tf.shape(self.input_y_tip)[0]
loss = 0.0
with tf.variable_scope(‘sentence_generate‘):
for t in range(para_dict[‘max_tip_len‘] - 1):
if t > 0: tf.get_variable_scope().reuse_variables()
_, self.h = gru_cell(self.x[:,t,:], state=self.h)
Ws = tf.get_variable(‘Ws‘, [para_dict[‘hidden_dimension‘], para_dict[‘tip_vocab_size‘]])
Bs = tf.get_variable(‘Bs‘, [para_dict[‘tip_vocab_size‘]])
logits = tf.matmul(self.h, Ws) + Bs # 对应18
loss += tf.reduce_sum(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = self.captions_out[:, t], logits=logits) * self.mask[:, t]) # 对应20 计算tip_loss
对应公式(18)
with tf.variable_scope(‘loss‘):
tip_loss = loss / tf.to_float(self.batch_size)
r_loss = tf.reduce_mean(tf.square(self.input_y - self.r)) #对应7 rating_loss
self.input_y_review = self.review_preprocess(para_dict)
review_loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = self.input_y_review, logits = self.review_prediction)) #对应16
print(tip_loss, r_loss, review_loss)
self.loss = tip_loss + r_loss + review_loss
clipper = 50
self.learning_rate = tf.Variable(para_dict[‘learning_rate‘], trainable=False, name=‘learning_rate‘)
optimizer = tf.train.AdamOptimizer(learning_rate = self.learning_rate )
tvars = tf.trainable_variables()
if para_dict[‘lambda_l2‘] > 0.0:
l2_loss = tf.add_n([tf.nn.l2_loss(v) for v in tvars if v.get_shape().ndims > 1])
self.loss = self.loss + para_dict[‘lambda_l2‘] * l2_loss
grads, _ = tf.clip_by_global_norm(tf.gradients(self.loss, tvars), clipper)
self.train_op = optimizer.apply_gradients(zip(grads, tvars))
tf.get_variable_scope().reuse_variables()
self.sample_words = self.generate_sentence(para_dict)
self.rmse = tf.sqrt(tf.reduce_mean(tf.square(self.input_y - self.r)))
self.mae = tf.reduce_mean(tf.abs(self.input_y - self.r))
最后这里对应公式22?
原文:https://www.cnblogs.com/pppqq77799/p/13449336.html