add

paddle.sparse. add ( x, y, name=None ) [source]

Add two sparse tensors element-wise. Input x and y’s shape should be identical and have same sparse type(SparseCooTensor or SparseCsrTensor).If input is SparseCooTensor, x and y’s sparse_dim should be identical. The equation is:

\[out = x + y\]
Parameters
  • x (Tensor) – the input tensor, it’s data type should be float32, float64, int32, int64.

  • y (Tensor) – the input tensor, it’s data type should be float32, float64, int32, int64.

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

Returns

the result tensor.

Return type

Tensor

Examples:

>>> import paddle

>>> paddle.device.set_device("cpu")

>>> x = paddle.to_tensor([[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]], 'float32')
>>> y = paddle.to_tensor([[0, 0, 0, -2], [0, 2, -3, 0], [2, 3, 4, 8]], 'float32')
>>> sparse_x = x.to_sparse_csr()
>>> sparse_y = y.to_sparse_csr()
>>> sparse_z = paddle.sparse.add(sparse_x, sparse_y)
>>> print(sparse_z.to_dense())
Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
       [[ 0., -1.,  0.,  0.],
        [ 0.,  2., -6.,  0.],
        [ 6.,  8.,  4.,  8.]])