Unknown loss function: sparse_catecorical_crossentropy

Issue

I have been learning about machine learning and deep neural networks for a while and I only started using Keras and TensorFlow recently. I’m trying to build a digit-recognizing neural network, but I’m facing this error:

import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.layers import  Dense
from tensorflow import keras
(Xtrain,Ytrain) ,(Xtest,Ytest)=mnist.load_data()
model=tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation=tf.nn.softmax))
model.compile(optimizer='adam',loss='sparse_catecorical_crossentropy',metrics=['accuracy'])
model.fit(Xtrain,Ytrain,epochs=3)

And this is the error I’m having:

Traceback (most recent call last):
  File "C:\Users\amelbarouni\Desktop\python yassinne\ppyytthhoonn\mlkjj.py", line 12, in <module>
    model.fit(Xtrain,Ytrain,epochs=3)
  File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\framework\func_graph.py", line 1147, in autograph_handler
    raise e.ag_error_metadata.to_exception(e)
ValueError: in user code:

    File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1010, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 860, in train_step
        loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\training.py", line 918, in compute_loss
        return self.compiled_loss(
    File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\compile_utils.py", line 184, in __call__
        self.build(y_pred)
    File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\compile_utils.py", line 133, in build
        self._losses = tf.nest.map_structure(self._get_loss_object, self._losses)
    File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\compile_utils.py", line 272, in _get_loss_object
        loss = losses_mod.get(loss)
    File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\losses.py", line 2369, in get
        return deserialize(identifier)
    File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\losses.py", line 2324, in deserialize
        return deserialize_keras_object(
    File "C:\Users\amelbarouni\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\utils\generic_utils.py", line 709, in deserialize_keras_object
        raise ValueError(

    ValueError: Unknown loss function: sparse_catecorical_crossentropy. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.

Any ideas how to fix it?

Solution

just correct the string name to "sparse_categorical_crossentropy" then it will work. There is spelling error

Answered By – Aman Vishnoi

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published