python - 具有 ValueError Shape 的 while_loop 必须为 0 级,但 'while/LoopCond' 为 2 级

标签 python loops tensorflow rank

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/

相关文章:

python - 在 Tensorflow 中将张量转换为 numpy 数组?

Python - 使用 Osmnx 包获取 "TypeError: argument of type ' CRS' 不可迭代

python - Tkinter:如何在 Canvas 窗口中获取框架以扩展到 Canvas 的大小?

python - 使用 python 中的 openpyxl 更新行和列

javascript - 如何更新本地存储中的json值

machine-learning - 为什么Tensorflow tf.learn分类结果相差很大?

python - 如何在 tf.while_loop 中使用 tf.scatter_update

Python - 如何将方法作为参数传递以从另一个库调用方法

python - Tensorflow - 平均恢复模型的模型权重

JAVA:代替 break 语句?