mindspore.ops.sparse_add

mindspore.ops.sparse_add(x1: COOTensor, x2: COOTensor, thresh: Tensor)[source]

Computes the sum of x1(COOTensor) and x2(COOTensor), and return a new COOTensor based on the computed result and thresh.

Parameters
  • x1 (COOTensor) – the first COOTensor to sum.

  • x2 (COOTensor) – the second COOTensor to sum.

  • thresh (Tensor) – A 0-D Tensor, represents the magnitude threshold that determines if an output value/index pair take space. Its dtype should match that of the values if they are real. If output’s value is less than the thresh, it will vanish.

Returns

A COOTensor, the result of sum.

Raises
  • ValueError – If any input(x1/x2)’s indices’s dim is not equal to 2.

  • ValueError – If any input(x1/x2)’s values’s dim is not equal to 1.

  • ValueError – If any input(x1/x2)’s shape’s dim is not equal to 1.

  • ValueError – If thresh’s dim is not equal to 0.

  • TypeError – If any input(x1/x2)’s indices’s type is not equal to int64.

  • TypeError – If any input(x1/x2)’s shape’s type is not equal to int64.

  • ValueError – If any input(x1/x2)’s indices’s length is not equal to its values’s length.

  • TypeError – If any input(x1/x2)’s values’s type is not equal to anf of (int8/int16/int32/int64/float32/float64/complex64/complex128).

  • TypeError – If thresh’s type is not equal to anf of (int8/int16/int32/int64/float32/float64).

  • TypeError – If x1’s indices’s type is not equal to x2’s indices’s type.

  • TypeError – If x1’s values’s type is not equal to x2’s values’s type.

  • TypeError – If x1’s shape’s type is not equal to x2’s shape’s type.

  • TypeError – If (x1/x2)’s value’s type is not matched with thresh’s type.

Supported Platforms:

CPU GPU

Examples

>>> from mindspore import Tensor, COOTensor
>>> from mindspore import dtype as mstype
>>> from mindspore import context
>>> from mindspore import ops
>>> indics0 = Tensor([[0, 1], [1, 2]], dtype=mstype.int64)
>>> values0 = Tensor([1, 2], dtype=mstype.int32)
>>> shape0 = (3, 4)
>>> input0 = COOTensor(indics0, values0, shape0)
>>> indics1 = Tensor([[0, 0], [1, 1]], dtype=mstype.int64)
>>> values1 = Tensor([3, 4], dtype=mstype.int32)
>>> shape1 = (3, 4)
>>> input1 = COOTensor(indics1, values1, shape1)
>>> thres = Tensor(0, dtype=mstype.int32)
>>> out = ops.sparse_add(input0, input1, thres)
>>> print(out)
COOTensor(shape = [3, 4], dtype = Int32, indices=Tensor(shape=[4, 2],
dtype = Int64, value=[[0 0], [0 1], [1 1], [1 2]]),  values=Tensor(shape[4],
dtype=Int32, value=[3 1 4 2]))