PyTorch .ldexp()
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).
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 torchimport math# Define a tensorinput = torch.tensor([2.0, 4.0 , 5.0])# Define the tensor of exponentsother = torch.tensor([2.0, 3.0, 4.0])# Compute the multiplication between the two tensorsout = torch.ldexp(input, other)print(out)
Here is the output:
tensor([ 8., 32., 80.])
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve 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