【问题标题】:TypeError: Input 'y' of 'Sub' Op has type float32 that does not match type int64 of argument 'x'TypeError:“Sub”Op 的输入“y”的 float32 类型与参数“x”的 int64 类型不匹配
【发布时间】:2021-11-01 04:31:54
【问题描述】:

错误:

Exception: in user code:

    /opt/conda/lib/python3.7/site-packages/keras/engine/training.py:853 train_function  *
        return step_function(self, iterator)
    /tmp/ipykernel_34/1396363757.py:53 class_loss_regr_fixed_num  *
        x = y_true[:, :, 4*num_classes:] - y_pred
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:1383 binary_op_wrapper
        raise e
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:1367 binary_op_wrapper
        return func(x, y, name=name)
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py:206 wrapper
        return target(*args, **kwargs)
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:548 subtract
        return gen_math_ops.sub(x, y, name)
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_math_ops.py:10654 sub
        "Sub", x=x, y=y, name=name)
    /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/op_def_library.py:558 _apply_op_helper
        inferred_from[input_arg.type_attr]))

    TypeError: Input 'y' of 'Sub' Op has type float32 that does not match type int64 of argument 'x'.

Exception: 'NoneType' object is not callable

此错误出现在图像类型和标签类型之间。从上面的图像类型是 int64 而标签类型是 float32。您可以查看我的笔记本了解更多详细信息。我不明白问题是什么,因为我是这个领域的新手。

【问题讨论】:

    标签: keras deep-learning tensorflow2.0


    【解决方案1】:

    这个错误来自x = y_true[:, :, 4*num_classes:] - y_pred这一行。

    在这个TypeError: Input 'y' of 'Sub' Op has type float32 that does not match type int64 of argument 'x' ===> 'y' = y_pred'x' = y_true[:, :, 4*num_classes:]

    y_pred 的 dtype = 'float32'

    y_true[:, :, 4*num_classes:] 具有 dtype = 'int64'

    当您尝试从dtype('int64') 中减去dtype('float32') 时出现错误

    要解决这个问题,您需要更改两个变量的数据类型。在我的例子中,y_predy_true 是 4D 张量,所以你需要 tf.cast(y_pred|y_true, tf.float32|tf.int64)。这意味着您可以将两者都转换为int64float32。两者都可以。

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 2021-01-06
      • 2019-08-14
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多