2017-04-08 66 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