sum¶
对指定维度上的 Tensor 元素进行求和运算,并输出相应的计算结果。
参数¶
x (Tensor) - 输入变量为多维 Tensor,支持数据类型为 float32、float64、int32、int64。
axis (int|list|tuple,可选) - 求和运算的维度。如果为 None,则计算所有元素的和并返回包含单个元素的 Tensor 变量,否则必须在 \([−rank(x),rank(x)]\) 范围内。如果 \(axis [i] <0\),则维度将变为 \(rank+axis[i]\),默认值为 None。
dtype (str,可选) - 输出变量的数据类型。若参数为空,则输出变量的数据类型和输入变量相同,默认值为 None。
keepdim (bool) - 是否在输出 Tensor 中保留减小的维度。如 keepdim 为 true,否则结果张量的维度将比输入张量小,默认值为 False。
name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。
返回¶
Tensor
,在指定维度上进行求和运算的 Tensor,数据类型和输入数据类型一致。
代码示例¶
import paddle
# x is a Tensor with following elements:
# [[0.2, 0.3, 0.5, 0.9]
# [0.1, 0.2, 0.6, 0.7]]
# Each example is followed by the corresponding output tensor.
x = paddle.to_tensor([[0.2, 0.3, 0.5, 0.9],
[0.1, 0.2, 0.6, 0.7]])
out1 = paddle.sum(x) # [3.5]
out2 = paddle.sum(x, axis=0) # [0.3, 0.5, 1.1, 1.6]
out3 = paddle.sum(x, axis=-1) # [1.9, 1.6]
out4 = paddle.sum(x, axis=1, keepdim=True) # [[1.9], [1.6]]
# y is a Tensor with shape [2, 2, 2] and elements as below:
# [[[1, 2], [3, 4]],
# [[5, 6], [7, 8]]]
# Each example is followed by the corresponding output tensor.
y = paddle.to_tensor([[[1, 2], [3, 4]],
[[5, 6], [7, 8]]])
out5 = paddle.sum(y, axis=[1, 2]) # [10, 26]
out6 = paddle.sum(y, axis=[0, 1]) # [16, 20]
# x is a Tensor with following elements:
# [[True, True, True, True]
# [False, False, False, False]]
# Each example is followed by the corresponding output tensor.
x = paddle.to_tensor([[True, True, True, True],
[False, False, False, False]])
out7 = paddle.sum(x) # [4]
out8 = paddle.sum(x, axis=0) # [1, 1, 1, 1]
out9 = paddle.sum(x, axis=1) # [4, 0]