首页 > 解决方案 > TypeError:“Mul”Op 的输入“y”的类型为 float32,与参数“x”的类型 int64 不匹配

问题描述

在这段代码之后,我得到了 categoricalfocalloss 中的错误,我没有得到 wheret64 错误即将到来

def categorical_focal_loss(gamma=2., alpha=.25):
    def categorical_focal_loss_fixed(y_true, y_pred):
        y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
        epsilon = K.epsilon()
        y_pred = K.clip(y_pred, epsilon, 1. - epsilon)
        y_pred = tf.cast(y_pred, dtype= tf.float32)
        cross_entropy = -y_true * K.log(y_pred)
        loss = alpha * K.pow(1 - y_pred, gamma) * cross_entropy
        return K.sum(loss, axis=1)
    return categorical_focal_loss_fixed

此代码中的模型描述,在损失中使用了分类焦点损失

    with strategy.scope():
        ef7 =tf.keras.Sequential()
        ef7.add(enet)
        ef7.add(tf.keras.layers.MaxPooling2D())
        ef7.add(tf.keras.layers.Conv2D(4096,3,padding='same'))
        ef7.add(tf.keras.layers.BatchNormalization())
        ef7.add(tf.keras.layers.ReLU())
        ef7.add(tf.keras.layers.GlobalAveragePooling2D())
        ef7.add(tf.keras.layers.Dropout(0.35))
        ef7.add(tf.keras.layers.Flatten())
    
        ef7.add(tf.keras.layers.Dense(2048,activation='relu'))
        ef7.add(tf.keras.layers.BatchNormalization())
        ef7.add(tf.keras.layers.LeakyReLU())
        ef7.add(tf.keras.layers.Dropout(0.35))
    
        ef7.add(tf.keras.layers.Dense(1024,activation='relu'))
        ef7.add(tf.keras.layers.BatchNormalization())
        ef7.add(tf.keras.layers.LeakyReLU())
        ef7.add(tf.keras.layers.Dropout(0.25))
        ef7.add(tf.keras.layers.Dense(3,activation='softmax'))
        ef7.compile(
                    optimizer=tf.optimizers.Adam(lr=0.0001),
                    loss=categorical_focal_loss(gamma=2., alpha=.25),
                    metrics=['categorical_accuracy',
                            tf.keras.metrics.Recall(),
                            tf.keras.metrics.Precision(),   
                            tf.keras.metrics.AUC(),
                            tfa.metrics.F1Score(num_classes=3, average="macro")
                           ])

在模型中,当我运行它时,我使用了分类焦点损失,在训练数据集中我没有得到如何 tcovert itintointoint64

    h7=ef7.fit(
    train_dataset,
    steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
    callbacks=[lr_callback],
    epochs=EPOCHS)

下面提到了错误

        Epoch 1/20
    
    ```Epoch 00001: LearningRateScheduler reducing learning rate to 1e-05.```
    ---------------------------------------------------------------------------
    >TypeError                                 Traceback (most recent call last)
    <ipython-input-133-d27eee469b2b> in <module>()
          3     steps_per_epoch=train_labels.shape[0] // BATCH_SIZE,
          4     callbacks=[lr_callback],
    ----> 5     epochs=EPOCHS)
    
    9 frames
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
        975           except Exception as e:  # pylint:disable=broad-except
        976             if hasattr(e, "ag_error_metadata"):
    --> 977               raise e.ag_error_metadata.to_exception(e)
        978             else:
        979               raise
    
    TypeError: in user code:
    
        >/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function  *
            >return step_function(self, iterator)
        ><ipython-input-68-de42355e464e>:7 categorical_focal_loss_fixed  *
            cross_entropy = -y_true * K.log(y_pred)
        >/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:1180 binary_op_wrapper
            >raise e
        >/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:1164 binary_op_wrapper
            >return func(x, y, name=name)
        >/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:1496 _mul_dispatch
            >return multiply(x, y, name=name)
        >/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
            return target(*args, **kwargs)
        >/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/math_ops.py:518 multiply
            >return gen_math_ops.mul(x, y, name)
        >/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/gen_math_ops.py:6078 mul
         >   "Mul", x=x, y=y, name=name)
        >/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:558 _apply_op_helper
         >   inferred_from[input_arg.type_attr]))
    
        >TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int64 of argument 'x'.```

标签: pythontensorflowtypeerrorcategoriescross-entropy

解决方案


错误指向这行代码:

cross_entropy = -y_true * K.log(y_pred)

math_ops.py并从tensorflow 包中的 multiply 函数中抛出。深入研究该文件,我找到了参数要求的摘要。

 Args:
    x: A Tensor. Must be one of the following types: `bfloat16`,
      `half`, `float32`, `float64`, `uint8`, `int8`, `uint16`,
      `int16`, `int32`, `int64`, `complex64`, `complex128`.
    y: A `Tensor`. Must have the same type as `x`.
    name: A name for the operation (optional).
  Returns:
  A `Tensor`.  Has the same type as `x`.
  Raises:
   * InvalidArgumentError: When `x` and `y` have incompatible shapes or types

回头看看错误

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

这意味着-y_trueis'x'K.log(y_pred)is 'y'。要执行此操作,您必须转换-y_true为 float32 或K.log(y_pred) 转换为 int64 或将它们都转换为任何其他类型,只要它们匹配即可。.


推荐阅读