as_strided

paddle. as_strided ( x: Tensor, shape: Sequence[int], stride: Sequence[int], offset: int = 0, name: str | None = None ) Tensor [source]

View x with specified shape, stride and offset.

Note that the output Tensor will share data with origin Tensor and doesn’t have a Tensor copy in dygraph mode.

The following image illustrates an example: transforming an input Tensor with shape [2,4,6] into a Tensor with shape [8,6] and stride [6,1].

Legend
Parameters
  • x (Tensor) – An N-D Tensor. The data type is float32, float64, int32, int64 or bool

  • shape (list|tuple) – Define the target shape. Each element of it should be integer.

  • stride (list|tuple) – Define the target stride. Each element of it should be integer.

  • offset (int, optional) – Define the target Tensor’s offset from x’s holder. Default: 0.

  • name (str|None, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.

Returns

Tensor, A as_strided Tensor with the same data type as x.

Examples

>>> import paddle
>>> paddle.base.set_flags({"FLAGS_use_stride_kernel": True})

>>> x = paddle.rand([2, 4, 6], dtype="float32")

>>> out = paddle.as_strided(x, [8, 6], [6, 1])
>>> print(out.shape)
[8, 6]
>>> # the stride is [6, 1].