torch.index_put
function index_put(input: Tensor, indices: (Tensor | null)[], values: Tensor, options?: IndexPutOptions): Tensorfunction index_put(input: Tensor, indices: (Tensor | null)[], values: Tensor, accumulate: boolean, options?: IndexPutOptions): TensorPlaces values into a tensor at index positions (general indexing operation).
General form of index assignment supporting multiple index tensors or None. Currently supports simplified single-dimension indexing. Equivalent to: input[index] = values for single index tensors. Useful for:
- Multi-dimensional indexing: setting values at complex positions
- Conditional assignment: setting based on computed indices
- General index_put replacement: Python-style tensor[indices] = values
- Flexible placement: placing at arbitrary positions
Note: Currently only supports single dimension indexing. Use index_copy or scatter operations for more complex indexing patterns.
- Single dimension: Currently limited to single 1D index tensor
- Accumulate flag: Controls replacement vs addition behavior
- Index restrictions: Only supports simple 1D indexing for now
- Limited scope: Multi-dimensional advanced indexing not yet fully supported
- Index type: Only 1D index tensors currently supported
Parameters
inputTensor- The input tensor
indices(Tensor | null)[]- Array of index tensors or None (currently only 1D single index supported)
valuesTensor- Source values to place
optionsIndexPutOptionsoptional
Returns
Tensor– A new tensor with values placed at index positionsExamples
// Simple 1D indexing
const data = torch.zeros(10);
const indices = [torch.tensor([0, 2, 4, 6, 8])];
const values = torch.ones(5);
const result = torch.index_put(data, indices, values); // [0]=1, [2]=1, etc.
// Accumulate mode
const accumulator = torch.zeros(10);
const acc_indices = [torch.tensor([0, 0, 1, 1, 2])]; // Repeated indices
const acc_values = torch.ones(5);
const accumulated = torch.index_put(accumulator, acc_indices, acc_values, true);
// [0]=2, [1]=2, [2]=1 (accumulated at same positions)See Also
- PyTorch torch.index_put()
- index_copy - Copy at 1D indices
- index_add - Add at 1D indices
- scatter - Flexible multi-D scattering