fn

broadcast_to

Tensor
broadcast_to(input: Tensor, shape: ShapeLike)
source

Broadcast a tensor to a target shape as a view.

Performs the standard right-aligned broadcasting rules: dimensions of size 1 in the input may be expanded to any size in shape; new leading dimensions may be prepended. No data is copied — strides for the broadcast axes are set to 0.

Parameters

inputTensor
Source tensor.
shapeShapeLike
Target shape. Must be broadcast-compatible with input.shape.

Returns

Tensor

A view sharing storage with input.

Notes

Because broadcast axes have stride 0, writing into the result will alias multiple logical locations to the same physical element; prefer expand (also a view) or call contiguous first to materialise a copy.

Examples

>>> import lucid
>>> x = lucid.tensor([1.0, 2.0, 3.0])
>>> lucid.broadcast_to(x, (2, 3))
Tensor([[1., 2., 3.],
        [1., 2., 3.]])