Document feedback

Question document fragment

When a question document fragment contains a formula, it is displayed as a space.

Submission type
issue

It's a little complicated...

I'd like to ask someone.

PR

Just a small problem.

I can fix it online!

Please select the submission type

Problem type
Specifications and Common Mistakes

- Specifications and Common Mistakes:

- Misspellings or punctuation mistakes,incorrect formulas, abnormal display.

- Incorrect links, empty cells, or wrong formats.

- Chinese characters in English context.

- Minor inconsistencies between the UI and descriptions.

- Low writing fluency that does not affect understanding.

- Incorrect version numbers, including software package names and version numbers on the UI.

Usability

- Usability:

- Incorrect or missing key steps.

- Missing main function descriptions, keyword explanation, necessary prerequisites, or precautions.

- Ambiguous descriptions, unclear reference, or contradictory context.

- Unclear logic, such as missing classifications, items, and steps.

Correctness

- Correctness:

- Technical principles, function descriptions, supported platforms, parameter types, or exceptions inconsistent with that of software implementation.

- Incorrect schematic or architecture diagrams.

- Incorrect commands or command parameters.

- Incorrect code.

- Commands inconsistent with the functions.

- Wrong screenshots.

- Sample code running error, or running results inconsistent with the expectation.

Risk Warnings

- Risk Warnings:

- Lack of risk warnings for operations that may damage the system or important data.

Content Compliance

- Content Compliance:

- Contents that may violate applicable laws and regulations or geo-cultural context-sensitive words and expressions.

- Copyright infringement.

Please select the type of question

Problem description

Describe the bug so that we can quickly locate the problem.

mindspore.Tensor.index_add_

View Source On Gitee
Tensor.index_add_(dim, index, source, *, alpha=1)[source]

Accumulate the elements of alpha times source into the self by adding to the index in the order given in index. For example, if dim == 0, index[i] == j, and alpha = -1, then the i th row of source is subtracted from the j th row of self . The dim th dimension of source must have the same size as the length of index , and all other dimensions must match self, or an error will be raised. For a 3-D tensor the output is defined as follows:

self[index[i], :, :] += alphasrc[i, :, :]#if dim==0self[:,  index[i], :] += alphasrc[:,  i, :]#if dim==1self[:, :,  index[i]] += alphasrc[:, :,  i]#if dim==2

Warning

This is an experimental API that is subject to change or deletion.

Parameters
  • dim (int) – The dimension along which to index.

  • index (Tensor) – Add the value of self and source along the dimension of the dim according to the specified index value, with data type int32. The index must be 1D with the same size as the size of source in the dim dimension. The values of index should be in [0, b), where the b is the size of self in the dim dimension.

  • source (Tensor) – The input tensor with the value to add. Must have same data type as self. The shape must be the same as self except the dim th dimension.

Keyword Arguments

alpha (number, optional) – The scalar multiplier for source. Default: 1.

Returns

Tensor, has the same shape and dtype as self.

Raises
  • TypeError – If neither index nor source is a Tensor.

  • ValueError – If dim is out of self rank's range.

  • ValueError – If self rank is not the same as source rank.

  • ValueError – If shape of index is not 1D or size of index is not equal to dimension of source[dim].

  • ValueError – If source's shape is not the same as self except the dim th dimension.

Supported Platforms:

Ascend

Examples

>>> import numpy as np
>>> import mindspore
>>> from mindspore import Tensor
>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32)
>>> index = Tensor(np.array([0, 2]), mindspore.int32)
>>> y = Tensor(np.array([[0.5, 1.0], [1.0, 1.5], [2.0, 2.5]]), mindspore.float32)
>>> output = x.index_add_(1, index, y, alpha=1)
>>> print(output)
[[ 1.5  2.   4. ]
 [ 5.   5.   7.5]
 [ 9.   8.  11.5]]
>>> print(x)
[[ 1.5  2.   4. ]
 [ 5.   5.   7.5]
 [ 9.   8.  11.5]]