x=([1.,2.],
[2.,1.])
xtensor = tf.convert_to_tensor(x)
A = xtensor
B = xtensor
def cond(now,pre):
return (tf.greater(now,pre))
def body(now,pre):
return pre,now
A,now = tf.while_loop(cond,body,[A,B])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
A = sess.run(A)
B = sess.run(B)
now = sess.run(now)
实际上,代码的目的可以忽略,因为我简化了提出这个问题的两个函数,但仍然有相同的错误:
ValueError:Shape must be rank 0 but is rank 2 for 'while/LoopCond'(op:'LoopCond') with input shapes:[2,2].
我真的很困惑......希望有人能帮助我。非常感谢!
最佳答案
cond
的条件函数 ( tf.while_loop()
)必须返回等级为 0 的 bool 张量(即形状 []
,即单个 bool 值)。您的cond
返回等级 2 的 bool 张量(如 tf.greater(now, pre)
返回与 now
形状相同的张量,执行每个元素比较)。
关于python - 具有 ValueError Shape 的 while_loop 必须为 0 级,但 'while/LoopCond' 为 2 级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50504743/