tensorflow中if判断相等 (使用==出错using a `tf.Tensor` as a Python `bool` is not allowed)
a=polyfit(t,Y,1);:
好像还是不太行,这种情况下,如果把“if A == B:”改成“if (A == B) is not None:”的话,最后判断只会是A == B,无论随机生成的矩阵A和B是否相同。比如下面的代码里,哪怕x和y打印出来明显是两个数值不同的矩阵,最终结果也永远print x==y。这是为什么呢?
[code=python]
import tensorflow as tf
def simple_mat_mul(x,y):
return tf.matmul(x,y)
def simple_mat_add(x,y):
return tf.add(x,y)
@tf.function
def simple_mat_op(x,y):
if (x == y) is not None:
print("x == y")
return simple_mat_add(x,y)
else:
print("x != y")
return simple_mat_mul(x,y)
x = tf.Variable(tf.random.uniform((4,4)))
y = tf.Variable(tf.random.uniform((4,4)))
print(x)
print(y)
print(simple_mat_op(x,y))
[/code]
|