PyTorch .exp()

Anonymous contributor's avatar
Anonymous contributor
Published Jul 15, 2025
Contribute to Docs

In PyTorch, the .exp() function computes the exponential of each element in the input tensor. This is mathematically equivalent to applying the function $y_i = e^{x_i}$ element-wise, where e is Euler’s number (approximately 2.71828).

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

Parameters:

  • input: The input tensor containing elements for which the exponential will be computed.
  • out (optional): A tensor to store the output. If provided, the result is written to this tensor.

Return value:

Returns a new tensor where each element is the exponential of the corresponding element within the input tensor.

Example

In this example, we compute the element-wise exponential of a tensor using torch.exp():

import torch
import math
# Define a tensor
x = torch.tensor([0.0 , 1.0 , 2.0 , math.log(2.) ])
# Compute the exponential
result = torch.exp(x)
print(result)

Here is the output:

tensor([1.0000 , 2.7183, 7.3891 , 2. ])

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