PyTorch .lerp()

alessandrocapialbi's avatar
Published Oct 29, 2025

In PyTorch, the .lerp() function computes the linear interpolation between an input tensor (input) and an end tensor (end), using a scalar or tensor weight. This is mathematically equivalent to applying the function $out_i = start_i + weight_i * (end_i - start_i)$.

The shapes of input, end, and weight must be broadcastable.

  • 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.lerp(input, end, weight, *, out=None)

Parameters:

  • input: The input tensor containing the initial points.
  • end: The ending tensor containing the finishing points.
  • weight: The shapes of input, end, and weight must be broadcastable.
  • out (optional): A tensor to store the output. If provided, the result is written to this tensor.

Return value:

Returns a new tensor containing the result given by the interpolation formula.

Example

The following example shows how to compute the interpolation between two tensors using torch.lerp() with a float scalar weight:

import torch
import math
# Define two tensors
start = torch.tensor([12.0 , 14.0 , 16.0 , math.log(2.)])
end = torch.tensor([11.0 , 13.0 , 15.0 , math.log(2.)])
# Compute the interpolation with a float weight
out = torch.lerp(start, end, 0.8)
print(out)

Here is the output:

tensor([11.2000, 13.2000, 15.2000, 0.6931])

All contributors

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