PyTorch .logit()

Anonymous contributor's avatar
Anonymous contributor
Published Nov 16, 2025
Contribute to Docs

The torch.logit() function computes the logit (log-odds) of each element in the input tensor. The logit function is the inverse of the logistic sigmoid function, defined as:

$$\text{logit}(x) = \log\left(\frac{x}{1 - x}\right)$$

This operation is widely used in statistics and machine learning, particularly in logistic regression and neural network transformations. This function is an alias for torch.special.logit().

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

torch.logit(input, eps=None, *, out=None)

Parameters:

  • input (Tensor): The input tensor, where each element should be in the range (0, 1) when eps is not provided.
  • eps (float, optional): A small value added for numerical stability. Values less than eps are clamped to eps, and values greater than 1 - eps are clamped to 1 - eps.
  • out (Tensor, optional): The output tensor to store the result.

Return value:

Returns a tensor containing the logit transformation of the input values.

Example 1

In this example, probabilities are converted into logits and then passed through a sigmoid function to verify the inverse relationship:

import torch
probs = torch.tensor([0.2, 0.5, 0.8])
logits = torch.logit(probs)
recovered = torch.sigmoid(logits)
print("probs:", probs)
print("logits:", logits)
print("sigmoid(logits):", recovered)

Expected output (values may vary slightly due to precision):

probs: tensor([0.2000, 0.5000, 0.8000])
logits: tensor([-1.3863, 0.0000, 1.3863])
sigmoid(logits): tensor([0.2000, 0.5000, 0.8000])

Example 2

In this example, the eps parameter is used to prevent infinities when the input contains 0 or 1:

import torch
x = torch.tensor([0.0, 1.0])
# Without eps: produces -inf and +inf
print(torch.logit(x, eps=None))
# With eps: clamps input to [eps, 1 - eps] before applying logit
print(torch.logit(x, eps=1e-6))

The output of this code is:

tensor([-inf, inf])
tensor([-13.8155, 13.8023])

All contributors

Contribute to Docs

Learn PyTorch on Codecademy

  • Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.
    • Includes 27 Courses
    • With Professional Certification
    • Beginner Friendly.
      95 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours