PyTorch .log1p()

arisdelaCruz1413618857's avatar
Published Oct 29, 2025
Contribute to Docs

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.

  • 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.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 tensor
x = torch.tensor([0.0, 1.0, 2.0])
# Compute log1p
y = 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].

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