Skip to content
fn

lerp

→Tensor
lerp(input: Tensor, end: Tensor, weight: _float | Tensor)

Linearly interpolate between input and an endpoint with a weight.

Computes the affine combination

out  =  x+w⋅(end−x)  =  (1−w)⋅x+w⋅end,\text{out} \;=\; x + w \cdot (\text{end} - x) \;=\; (1 - w) \cdot x + w \cdot \text{end} ,

where ww is the interpolation weight. weight may be a scalar or a tensor broadcastable to input.

Parameters

inputTensor
Start point.

Returns

Tensor

Interpolated tensor of the broadcast shape.

Notes

With w = 0 the result equals input; with w = 1 it equals end; out-of-range weights extrapolate beyond the endpoints.

The value takes one of two forms by the weight, as the reference does: input + w * (end - input) for |w| < 0.5 and end - (end - input) * (1 - w) otherwise, so w = 1 gives end exactly and an infinite operand gives what its form gives (lerp(0, inf, 0.5) is NaN). The gradient ignores the form: grad * (1 - w) for input, grad * w for end and grad * (end - input) for w.

Examples

>>> import lucid
>>> a = lucid.tensor([0.0, 0.0, 0.0])
>>> b = lucid.tensor([1.0, 2.0, 4.0])
>>> lucid.lerp(a, b, 0.5)
Tensor([0.5, 1. , 2. ])