Custom Loss Functions Creation

MamtaWardhani's avatar
Published Feb 13, 2025
Contribute to Docs

Loss functions are a critical component in training deep learning models, as they quantify the difference between predicted and actual values, guiding the model’s learning process. While PyTorch provides several built-in loss functions like MSELoss, CrossEntropyLoss, and L1Loss, these may not always be suitable for specialized tasks.

Custom loss functions offer the flexibility to define domain-specific error calculations tailored to particular problems. PyTorch allows users to implement these functions by subclassing torch.nn.Module or defining a simple Python function that operates on tensors. These functions are particularly useful in scenarios where unique constraints, penalties, or weighted losses are required.

By creating a custom loss function, developers can enhance model performance and adapt it to specific applications such as imbalanced datasets, reinforcement learning, or multi-task learning.

Steps to create a Custom Loss Function in PyTorch

  • Define the Custom Loss Class: Create a class that inherits from nn.Module and includes a weight parameter in the constructor.
  • Implement the Forward Method: Inside the forward method, compute the loss using predicted (y_pred) and actual (y_true) tensors. This example uses a weighted mean squared error (MSE) loss, but the calculation can be customized.
  • Instantiate the Loss Function: Create an object of the class, passing the desired weight parameter.
  • Compute the Loss – Call the instantiated loss function with the predicted and target tensors to obtain the loss value.

Example

This code defines a custom weighted mean squared error (MSE) loss function in PyTorch, initializes it with a weight parameter, and computes the loss for given predicted and actual values:

import torch
import torch.nn as nn
# Step 1: Define the Custom Loss Class
class CustomLoss(nn.Module):
def __init__(self, weight):
super(CustomLoss, self).__init__()
self.weight = weight # Store the weight parameter
# Step 2: Implement the Forward Method
def forward(self, y_pred, y_true):
loss = self.weight * torch.mean((y_pred - y_true) ** 2) # Weighted MSE loss
return loss
# Step 3: Instantiate the Loss Function
custom_loss = CustomLoss(weight=0.5) # Example weight parameter
# Sample Data
y_pred = torch.tensor([3.0, 4.5, 2.1], requires_grad=True)
y_true = torch.tensor([3.2, 4.0, 2.0])
# Step 4: Compute the Loss
loss_value = custom_loss(y_pred, y_true)
# Print the loss
print("Custom Loss Value:", loss_value.item())

The code above produces the output as follows:

Custom Loss Value: 0.05000000074505806

This output represents the computed mean absolute error between the predicted and actual values.

All contributors

Contribute to Docs

Learn PyTorch on Codecademy