torch.Tensor.Tensor.as_strided
Tensor.as_strided(size: readonly number[], stride: readonly number[], options?: AsStridedOptions): AnyTensorTensor.as_strided(size: readonly number[], stride: readonly number[], storage_offset: number, options?: AsStridedOptions): AnyTensorCreates a view of the 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 (on CPU).
- Advanced operation: Incorrect strides can cause undefined behavior.
- CPU only: Currently only supports CPU tensors.
Parameters
sizereadonly number[]- The desired shape of the output tensor
stridereadonly number[]- The stride for each dimension
optionsAsStridedOptionsoptional- Optional settings
Returns
AnyTensor– A new view of the tensor with the specified size and strideExamples
const x = torch.arange(9); // [0, 1, 2, 3, 4, 5, 6, 7, 8]
x.as_strided([2, 2], [3, 1]); // [[0, 1], [3, 4]]
// Create overlapping windows
const y = torch.arange(6);
y.as_strided([3, 2], [1, 1]); // [[0, 1], [1, 2], [2, 3]]See Also
- PyTorch torch.Tensor.as_strided()
- view - Simple reshape (contiguous required)
- reshape - Reshape with potential copy