PyTorch .log1p()
In PyTorch, .log1p() computes the natural logarithm of one plus the input tensor element-wise.
This function improves numerical stability for small input values, avoiding issues that can occur when computing the logarithm directly.
Syntax
torch.log1p(input, *, out=None)
Parameters:
input: The input tensor for which the natural logarithm of one plus the elements will be computed.out(Optional): A tensor to store the result. If provided, the computed values will be written to this tensor instead of creating a new one.
Return value:
Returns a new tensor where each element is the natural logarithm of one plus the corresponding element in the input tensor. The original tensor remains unchanged unless an out tensor is provided.
Example
In this example, .log1p() computes the natural logarithm of one plus each element in a tensor:
import torch# Create a tensorx = torch.tensor([0.0, 1.0, 2.0])# Compute log1py = torch.log1p(x)print(y)
The code above generates the output as follows:
tensor([0.0000, 0.6931, 1.0986])
In this example, the input tensor x contains the values [0.0, 1.0, 2.0]. The log1p() function computes the natural logarithm of one plus each element in the tensor, resulting in the output tensor [0.0000, 0.6931, 1.0986].
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
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn how to use PyTorch to build, train, and test artificial neural networks in this course.
- Intermediate.3 hours