fn

reshape

Tensor
reshape(input: Tensor, shape: int | Sequence[int] = ())
source

Reshape a tensor to shape, returning a view when possible.

The total number of elements must match (shape=numel\prod \text{shape} = \text{numel}). Unlike view, reshape may allocate a new contiguous buffer when the requested layout cannot be expressed by stride manipulation alone (e.g. after a non-contiguous transpose). Exactly one entry may be -1, inferred from the remaining sizes.

Parameters

inputTensor
Source tensor.
shapeint or sequence of int= ()
Target shape. Exactly one entry may be -1 (auto-inferred).

Returns

Tensor

Tensor of the requested shape. View when feasible, otherwise a copy.

Notes

For the strict no-copy contract, use view, which raises if the layout cannot be expressed as a view. reshape's flexibility makes it the safer default for general user code.

Examples

>>> import lucid
>>> x = lucid.arange(12)
>>> lucid.reshape(x, (3, 4)).shape
(3, 4)