view_as

paddle. view_as ( x: Tensor, other: Tensor, name: str | None = None ) Tensor [source]

View x with other’s shape.

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

The following figure shows a view_as operation - a three-dimensional tensor with a shape of [2, 4, 6] is transformed into a two-dimensional tensor with a shape of [8, 6] through the view_as operation. We can clearly see the corresponding relationship between the elements before and after the transformation.

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

  • other (Tensor) – The result tensor has the same size as other.

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

Returns

Tensor, A viewed Tensor with the same shape as other.

Examples

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

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

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