PyTorch .log()

alessandrocapialbi's avatar
Published Oct 30, 2025
Contribute to Docs

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.

  • 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

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 as input.

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 torch
import math
# Define a tensor
x = torch.tensor([7.0 , 8.0 , 9.0 , math.log(3.)])
# Compute the natural logarithm
result = torch.log(x)
print(result)

Here is the output:

tensor([1.9459, 2.0794, 2.1972, 0.0940])

All contributors

Contribute to 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