2017-09-05 77 views
0

我在張量流中創建SMAPE損失函數,並且在計算減少均值之前,我需要將張量diff的值設置爲0。這裏是我的代碼,但它不工作:在張量流中將值設置爲cero

function loss(yHat, y): 
    denominator = (tf.abs(yhat) + np.abs(y))/2.0 
    diff = tf.div(tf.abs(yhat - y),denominator) 
    other_variable = tf.get_variable("other_variable", 
           dtype=tf.float32, 
           initializer= diff) 
    comp = tf.equal(denominator, 0) 
    cond_diff = tf.scatter_update(other_variable, comp, 0) 
return tf.reduce_mean(cond_diff) 

它給了我這個錯誤

ValueError異常:initial_value必須指定的形狀:張量( 「div_49:0」,D類= FLOAT32)

有人可以幫助我,謝謝?

回答

0

只是掩蓋你的diff而不是創建另一個變量。

mask = tf.where(denominator == 0, tf.ones_like(denominator), tf.zeros_like(denominator)) 

return tf.reduce_mean(tf.multiply(diff,mask))