xbe.at

Deep learning / Activation functions and loss

Common Loss Functions for Deep Learning Using Python

4 min read, 12 sections

Contents
  1. Introduction to Loss Functions in Deep Learning
  2. Regression Loss Functions
  3. Mean Absolute Error (MAE)
  4. Mean Squared Error (MSE)
  5. Huber Loss
  6. Classification Loss Functions
  7. Binary Cross-Entropy Loss
  8. Categorical Cross-Entropy Loss
  9. Sparse Categorical Cross-Entropy Loss
  10. Custom Loss Functions
  11. Loss Function Selection
  12. Additional Resources

Introduction to Loss Functions in Deep Learning

In deep learning, loss functions play a crucial role in training models by quantifying the difference between the predicted output and the actual target. They guide the optimization process by providing a measure of how well the model is performing. Different types of problems require different loss functions, which are designed to handle specific characteristics of the output data.

Regression Loss Functions

Regression loss functions are used when the target variable is continuous, and the model aims to predict a numerical value. These functions measure the difference between the predicted and true values.

Code:

import numpy as np

# Mean Absolute Error (MAE)
true_values = np.array([1, 2, 3, 4, 5])
predicted_values = np.array([1.2, 2.1, 2.9, 4.1, 4.8])
mae = np.mean(np.abs(true_values - predicted_values))
print(f"Mean Absolute Error: {mae}")

Mean Absolute Error (MAE)

The Mean Absolute Error (MAE) is the average of the absolute differences between the predicted and true values. It is less sensitive to outliers compared to the Mean Squared Error (MSE) and provides an intuitive understanding of the average error magnitude.

Code:

import numpy as np

true_values = np.array([1, 2, 3, 4, 5])
predicted_values = np.array([1.2, 2.1, 2.9, 4.1, 4.8])
mae = np.mean(np.abs(true_values - predicted_values))
print(f"Mean Absolute Error: {mae}")

Mean Squared Error (MSE)

The Mean Squared Error (MSE) is the average of the squared differences between the predicted and true values. It penalizes larger errors more heavily than smaller errors, making it more sensitive to outliers compared to MAE.

Code:

import numpy as np

true_values = np.array([1, 2, 3, 4, 5])
predicted_values = np.array([1.2, 2.1, 2.9, 4.1, 4.8])
mse = np.mean((true_values - predicted_values) ** 2)
print(f"Mean Squared Error: {mse}")

Huber Loss

The Huber Loss is a combination of MAE and MSE. It behaves like MAE for small errors and like MSE for large errors, providing a trade-off between robustness to outliers and sensitivity to large errors.

Code:

import numpy as np
from sklearn.metrics import huber_loss

true_values = np.array([1, 2, 3, 4, 5])
predicted_values = np.array([1.2, 2.1, 2.9, 4.1, 4.8])
huber = huber_loss(true_values, predicted_values)
print(f"Huber Loss: {huber}")

Classification Loss Functions

Classification loss functions are used when the target variable is categorical, and the model aims to predict the correct class or label. These functions measure the difference between the predicted probability distribution and the true distribution.

Binary Cross-Entropy Loss

The Binary Cross-Entropy Loss is used for binary classification problems, where there are only two possible classes (e.g., 0 and 1). It measures the performance of a model whose output is a probability value between 0 and 1.

Code:

import numpy as np

true_labels = np.array([0, 1, 0, 1])
predicted_probabilities = np.array([0.2, 0.9, 0.1, 0.7])
bce_loss = -(true_labels * np.log(predicted_probabilities) + (1 - true_labels) * np.log(1 - predicted_probabilities)).mean()
print(f"Binary Cross-Entropy Loss: {bce_loss}")

Categorical Cross-Entropy Loss

The Categorical Cross-Entropy Loss is used for multi-class classification problems, where there are more than two possible classes. It measures the performance of a model whose output is a probability distribution over all classes.

Code:

import numpy as np

true_labels = np.array([[0, 1, 0], [1, 0, 0], [0, 0, 1]])
predicted_probabilities = np.array([[0.1, 0.7, 0.2], [0.6, 0.2, 0.2], [0.3, 0.3, 0.4]])
cce_loss = -(true_labels * np.log(predicted_probabilities)).sum(axis=1).mean()
print(f"Categorical Cross-Entropy Loss: {cce_loss}")

Sparse Categorical Cross-Entropy Loss

The Sparse Categorical Cross-Entropy Loss is a special case of the Categorical Cross-Entropy Loss, where the true labels are provided as integer indices instead of one-hot encoded vectors. It is computationally more efficient for multi-class classification problems.

Code:

import numpy as np
from keras.losses import sparse_categorical_crossentropy

true_labels = np.array([1, 0, 2])
predicted_probabilities = np.array([[0.1, 0.7, 0.2], [0.6, 0.2, 0.2], [0.3, 0.3, 0.4]])
scce_loss = sparse_categorical_crossentropy(true_labels, predicted_probabilities)
print(f"Sparse Categorical Cross-Entropy Loss: {scce_loss}")

Custom Loss Functions

In some cases, custom loss functions may be required to handle specific problem characteristics or incorporate domain knowledge. These functions can be defined by subclassing the keras.losses.Loss class or by creating a custom function.

Code:

import tensorflow as tf

class CustomLoss(tf.keras.losses.Loss):
    def call(self, y_true, y_pred):
        # Custom loss calculation
        loss = ...
        return loss

Loss Function Selection

Choosing the appropriate loss function is crucial for achieving optimal model performance. The selection depends on the problem type (regression or classification), the output distribution, and any specific requirements or assumptions. Sometimes, experimenting with different loss functions can lead to improved results.

Additional Resources

For further exploration and learning, here are some recommended resources:

  • "Pattern Recognition and Machine Learning" by Christopher Bishop
  • "Deep Learning" by Ian Goodfellow, Yoshua Bengio, and Aaron Courville
  • "Machine Learning Mastery" blog by Jason Brownlee
  • ArXiv papers:

These resources provide in-depth explanations, mathematical derivations, and practical examples of loss functions in deep learning.

More in Activation functions and loss

  1. Cross-Entropy Loss for Multiclass Classification5 min

    Cross-entropy loss is widely used for training neural networks in classification tasks. It measures the dissimilarity between predicted probability...

  2. Building a Sigmoid Activation Function from Scratch in Python7 min

    The sigmoid function is a crucial component in neural networks, particularly in binary classification problems. It maps any input value to a number between 0...

  3. Customizing Loss Functions for Precise Machine Learning Models8 min

    Custom loss functions are powerful tools in machine learning that allow developers to tailor the training process to specific project requirements. They...

  4. Building a ReLU Activation Function from Scratch in Python7 min

    The Rectified Linear Unit (ReLU) is a popular activation function in neural networks. It's simple yet effective, helping to solve the vanishing gradient...

  5. Discover Activation Functions in Neural Networks9 min

    The Rectified Linear Unit (ReLU) is one of the most widely used activation functions in modern neural networks. It transforms input by outputting zero for...

  6. Binary Cross-Entropy Limitations for Imbalanced Datasets7 min

    Binary cross-entropy is a popular loss function for binary classification tasks. However, it can be problematic when dealing with imbalanced datasets. This...