将张量流占位符作为函数参数传递

时间:2017-04-08 03:48:12

标签: python tensorflow

我将两个参数传递给张量流中的my(丢失)函数,我认为它应该采用占位符的形式,因为它们会针对不同的步骤进行更改。我在训练期间喂它们。 我的课程大纲如下。 我的问题是他们是否采取了我有效提供的价值观? 如果您能看一下以下片段并告诉我这样做,我将不胜感激? 顺便说一下,我没有收到任何错误或其他任何错误。

tetha1_placeholder, tetha2_placeholder = tf.placeholder(tf.float32, name='tetha1plh'), tf.placeholder(tf.float32, name='tetha2plh')
hyperparams = {'tetha1': tetha1_placeholder,'tetha2':tetha2_placeholder} 
[getting embeddings1,embeddings2, embeddings3 from my model]
loss = loss_function (embeddings1,embeddings1,embeddings3, hyperparams) 

with sess.as_default():

   while true:
        step = sess.run(global_step, feed_dict=None)
        t1, t2 = calculate_params(step)
        feed_dict = {tetha1_placeholder:t1, tetha2_placeholder:t2}            
        error=sess.run([loss], feed_dict=feed_dict)

def loss_function (embeddings1,embeddings2,embeddings3, hyperparams):
       pos_dist =hyperparams['tetha1'] * tf.reduce_sum(tf.square(tf.subtract(embeddings1, embeddings2)), 1)
       neg_dist = hyperparams['tetha2'] *tf.reduce_sum(tf.square(tf.subtract(embeddings1, embeddings3)), 1)
       loss = tf.reduce_mean(tf.add(pos_dist,neg_dist))
       return loss

1 个答案:

答案 0 :(得分:0)

该程序看起来正确。当您致电sess.run([loss], feed_dict=feed_dict)时,hyperparams['tetha1']中的张量loss_function()将具有值t1,而hyperparams['tetha2']中的张量loss_function()将具有该值t2

顺便说一句,如果t1总是具有相同的形状,我建议在为tf.placeholder()构建tetha1_placeholder时传递该形状(同样适用于t2tetha2_placeholder)。