.as_strided()
Published Mar 8, 2025
Contribute to Docs
In PyTorch, the .as_strided()
function creates a view of a tensor with a specified shape and strides. Unlike operations that copy data, .as_strided()
allows modifying how the tensor accesses memory, which enables efficient slicing, reshaping, and advanced indexing without additional memory allocation.
Note: Since
.as_strided()
manipulates tensor memory layout directly, incorrect stride values can lead to unexpected behavior or memory overlap.
Syntax
torch.as_strided(input, size, stride, storage_offset=None)
input
: The input tensor.size
: The desired shape of the output tensor.stride
: A tuple specifying the step size to move across dimensions.storage_offset
(Optional): Defines the starting point in the underlying storage for the output tensor. If set toNone
, the output tensor retains the samestorage_offset
as the input tensor.
Example
The following example demonstrates how .as_strided()
can be used to create a sliding window view of a tensor:
import torch# Create a 1D tensortensor = torch.arange(10)# Create a 2x3 strided view (overlapping windows)windowed_tensor = tensor.as_strided((2, 3), (2, 1))# Print the resultant tensorprint(windowed_tensor)
This code generates the output as:
tensor([[0, 1, 2],[2, 3, 4]])
- The original tensor
tensor
contains values[0, 1, 2, ..., 9]
. - The
.as_strided()
function generates a view where:- The new shape is
(2, 3)
, meaning two rows and three columns. - The first stride is
2
, meaning each row starts 2 elements ahead in the original tensor. - The second stride is
1
, meaning elements within each row are consecutive.
- The new shape is
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Free course
Intro to PyTorch and Neural Networks
Learn how to use PyTorch to build, train, and test artificial neural networks in this course.Intermediate3 hours