Meet Gondaliya
Meet Gondaliya

Reputation: 435

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

Error:

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

This error comes between image type and label type. From the above image type is int64 while the label type is float32. You can look at my notebook for more details. I can't understand what the problem is as I am new in this field.

Upvotes: 3

Views: 9817

Answers (2)

Zakariya
Zakariya

Reputation: 777

use y_true = tf.cast(y_true, tf.float32) just before x = y_true[:, :, 4 * num_classes :] - y_pred.

Upvotes: 1

Meet Gondaliya
Meet Gondaliya

Reputation: 435

This error comes from the line x = y_true[:, :, 4*num_classes:] - y_pred.

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

y_pred has dtype = 'float32'

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

The error comes when you try to subtract dtype('float32') from dtype('int64')

To solve this you need to change the datatype of both the variable. In my case y_pred and y_true are 4D tensors so you need tf.cast(y_pred|y_true, tf.float32|tf.int64). This means you can either convert both to int64 or float32. Both will work.

Upvotes: 3

Related Questions