Search code examples
pythontensorflowkerasloss-function

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


I am implementing the custom loss function and my code is as below:

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

But I am getting the below error

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 

I tried to to K.cast for int64 but still I am getting the same error. Can someone help me in understanding reason behind this error and how to resolve this


Solution

  • The problem is withing the count tensor as its type is tf.int64 by default according to the official documentation here.

    You can solve this issue by setting the tensor type like so:

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