pythontensorflowkerasloss-functioneager-execution

Tensorflow 2.0.0-beta1: 'EagerTensor object is not callable'


I am trying to implement custom training with Tensorflow and Keras API on the google colab. I use Tensorflow 2.0.0-beta1.

My code part for loss function is :

model = tf.keras.Sequential([
    tf.keras.layers.Embedding(
        max_features, 32, 
        embeddings_initializer='random_uniform'
    ),
    tf.keras.layers.SimpleRNN(32, kernel_initializer='random_uniform'),  
    tf.keras.layers.Dense(1, activation=tf.nn.sigmoid,),  # input shape is required
])

predictions = model(input_train)
predictions = tf.reshape(predictions,[25000,])

loss_object = tf.keras.losses.binary_crossentropy(
    y_true=y_train, 
    y_pred=predictions
)

def loss(model, x, y):
    y_ = model(x)

    return loss_object(y_true=y, y_pred=y_)

l = loss(model, input_train, y_train)

Which yelds this error:

 TypeError  Traceback (most recent call last) <ipython-input-17-675f7c1fd9d0> in <module>()
 return loss_object(y_true=y, y_pred=y_) 
 l = loss(model, input_train, y_train) 
 <ipython-input-17-675f7c1fd9d0> in 
 loss(model, x, y) y_ = model(x) 
return loss_object(y_true=y, y_pred=y_) l = loss(model, input_train, y_train) 
 TypeError: 'tensorflow.python.framework.ops.EagerTensor' object is not callable

Solution

  • You want to compute the loss of the model given the input x, target output y and the prediction y_. So the loss_object should be a loss function (and not a pre-computed loss) which you would use to compute the loss. Therefore, replace this:

    loss_object = tf.keras.losses.binary_crossentropy(y_true=y_train, y_pred=predictions)
    

    with this:

    loss_object = tf.keras.losses.binary_crossentropy