as_strided

paddle. as_strided ( x, shape, stride, offset=0, name=None ) [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.

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) – Define the target Tensor’s offset from x’s holder. Default: 0.

  • name (str, 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].