PyTorch .exp2()

arisdelaCruz1413618857's avatar
Published Aug 20, 2025
Contribute to Docs

The .exp2() function in PyTorch computes the base-2 exponential of each element in the input tensor. This function is useful for various mathematical operations and can be applied to tensors of any shape. The result is a new tensor with the same shape as the input, containing the base-2 exponentials of the input values.

  • 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

# Function syntax:
result = torch.exp2(input, *, out=None)

# Tensor method syntax:
result = tensor.exp2()

This function is an alias for:

torch.special.exp2(input, *, out=None)

Parameters:

  • input (Tensor): The input tensor
  • out (Tensor, optional): The output tensor to store the result

Return value:

The .exp2() function returns a tensor with the base-2 exponentials of the input tensor, same shape as the input.

Example

Here’s an example of how to use the .exp2() function in PyTorch:

import torch
# Create a tensor
x = torch.tensor([1.0, 2.0, 3.0])
# Compute the base-2 exponential
y = x.exp2()
print(y)

The output will be:

tensor([2., 4., 8.])

In this example, the .exp2() function is applied to the tensor x, and the result is stored in the tensor y. The output will be a tensor containing the base-2 exponentials of the elements in x.

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