Why do these error messages show up in Compilation

Viewed 120

I am trying to compile the Keras model to train and test the dataset. But in the compilation process, the following error messages show up. Could anyone help me how to solve this? I have been checking other pages and followed their suggestions, but none of them really helps me to solve that.

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation="relu"), # Rectified Linear Unit.
    tf.keras.layers.Dense(10, activation="softmax")
model.compile(optimizer="adam", loss="sparse_categorial_crossentropy", metrics=["accuracy"])

The lines below appear when I try to compile and run.

Traceback (most recent call last):
  File "/home/eaindra/PycharmProjects/NeuralNetwork/Tensorflow1.py", line 42, in <module>
    model.compile(optimizer="adam", loss="sparse_categorial_crossentropy", metrics=["accuracy"])
  File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/training/tracking/base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training.py", line 336, in compile
    self.loss, self.output_names)
  File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 1351, in prepare_loss_functions
    loss_functions = [get_loss_function(loss) for _ in output_names]
  File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 1351, in <listcomp>
    loss_functions = [get_loss_function(loss) for _ in output_names]
  File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/training_utils.py", line 1087, in get_loss_function
    loss_fn = losses.get(loss)
  File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/losses.py", line 1183, in get
    return deserialize(identifier)
  File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/losses.py", line 1174, in deserialize
    printable_module_name='loss function')
  File "/home/eaindra/anaconda3/envs/tensor/lib/python3.6/site-packages/tensorflow_core/python/keras/utils/generic_utils.py", line 210, in deserialize_keras_object
    raise ValueError('Unknown ' + printable_module_name + ':' + object_name)
ValueError: Unknown loss function:sparse_categorial_crossentropy
1 Answers

It seems to be a typo on the loss function. You wrote categorial instead of categorical and missed the closing square brackets in the model definition. Fixed code segment attached below;

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.Dense(128, activation="relu"), # Rectified Linear Unit.
    tf.keras.layers.Dense(10, activation="softmax")])

model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
Related