Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

PyTorch · Function

torch.exp

Computes the elementwise exponential of a tensor. It can optionally write the result to a supplied output tensor.

How it works

torch.exp applies the exponential function element by element, producing a tensor whose values satisfy yᵢ = eˣᵢ. You can optionally provide an output tensor through the keyword-only out argument.

Syntax

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

Parameters

NameTypeDescription
inputTensorThe input tensor whose elements are exponentiated.
outTensor, optionalOptional output tensor that receives the result.

Returns

A new Tensor containing the elementwise exponential of input, or the supplied output tensor when out is used.

Key points

  • The function creates a result tensor containing the exponential of each input element.
  • The required input is a PyTorch Tensor.
  • An optional destination tensor can receive the computed values through out.
  • A tensor containing 0 and log(2) demonstrates outputs of 1 and 2.

Common mistakes

  • If supplied, out must be a tensor intended to receive the result; use it as a keyword argument.

Example: Compute elementwise exponentials

import math
import torch

x = torch.tensor([0.0, math.log(2.0)])
y = torch.exp(x)
print(y)  # tensor([1., 2.])

Computes the exponential independently for each element of the input tensor.

When to use it

Elementwise exponential transforms

Transforming logits or other tensor values into positive exponential-scale quantities.

Recovering values from natural logarithms

Constructing values such as powers of e from tensorized logarithms.

Destination-buffer computation

Writing results into an existing tensor buffer when an output destination is available.

Interview questions

QWhat does torch.exp compute?

torch.exp returns a new tensor containing the exponential of every element in the input. For each input value xᵢ, the corresponding output is eˣᵢ.

Follow-up: How can the result be written into a preallocated tensor? Pass that tensor with the keyword-only out argument.

QWhat are torch.exp's parameter and return types?

The input parameter is a Tensor, and the function returns a Tensor. The operation is pointwise, so the output has the corresponding elementwise exponential values.

Follow-up: What optional argument is available? The documentation exposes out, an optional Tensor used as the output destination.

QWhat is the function signature?

Its signature is torch.exp(input, *, out=None) → Tensor. The asterisk means out is keyword-only, while input is the tensor being transformed.

Follow-up: What mathematical relationship does the operation implement? Each output element follows yᵢ = eˣᵢ.

  • torch.exp_
  • torch.Tensor.exp
  • torch.special.exp2
  • torch.special.expm1

Source: torch.exp — PyTorch 2.12 documentation. Checked 2026-10-16.