Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: categorical_crossentropy() missing 2 required positional arguments: 'y_true' and 'y_pred'

Import libraries and models,

from __future__ import print_function
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
import keras.backend as k

batch_size = 128
num_classes = 10
epochs = 12

Below the written code,

#Loss and Optimizer
optimizer = keras.optimizers.Adam()
loss = keras.losses.categorical_crossentropy()

Below the type error, which I badly faced and i can't make the solution,

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-f3fea941b382> in <module>()
      1 #Loss and Optimizer
      2 optimizer = keras.optimizers.Adam()
----> 3 loss = keras.losses.categorical_crossentropy()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
    199     """Call target, and fall back on dispatchers if there is a TypeError."""
    200     try:
--> 201       return target(*args, **kwargs)
    202     except (TypeError, ValueError):
    203       # Note: convert_to_eager_tensor currently raises a ValueError, not a

TypeError: categorical_crossentropy() missing 2 required positional arguments: 'y_true' and 'y_pred'

Need help to solve this problem, please help me. Advanced thanks.

like image 661
Imdadul Haque Avatar asked Mar 03 '23 02:03

Imdadul Haque


1 Answers

This is the correct implementation for getting a Categorical Crossentropy class object.

loss = keras.losses.CategoricalCrossentropy()

keras.losses.categorical_crossentropy this is a function which requires 2 parameters.

like image 122
Aniket Bote Avatar answered Mar 05 '23 16:03

Aniket Bote