PyTorch .expm1()

Anonymous contributor's avatar
Anonymous contributor
Published Aug 19, 2025
Contribute to Docs

In PyTorch, the .expm1() function takes in an input tensor and returns a tensor where each element is the exponential of the input element minus 1. It provides greater precision than performing $$exp(x) - 1$$ directly, especially for small values of x. The function computes each element as:

$$y_i= e^{x_i}-1$$

  • 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.expm1(input, *, out=None) → Tensor

Or,

torch.special.expm1(input, *, out=None) → Tensor

Parameters:

  • input: The input tensor.
  • out (Optional): A tensor to store the output. If provided, the result is written to this tensor.

Return value:

It returns a new tensor of the same shape as the input, containing the computed values for each corresponding element.

Example

This example demonstrates the usage of the .expm1() function:

import torch
# Define a tensor
x = torch.tensor([0, math.log(2.)])
result = torch.special.expm1(x)
print(result)

The output of this code is:

tensor([ 0., 1.])

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