2017-04-08 102 views
0

我将两个参数传递给张量流中的一个我的(损失)函数,我认为这个函数应该是占位符的形式,因为它们因不同的步骤而改变。我在训练期间喂他们。 我的程序大纲如下。 我的问题是他们采取我有效地喂养他们的价值? 如果你能看看下面的代码片段,并告诉我我做得对,我将不胜感激。 我没有得到任何错误或顺便说一句。将张量流占位符作为函数参数传递

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

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

顺便说一句,如果t1总是具有相同的形状,我建议你传递一个形状,当你构建tf.placeholder()tetha1_placeholder(以及类似的t2tetha2_placeholder)。

+0

谢谢@mrry 我没有得到最后一部分。 t1和t2只是两个标量浮点数。你的意思是,如果我明确指定tetha1_placeholder的形状为shape =(1),会更好吗? – Hamid

+0

哦,如果它们都是标量,你可以将它们定义为'tetha1_placeholder = tf.placeholder(tf.float32,shape = [],name'tetha1plh')'(对于其他占位符类似)。这可以给你稍微更好的性能和错误消息,具体取决于确切的计算。 – mrry