class nbla::ScatterAdd

template<typename T>
class ScatterAdd : public nbla::BaseFunction<int>

Add all values from `x1` into the `x0` according to index specified by `indices`.

This function adds `x1` into the copy of `x0` and outputs the copy. The original `x0` will not be changed. `x0`, `indices` and `x1` must have same number of dimensions.

The forward of :func:`~nnabla.functions.scatter_add` is equivalent to:

def scatter_add(x0, indices, x1, axis):
    # Assuming each input is 3 dimensional
    import numpy as np
    output = np.copy(x0)
    for i in range(indices.shape[0]):
        for j in range(indices.shape[1]):
            for k in range(indices.shape[2]):
                if axis == 0:
                    output[indices[i][j][k]][j][k] += x1[i][j][k]
                elif axis == 1:
                    output[i][indices[i][j][k]][k] += x1[i][j][k]
                elif axis == 2:
                    output[i][j][indices[i][j][k]] += x1[i][j][k]
    return output

inputs:
  x0:
    doc: N-D array which the data is added to its copy.
  indices:
    doc: N-D array scatter indices. The size of each dimension must be equal
        or smaller than that of x0 except for the specified axis. The value
        of indices must be smaller than the size of specified axis' dimension
        of x0. The size of each dimension must be equal or smaller than that
        of x1. Indices must not be negative.
  x1:
    doc: N-D array which is scattered and added to x0.
arguments:
  axis:
    doc: Axis along which to index. The axis must not exceed the inputs'
        dimension.
    type: int64
    default: 0
outputs:
  y:
    doc: N-D array which contains the result of scatter addition. The shape is
        same as x0.

Inputs:

  • N-D array x0

  • N-D array scatter indices

  • N-D array x1

Outputs:

  • N-D array of shape same as x0

Param axis:

Axis along which to index

Public Functions

inline virtual shared_ptr<Function> copy() const

Copy another instance of Function with the same context.

inline virtual int min_inputs()

Get minimum number of inputs.

This is meant to be used in setup function with in_types which is used to get maximum number of inputs.

inline virtual int min_outputs()

Get minimum number of outputs.

This is meant to be used in setup function with out_types which is used to get max number of outputs.

inline virtual vector<dtypes> in_types()

Get input dtypes.

Last in_type will be used repeatedly if size of in_types is smaller than size of inputs

inline virtual vector<dtypes> out_types()

Get output dtypes.

Last out_type will be used repeatedly if size of out_types is smaller than size of outputs

inline virtual vector<string> allowed_array_classes()

Get array classes that are allowed to be specified by Context.

inline virtual string name()

Get function name in string.

inline virtual bool grad_depends_output_data(int i, int o) const

Dependency flag for checking if in-grad depends on out-data.

Checking if i-th input’ gradient computation requires o-th output’s data or not.

Note

If any of inputs requires an output variable data when computing its gradient, this function must be overridden to return appropriate boolean value. Otherwise, backward computation will be incorrect.

Parameters:
  • i[in] Input variable index.

  • o[in] Output variable index.