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

1

6

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.

Kokand answered 7/9, 2020 at 14:50 Comment(0)
R
13

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.

Riccio answered 7/9, 2020 at 15:15 Comment(2)
Thanks! Solved!Undulatory
yeah sometimes, its so confusingCretan

© 2022 - 2024 — McMap. All rights reserved.