expand_as

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

Expand the input tensor x to the same shape as the input tensor y.

Both the number of dimensions of x and y must be less than or equal to 6, and the number of dimensions of y must be greater than or equal to that of x. The dimension to expand must have a value of 0.

The following diagram illustrates how a one-dimensional tensor is transformed into a tensor with a shape of [2,3] through the expand_as operation. The target tensor has a shape of [2,3], and through expand_as, the one-dimensional tensor is expanded into a tensor with a shape of [2,3].

expand_as API
Parameters
  • x (Tensor) – The input tensor, its data type is bool, float32, float64, int32 or int64.

  • y (Tensor) – The input tensor that gives the shape to expand to.

  • name (str|None, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name.

Returns

N-D Tensor, A Tensor with the same shape as y. The data type is the same as x.

Examples

>>> import paddle

>>> data_x = paddle.to_tensor([1, 2, 3], 'int32')
>>> data_y = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], 'int32')
>>> out = paddle.expand_as(data_x, data_y)
>>> print(out)
Tensor(shape=[2, 3], dtype=int32, place=Place(cpu), stop_gradient=True,
[[1, 2, 3],
 [1, 2, 3]])