torch.index_add
function index_add<S extends Shape, Dt extends DType, Dev extends DeviceType>(input: Tensor<S, Dt, Dev>, dim: number, index: Tensor, source: Tensor, options?: AlphaBetaOptions): Tensor<S, Dt, Dev>function index_add<S extends Shape, Dt extends DType, Dev extends DeviceType>(input: Tensor<S, Dt, Dev>, dim: number, index: Tensor, source: Tensor, alpha: number, options?: AlphaBetaOptions): Tensor<S, Dt, Dev>Adds source elements to input at positions specified by 1D indices.
Similar to index_copy but accumulates (adds) values instead of replacing. Values at the same index position are summed. Useful for:
- Accumulation: summing values at specific positions
- Histogram computation: counting in bins
- Sparse updates: adding to multiple positions
- Weighted accumulation: adding scaled values
- Aggregation: combining multiple sources at positions
- Accumulation: Values at same index are summed
- 1D indices: Index must be 1D
- Scaling: Alpha parameter scales source before adding
- Repeated indices: Same index can appear multiple times (values sum)
- Index length: Must match source.shape[dim]
- Repeated indices: Adding at same position multiple times accumulates
Parameters
inputTensor<S, Dt, Dev>- The input tensor
dimnumber- The dimension along which to index
indexTensor- 1D tensor of indices (length must match source.shape[dim])
sourceTensor- The source tensor with values to add
optionsAlphaBetaOptionsoptional
Returns
Tensor<S, Dt, Dev>– A new tensor with source values accumulated into inputExamples
// Accumulation at specific positions
const base = torch.zeros(10, 5);
const indices = torch.tensor([0, 2, 4, 2, 0]); // Can repeat
const values = torch.ones(5, 5);
const result = torch.index_add(base, 0, indices, values);
// Positions 0 and 2 get added values multiple times
// Weighted accumulation
const accumulator = torch.zeros(100, 64);
const selected_indices = torch.randint(0, 100, [50]);
const updates = torch.randn(50, 64);
const alpha = 0.1; // Learning rate
const updated = torch.index_add(accumulator, 0, selected_indices, updates, alpha);
// Histogram binning
const histogram = torch.zeros(10); // 10 bins
const bin_indices = torch.tensor([0, 1, 1, 2, 2, 2, 3]); // Which bins
const counts = torch.ones(7);
const hist_result = torch.index_add(histogram, 0, bin_indices, counts);See Also
- PyTorch torch.index_add()
- index_copy - Copy instead of add
- index_reduce - Custom reduction operation
- scatter_add - More flexible multi-D version