sums

paddle.fluid.layers.sums(input, out=None)[source]

This function computes the sum of multiple input Tensors elementwisely.

  • Case 1, sum of 3 Tensors

# Input Tensors
x0.shape = [2, 3]
x0.data = [[1., 2., 3.],
           [4., 5., 6.]]
x1.shape = [2, 3]
x1.data = [[10., 20., 30.],
           [40., 50., 60.]]
x2.shape = [2, 3]
x2.data = [[100., 200., 300.],
           [400., 500., 600.]]

# Output Tensor
out.shape = [2, 3]
out.data = [[111., 222., 333.],
            [444., 555., 666.]]
Parameters
  • input (list) – A list of Variables which hold input Tensors with the same data type and shape. Optional data types are: float32, float64, int32, int64.

  • out (Variable, optional) – Output Tensor. It can be any existing Variable. The default value is None, then a new Variable will be created and returned.

Returns

The sum of inputs. The shape and data type is the same with input. If out is not None, the returned value is out .

Return type

Variable

Examples

import paddle.fluid as fluid

x0 = fluid.layers.fill_constant(shape=[16, 32], dtype='int64', value=1)
x1 = fluid.layers.fill_constant(shape=[16, 32], dtype='int64', value=2)
x2 = fluid.layers.fill_constant(shape=[16, 32], dtype='int64', value=3)
x3 = fluid.layers.fill_constant(shape=[16, 32], dtype='int64', value=0)

# Sum of multiple Tensors, the result is stored to a new Variable sum0 (sum0=x0+x1+x2, the value is [[6, ..., 6], ..., [6, ..., 6]])
sum0 = fluid.layers.sums(input=[x0, x1, x2])

# Sum of multiple Tensors, sum1 and x3 represents the same Variable (x3=x0+x1+x2, the value is [[6, ..., 6], ..., [6, ..., 6]])
sum1 = fluid.layers.sums(input=[x0, x1, x2], out=x3)