在张量流中将值设置为零

时间:2017-09-05 01:57:30

标签: tensorflow

我在张量流中创建了SMAPE损失函数,我需要在计算降低均值之前将张量差值设置为0。这是我的代码,但它不起作用:

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必须具有指定的形状:Tensor(“div_49:0”,dtype = float32)

1 个答案:

答案 0 :(得分:0)

只需屏蔽您的diff,而不是创建另一个变量。

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

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