【问题标题】:Shapes (1,) and () are not compatible on cond operator形状 (1,) 和 () 在 cond 运算符上不兼容
【发布时间】:2016-10-08 08:51:38
【问题描述】:

我想跳过一些具有特定标签的数据(例如 if label >= 7 或其他)。我的代码在这里:

true = tf.constant(True)
less_op = tf.less(label, tf.constant(delimiter))
label = tf.cast(
    tf.slice(record_bytes, [0], [label_bytes]), tf.int32)
tf.cond(less_op, lambda: true, lambda: true)

在第 4 行我有错误:ValueError: Shapes (1,) and () are not compatible。我假设它是由less_op 引起的(如果我用true 代码代替它)。我还调查了label 存在一些问题:代码less_op = tf.less(tf.constant(1), tf.constant(delimiter)) 完美运行。

【问题讨论】:

    标签: python-3.x tensorflow


    【解决方案1】:

    Tensorflow 期望它的形状为 None 或 [] 而不是 (1,)。这是我认为应该解决的奇怪行为,因为 tf.less 返回一个形状为 (1,) 而不是形状 () 的张量。

    改变这个:

    tf.cond(less_op, lambda: true, lambda: true)
    

    到这里:

    tf.cond(tf.reshape(less_op,[]), lambda: true, lambda: true)
    

    【讨论】:

    • tf.less 返回与输入相同的排名,因此 sess.run(tf.less(5, 4)) 具有形状 ()
    猜你喜欢
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多