Label Smoothing for sparse_categorical_crossentropy

Issue

Based on the Tensorflow Documentation, one can add label smoothing to categorical_crossentropy by adding label_smoothing argument. My question is what about sparse categorical crossentropy loss. There is no label_smoothing argument for this loss function.

Solution

It’s easy to write your own loss function:

from tensorflow.keras.losses import categorical_crossentropy

def scce_with_ls(y, y_hat):
    y = tf.one_hot(tf.cast(y, tf.int32), n_classes)
    return categorical_crossentropy(y, y_hat, label_smoothing = 0.1)

Answered By – Björn Lindqvist

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