torch.as_strided
function as_strided(input: Tensor, size: readonly number[], stride: readonly number[], options?: AsStridedOptions): Tensorfunction as_strided(input: Tensor, size: readonly number[], stride: readonly number[], storage_offset: number, options?: AsStridedOptions): TensorCreates a view of the input tensor with the given size and stride.
This allows creating views that overlap or have gaps in the underlying storage. The resulting tensor shares storage with the input tensor.
Parameters
inputTensor- The input tensor
sizereadonly number[]- The desired shape of the output tensor
stridereadonly number[]- The stride for each dimension
optionsAsStridedOptionsoptional- Optional settings for as_strided
Returns
Tensor– A new view of the input tensorExamples
const x = torch.arange(9); // [0, 1, 2, 3, 4, 5, 6, 7, 8]
torch.as_strided(x, [2, 2], [3, 1]); // [[0, 1], [3, 4]]