reduce_as

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

Computes the sum of tensor elements make the shape of its result equal to the shape of target.

Parameters
  • x (Tensor) – An N-D Tensor, the data type is bool, float16, float32, float64, int8, uint8, int16, uint16, int32, int64, complex64 or complex128.

  • target (Tensor) – An N-D Tensor, the length of x shape must greater than or equal to the length of target shape. The data type is bool, float16, float32, float64, int8, uint8, int16, uint16, int32, int64, complex64 or complex128.

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

Returns

The sum of the input tensor x along some axis has the same shape as the shape of the input tensor target, if x.dtype=’bool’, x.dtype=’int32’, it’s data type is ‘int64’, otherwise it’s data type is the same as x.

Return type

Tensor

Examples

>>> import paddle

>>> x = paddle.to_tensor([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> x
Tensor(shape=[2, 4], dtype=int64, place=Place(gpu:0), stop_gradient=True,
[[1, 2, 3, 4],
 [5, 6, 7, 8]])
>>> target = paddle.to_tensor([1, 2, 3, 4])
>>> target
Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True,
[1, 2, 3, 4])
>>> res = paddle.reduce_as(x, target)
>>> res
Tensor(shape=[4], dtype=int64, place=Place(gpu:0), stop_gradient=True,
[6 , 8 , 10, 12])