TypeError: x and y must have the same dtype, got tf.float32 != tf.int64 in custom loss function keras

TypeError: x and y must have the same dtype, got tf.float32 != tf.int64 in custom loss function keras

我正在实现自定义损失函数,我的代码如下:

def seedloss(y_true,y_pred):
        count = tf.count_nonzero(y_pred)
        loss = K.log(y_pred)
        logval = tf.where(tf.is_inf(loss), tf.zeros_like(loss), loss)
        loss = -(K.sum(logval,axis=(1,2,3))/count)
        return loss

但我收到以下错误

Traceback (most recent call last):
  File "trainrefinenet.py", line 69, in <module>
    refinenet_model.compile(optimizer = Adam(lr=lr_init), loss = seedloss, metrics = [iou_coef])
  File "/home/ssindhu/deeplab_env/lib64/python3.6/site-packages/keras/engine/training.py", line 229, in compile
    self.total_loss = self._prepare_total_loss(masks)
  File "/home/ssindhu/deeplab_env/lib64/python3.6/site-packages/keras/engine/training.py", line 692, in _prepare_total_loss
    y_true, y_pred, sample_weight=sample_weight)
  File "/home/ssindhu/deeplab_env/lib64/python3.6/site-packages/keras/losses.py", line 71, in __call__
    losses = self.call(y_true, y_pred)
  File "/home/ssindhu/deeplab_env/lib64/python3.6/site-packages/keras/losses.py", line 132, in call
    return self.fn(y_true, y_pred, **self._fn_kwargs)
  File "/home/ssindhu/metrics.py", line 32, in seedloss
    loss = -(K.sum(logval,axis=(1,2,3),keepdims=True)/count)
  File "/home/ssindhu/deeplab_env/lib64/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 884, in binary_op_wrapper
    return func(x, y, name=name)
  File "/home/ssindhu/deeplab_env/lib64/python3.6/site-packages/tensorflow/python/ops/math_ops.py", line 982, in _truediv_python3
    (x_dtype, y_dtype))
TypeError: x and y must have the same dtype, got tf.float32 != tf.int64 

我尝试为 int64 K.cast,但我仍然遇到同样的错误。谁能帮我理解这个错误背后的原因以及如何解决这个问题

问题出在 count 张量上,因为根据官方文档 here,其类型默认为 tf.int64

您可以通过像这样设置张量类型来解决这个问题:

count = tf.count_nonzero(np.array([1, 2, 0]), dtype=tf.float32)