Why am I having this error? TypeError: Failed to convert object of type <class 'tensorflow.python.keras.losses.BinaryCrossentropy'> to Tensor

Question:

I’m practicing Dense Neural Network on Google Colab and had this error when doing model.fit.

This is the whole code:

I imported my data from Google drive and was able to passed in the data to panda.

import functools

import numpy as np
import pandas as pd
import tensorflow as tf
from tensorflow import keras

from google.colab import drive

drive.mount('/content/drive')
train_file_path =  "/content/drive/My Drive/Colab Notebooks/1111_train.csv"
test_file_path  =  "/content/drive/My Drive/Colab Notebooks/1111_test.csv"

df_train = pd.read_csv(train_file_path)
df_test = pd.read_csv(test_file_path)

I then sliced in inorder to create tensorslice

train_target = df_train.pop('22')
test_target = df_test.pop('22')

train_dataset = tf.data.Dataset.from_tensor_slices((df_train.values, train_target.values))
test_dataset = tf.data.Dataset.from_tensor_slices((df_test.values, test_target.values))


After this, I built my model

model = tf.keras.Sequential([
    tf.keras.layers.Dense(22, activation='relu'),
    tf.keras.layers.Dense(10, activation='relu'),
    tf.keras.layers.Dense(1)])

model.compile(optimizer='adam',
                loss = tf.keras.losses.BinaryCrossentropy,
                metrics=['accuracy'])

model.fit(train_dataset, epochs=15)

This is the whole error message when runnning model.fit.

WARNING:tensorflow:Layer dense_6 is casting an input tensor from dtype float64 to the layer's dtype of float32, which is new behavior in TensorFlow 2.  The layer has dtype float32 because it's dtype defaults to floatx.

If you intended to run this layer in float32, you can safely ignore this warning. If in doubt, this warning is likely only an issue if you are porting a TensorFlow 1.X model to TensorFlow 2.

To change all layers to have dtype float64 by default, call `tf.keras.backend.set_floatx('float64')`. To change just this layer, pass dtype='float64' to the layer constructor. If you are the author of this layer, you can disable autocasting by passing autocast=False to the base Layer constructor.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-d6feee4cfcc8> in <module>()
----> 1 model.fit(train_dataset, epochs=15)

10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/func_graph.py in wrapper(*args, **kwargs)
    966           except Exception as e:  # pylint_disable=broad-except
    967             if hasattr(e, "ag_error_metadata"):
--> 968               raise e.ag_error_metadata.to_exception(e)
    969             else:
    970               raise

TypeError: in user code:

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:571 train_function  *
        outputs = self.distribute_strategy.run(
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:951 run  **
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2290 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2649 _call_for_each_replica
        return fn(*args, **kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:533 train_step  **
        y, y_pred, sample_weight, regularization_losses=self.losses)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/compile_utils.py:205 __call__
        loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/losses.py:145 __call__
        losses, sample_weight, reduction=self._get_reduction())
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/losses_utils.py:104 compute_weighted_loss
        losses = ops.convert_to_tensor_v2(losses)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1283 convert_to_tensor_v2
        as_ref=False)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py:1341 convert_to_tensor
        ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:321 _constant_tensor_conversion_function
        return constant(v, dtype=dtype, name=name)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:262 constant
        allow_broadcast=True)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/constant_op.py:300 _constant_impl
        allow_broadcast=allow_broadcast))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_util.py:547 make_tensor_proto
        "supported type." % (type(values), values))

    TypeError: Failed to convert object of type <class 'tensorflow.python.keras.losses.BinaryCrossentropy'> to Tensor. Contents: <tensorflow.python.keras.losses.BinaryCrossentropy object at 0x7f76215279b0>. Consider casting elements to a supported type.
Asked By: Rudolf Pablico

||

Answers:

Nevermind. I found a solution.

I just change the loss function on model.compile

model.compile(optimizer='adam',
                loss = tf.keras.losses.binary_crossentropy,
                metrics=['accuracy'])
Answered By: Rudolf Pablico

Or you can try this

model.compile(optimizer='adam', loss=tf.keras.losses.BinaryCrossentropy(), metrics=['accuracy'])

Tensorflow objects—>

print(type(tf.keras.losses.BinaryCrossentropy()))
# <class 'tensorflow.python.keras.losses.BinaryCrossentropy'>

print(type(tf.keras.losses.BinaryCrossentropy))
# <class 'type'>

And what <class ‘type’> definition means, at wrong code?

When you call loss func without ‘()’ at the end; it is an unborn or uncalled class that possible to born, which is an one of ‘type’.

As you know Object is biggest thing in python. And everything has a type, as so; type is biggest thing in classes.

Object = God = Universe > Earth > PC > Python >= python3.6 >

Object >= type >= class > method > function

print(tf.keras.losses.BinaryCrossentropy().__class__)
# <class 'tensorflow.python.keras.losses.BinaryCrossentropy'>

print(tf.keras.losses.BinaryCrossentropy.__class__)
# <class 'type'>

But we can use functions inside that uncalled class independently or after called the class, if exist. Note that method > function here

class Foo:
    def foo(self):
        a = 5
        return a

print(type(Foo.foo))
# <class 'function'>

print(type(Foo().foo))
# <class 'method'>

If you add ‘()’ at the end of a function, it returns a result of this func

print(type(Foo().foo()))
# <class 'int'>
Answered By: bitbang

For me, I was receiving similar error, and the problem was at one line I used the loss with out setting its necessary two parameters, namely: the real labels y_real and the predictions y_pred.

The solution was to calculate the loss by calling the loss function and passing the two parameters and after that using the calculated value.

In other words, here is the code before solving the error:

grads = tape.gradient (train_loss, model.trainable_variables)

And here is the code after solving the issue:

loss = train_loss(y, predictions)
grads = tape.gradient (loss, model.trainable_variables)
Answered By: glitch

Your problem is where you compile your model:

model.compile(optimizer='adam',
                loss = tf.keras.losses.BinaryCrossentropy,
                metrics=['accuracy'])

tf.keras.losses.BinaryCrossentropy is a class, which you need to create an instance of:

>>> tf.keras.losses.BinaryCrossentropy
<class 'keras.losses.BinaryCrossentropy'>

If we instead create an instance thereof like this:

>>> tf.keras.losses.BinaryCrossentropy()
<keras.losses.BinaryCrossentropy object at 0x7faf0bba0550>

…we get an instance of tf.keras.losses.BinaryCrossentropy, not the class itself.

Practically, you need to change you compile() call to match this:

model.compile(optimizer='adam',
                loss = tf.keras.losses.BinaryCrossentropy(),
                metrics=['accuracy'])
Answered By: starbeamrainbowlabs