PyTorch .log()
Published Oct 30, 2025
In PyTorch, the .log() function computes the natural logarithm of each element in the input tensor. This is mathematically equivalent to applying the function $y_i = log_{e}(x_i)$ element-wise, where $log_{e}$ is the natural logarithm.
Syntax
torch.log(input, *, out=None) → Tensor
Parameters:
input: The input tensor containing elements for which the logarithm will be computed.out(optional): Output tensor to store the result. Must have the same shape asinput.
Return value:
Returns a new tensor where each element is the natural logarithm of the corresponding element within the input tensor.
Example
The following example shows how to compute the element-wise natural logarithm of a tensor using torch.log():
import torchimport math# Define a tensorx = torch.tensor([7.0 , 8.0 , 9.0 , math.log(3.)])# Compute the natural logarithmresult = torch.log(x)print(result)
Here is the output:
tensor([1.9459, 2.0794, 2.1972, 0.0940])
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