Skip to main content

How to Use Label Smoothing in TensorFlow

In this article, we learn how to use label smoothing using TensorFlow in a short tutorial complete with code and interactive visualizations so you can follow along.
Created on June 20|Last edited on January 20
In this article, we'll look at how you can use Label Smoothing in TensorFlow to help make your Tensorflow and Keras models more robust and prevent overfitting on your training data.
TensorFlow makes it very easy to use Label Smoothing in existing codebases which we can easily add to the codebase by just adding a parameter. Here's what we'll cover:

Table of Contents (click to expand)



NOTE: This Report assumes you know what Label Smoothing and only walks you through how you can use it in Tensorflow.
💡

For a theoretical understanding of how and why Label Smoothing works, you can refer to this video:



If you are curious about other Regularization methods, you can refer to these reports:


Show Me the Code

Let's say you have a pre-existing pipeline in TensorFlow which looks something like this:
model = ...

model.compile(..., loss=tf.keras.losses.CategoricalCrossentropy())

model.fit(...)
Tensorflow has a label_smoothing parameter in the CategoricalCrossentropy and BinaryCrossentropy loss function. You can specify the value to be used while instantiating the loss function, viz.
model = ...

model.compile(...,
loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing = 0.1)
)

model.fit(...)

Summary

In this article, you saw how you can use Label Smoothing in TensorFlow to help make your TensorFlow and Keras models more robust and prevent overfitting on your training data and how using Weights & Biases to monitor your metrics can lead to valuable insights.
To see the full suite of W&B features, please check out this short 5 minute guide. If you want more reports covering the math and from-scratch code implementations, let us know in the comments below or on our forum ✨!
Check out these other reports on Fully Connected covering other fundamental development topics like GPU Utilization and Saving Models.


Iterate on AI agents and models faster. Try Weights & Biases today.