gpt4 book ai didi

python - Tensorflow.py protected 划分

转载 作者:太空宇宙 更新时间:2023-11-03 21:18:32 25 4
gpt4 key购买 nike

我正在尝试使用 Tensorflow.where 实现一种 protected 除法,但不知何故它似乎跳过了 where 语句上设置的条件。

主要思想是,在除 x/y 时,如果 y == 0. 则除法的结果为 x而不是抛出错误。

我的代码如下:

def Pdivide(x,y):
result = tf.where(y == 0., x, x/y)
return result

但不知何故,这个条件被跳过了:

>>> a = tf.Variable([1.7, 0.2, 0., 1.1, 0.9, 0.3, 23., -1.])
>>> b = tf.Variable([0., 0., 0., 1., 1., 0., 1., 1.])

>>>Pdivide(a,b)

>>>(inf, inf, nan, 1.1, 0.9, inf, 23, -1)

预期输出:

>>>(1.7, 0.2, 0., 1.1, 0.9, 0.3, 23, -1)

PS:使用eager执行。

最佳答案

好吧,答案显然很简单。

由于某种原因,张量元素无法与简单的 == 进行比较,但使用 tf.equal(y, 0.) 可以解决问题并产生正确的输出。

关于python - Tensorflow.py protected 划分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54499275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com