view

paddle. view ( x, shape_or_dtype, name=None ) [source]

View x with specified shape or dtype.

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_or_dtype (list|tuple|np.dtype|str|VarType) – Define the target shape or dtype. If list or tuple, shape_or_dtype represents shape, each element of it should be integer. If np.dtype or str or VarType, shape_or_dtype represents dtype, it can be bool, float16, float32, float64, int8, int32, int64, uint8.

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

Returns

Tensor, A viewed Tensor with the same data as x.

Examples

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

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

>>> out = paddle.view(x, [8, 6])
>>> print(out.shape)
[8, 6]

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

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

>>> out = paddle.view(x, "uint8")
>>> print(out.shape)
[2, 4, 24]

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

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

>>> out = paddle.view(x, [8, -1])
>>> print(out.shape)
[8, 6]

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

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

>>> out = paddle.view(x, paddle.uint8)
>>> print(out.shape)
[2, 4, 24]