What is the difference between SparseCategoricalCrosstentropy and sparse_categorical_crossentropy ?

SparseCategoricalCrossentropy: Computes the crossentropy loss between the labels and predictions.

sparse_categorical_crossentropy: Computes the sparse categorical crossentropy loss.

But I am still not sure. Any loss will always be calculates between labels and predictions. SO how are these two different ?


3 Answers 3


SparseCategoricalCrossentropy is a class. So you have to define a object first then you can compute the loss using it.

scce = tf.keras.losses.SparseCategoricalCrossentropy()
scce(y_true, y_pred).numpy()

While sparse_categorical_crossentropy is merely a function which can be directly used to compute cost.

loss = tf.keras.losses.sparse_categorical_crossentropy(y_true, y_pred)

If you are to pass the loss to a Sequential API then you must pass the object ,not the function.

model.compile('sgd', loss=tf.keras.losses.SparseCategoricalCrossentropy())
  • 1
    $\begingroup$ so value of scce(y_true, y_pred).numpy() should be equal to tf.keras.losses.sparse_categorical_crossentropy(y_true, y_pred), right ? $\endgroup$ Commented Jul 10, 2020 at 8:04
  • $\begingroup$ yes it will be same $\endgroup$
    – SrJ
    Commented Jul 10, 2020 at 8:34

SparseCategoricalCrossentropy is a class while sparse_categorical_crossentropy is a function.

SparseCategoricalCrossentropy -> You create an instance of this class and then pass the true values and predicted values.

sparse_categorical_crossentropy -> You pass the true and predicted values just as you would do with any other function.


Apart from the difference between being function and class, the instance from tf.keras.losses.SparseCategoricalCrossentropy allows to be invoked with argument sample_weight. It would be rather convenient if you need to assign samples with different weights. More details can be found https://keras.io/api/losses/#standalone-usage-of-losses.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.