PyTorch .logit()
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().
Syntax
torch.logit(input, eps=None, *, out=None)
Parameters:
input(Tensor): The input tensor, where each element should be in the range(0, 1)whenepsis not provided.eps(float, optional): A small value added for numerical stability. Values less thanepsare clamped toeps, and values greater than1 - epsare clamped to1 - 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 torchprobs = 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 torchx = torch.tensor([0.0, 1.0])# Without eps: produces -inf and +infprint(torch.logit(x, eps=None))# With eps: clamps input to [eps, 1 - eps] before applying logitprint(torch.logit(x, eps=1e-6))
The output of this code is:
tensor([-inf, inf])tensor([-13.8155, 13.8023])
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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