Rate this Page

torch.as_strided#

torch.as_strided(input,size,stride,storage_offset=None)Tensor#

Create a view of an existingtorch.Tensorinput with specifiedsize,stride andstorage_offset.

Warning

Prefer using other view functions, liketorch.Tensor.view() ortorch.Tensor.expand(), to setting a view’s strides manually withas_strided, as this function will throw an error on non-standard Pytorchbackends (that do not have a concept of stride) and the result will dependon the current layout in memory. The constructed view must only refer toelements within the Tensor’s storage or a runtime error will be thrown.If the generated view is “overlapped” (with multiple indices referring tothe same element in memory), the behavior of inplace operations on this viewis undefined (and might not throw runtime errors).

Parameters
  • input (Tensor) – the input tensor.

  • size (tuple orints) – the shape of the output tensor

  • stride (tuple orints) – the stride of the output tensor

  • storage_offset (int,optional) – the offset in the underlying storage of the output tensor.IfNone, the storage_offset of the output tensor will match the input tensor.

Example:

>>>x=torch.randn(3,3)>>>xtensor([[ 0.9039,  0.6291,  1.0795],        [ 0.1586,  2.1939, -0.4900],        [-0.1909, -0.7503,  1.9355]])>>>t=torch.as_strided(x,(2,2),(1,2))>>>ttensor([[0.9039, 1.0795],        [0.6291, 0.1586]])>>>t=torch.as_strided(x,(2,2),(1,2),1)tensor([[0.6291, 0.1586],        [1.0795, 2.1939]])