VOOZH about

URL: https://www.geeksforgeeks.org/deep-learning/loss-function-in-tensorflow/

⇱ Loss Function in TensorFlow - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Loss Function in TensorFlow

Last Updated : 23 Jul, 2025

Loss function compute errors between the predicted output and actual output. The optimizer then updates the model parameters based on the loss value to improve accuracy.

Mathematically, a loss function is represented as:

TensorFlow provides various loss functions under the tf.keras.losses module, which are widely used for different types of tasks such as regression, classification, and ranking.

Loss Functions for Regression

Regression problems involve continuous numerical predictions. Some commonly used loss functions include:

Mean Squared Error (MSE)

Mean Squared Error (MSE) calculates the average squared difference between the actual and predicted values. It is defined as:

TensorFlow implementation using tf.keras.losses.MeanSquaredError():

Output:

MSE Loss: 0.15166667

Mean Absolute Error (MAE)

Mean Absolute Error (MAE)computes the average absolute difference between the actual and predicted values:

TensorFlow implementation using tf.keras.losses.MeanAbsoluteError():

Output:

MAE Loss: 0.35

Loss Functions for Classification

Classification problems involve categorical outputs. Common loss functions include:

Binary Crossentropy

Binary crossentropy is used for binary classification, this loss function measures the difference between two probability distributions:

TensorFlow implementation using tf.keras.losses.BinaryCrossentropy():

Output:

Binary Crossentropy Loss: 0.16425204

Categorical Crossentropy

Categorical Crossentropy is used for multi-class classification. Categorical crossentropy extends BCE for multiple classes:

TensorFlow implementation using tf.keras.losses.CategoricalCrossentropy():

Output:

Categorical Crossentropy Loss: 0.20398414

Sparse Categorical Crossentropy

Sparse Categorical Crossentropy is used for classification tasks where labels are integers instead of one-hot encoded vectors.

TensorFlow implementation using tf.keras.losses.SparseCategoricalCrossentropy():

Output:

Sparse Categorical Crossentropy Loss: 0.2039842

Loss Functions for Specialized Tasks

TensorFlow also provides loss functions for specific use cases:

Huber Loss

Huber loss is the combination of MSE and MAE, useful for handling outliers. Huber loss for a single data point is defined as:

In TensorFlow, we can use tf.keras.losses.Huber() to implement Huber loss:

Output:

Huber Loss: 0.21875

Kullback-Leibler Divergence (KL Divergence)

KL Divergence measures how one probability distribution diverges from a second, expected probability distribution. It is commonly used in tasks involving probability distributions, such as classification or generative models.

KL Divergence between two probability distributions and is computed as:

In TensorFlow, KL Divergence can be used with tf.keras.losses.KLDivergence():

Output:

KL Divergence Loss: 0.03669001

Loss functions are the backbone of deep learning model training, guiding optimization towards accurate predictions. TensorFlow provides a variety of built-in loss functions for different tasks:

  • Regression: MeanSquaredError(), MeanAbsoluteError(), Huber()
  • Classification: BinaryCrossentropy(), CategoricalCrossentropy(), SparseCategoricalCrossentropy()
  • Other: KLDivergence(), CosineSimilarity(), etc.

Understanding and selecting the right loss function for your problem is crucial for achieving optimal performance.

Comment