logit-Laplace loss

ML Reading Group Discussion
Posts: 150
Joined: Sun Apr 17, 2022 4:57 pm

logit-Laplace loss

Post by henrywu »

Logit-Laplace loss is a novel loss function proposed by OpenAI researchers in their paper(dVAE). It's designed as an alternative to the commonly used Gaussian-based reconstruction loss.

image.png (206.85 KiB) Viewed 257 times

Key Points:

  • Motivation: The logit-Laplace loss aims to address the limitations of Gaussian assumptions in modeling the distribution of pixel intensities, especially when dealing with images that exhibit sharp contrasts and edges.
  • Distribution: The logit-Laplace distribution is derived by applying the logistic function (sigmoid) to a Laplace distribution. This transformation leads to a distribution that better captures the heavy-tailed nature of pixel intensity values.
  • Formulation: The logit-Laplace loss measures the negative log-likelihood of the reconstructed pixel values under the logit-Laplace distribution. It's calculated by taking the logarithm of the probability density function of the logit-Laplace distribution.
  • Benefits:
    • Better modeling of pixel intensities with heavy tails.
    • Improved reconstruction quality, particularly for images with sharp edges.
    • Potential for more realistic image generation in VAEs.

Further Clarifications:

  • The logit-Laplace distribution is not the same as the log-Laplace distribution mentioned in your previous question. The logit-Laplace distribution is a specific transformation of the Laplace distribution tailored for modeling pixel intensities.
  • While the logit-Laplace loss has shown promising results, it might require some tuning of hyperparameters (e.g., scale parameter of the Laplace distribution) for optimal performance in specific applications.

Here's a demo PyTorch code demonstrating the logit-Laplace loss function, along with explanations and considerations:

Code: Select all

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Laplace

class LogitLaplaceLoss(nn.Module):
    def __init__(self, scale=0.1):  # Adjust 'scale' for your specific data
        super(LogitLaplaceLoss, self).__init__()
        self.scale = scale

    def forward(self, recon_x, x):
        recon_x: Reconstructed pixel values (logits) from VAE decoder
        x: Original pixel values (in range [0, 1])
        # Convert pixel values to logits
        x = torch.logit(x.clamp(min=1e-7, max=1-1e-7))  # Avoid log(0) or log(1)
        # Create Laplace distribution for each pixel
        laplace = Laplace(loc=recon_x, scale=self.scale)
        # Calculate negative log-likelihood (NLL) under Laplace distribution
        nll = -laplace.log_prob(x)  
        # Average NLL over all pixels
        loss = nll.mean()  

        return loss

# Example Usage:
recon_image = torch.randn(1, 3, 256, 256)  # Example reconstructed image
original_image = torch.rand(1, 3, 256, 256)  # Example original image

criterion = LogitLaplaceLoss()
loss = criterion(recon_image, original_image)

print("Logit-Laplace Loss:", loss.item())

Key Points and Considerations:

  • torch.logit: Converts pixel values to logits (inverse of sigmoid).
  • Laplace Distribution: Creates a Laplace distribution for each pixel with the reconstructed logit as the mean and the specified scale.
  • Scale Parameter: The scale parameter controls the spread of the Laplace distribution. Tuning this is important to match the characteristics of your dataset.
  • Clamping: Ensures that the pixel values stay within a valid range for the logit function to prevent numerical errors.
  • Loss Averaging: The average negative log-likelihood is computed across all pixels to obtain the final loss value.

When to Use:

  • Consider using logit-Laplace loss if your image data exhibits sharp contrasts or edges, or if you find that Gaussian-based reconstruction losses don't produce satisfactory results.


  • As with any loss function, use optimization algorithms (e.g., Adam, SGD) to minimize the loss during VAE training.