PyTorch .ldexp()

alessandrocapialbi's avatar
Published Oct 30, 2025
Contribute to Docs

In PyTorch, the .ldexp() function computes a new tensor, where each element of input is multiplied by 2 raised to the power of the corresponding element in other. It is mathematically equivalent to the function $out_i = input_i * 2^{other_i}$.

This operation is commonly used to build floating-point numbers by combining mantissas (from input) with powers of two derived from integer exponents (from other).

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

Parameters:

  • input: The input tensor containing the mantissas.
  • other: A tensor of exponents (int or float), applied element-wise.
  • out (Optional): The output tensor to store results. If not provided, a new tensor is returned.

Return value:

Returns a new tensor containing the result of the multiplication.

Example

This example computes the multiplication between an input tensor and another tensor using torch.ldexp():

import torch
import math
# Define a tensor
input = torch.tensor([2.0, 4.0 , 5.0])
# Define the tensor of exponents
other = torch.tensor([2.0, 3.0, 4.0])
# Compute the multiplication between the two tensors
out = torch.ldexp(input, other)
print(out)

Here is the output:

tensor([ 8., 32., 80.])

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