torch.Tensor.Tensor.index_put_
Tensor.index_put_(indices: (Tensor | null)[], values: Tensor, options?: IndexPutOptions): thisTensor.index_put_(indices: (Tensor | null)[], values: Tensor, accumulate: boolean, options?: IndexPutOptions): thisIn-place version of index_put - updates values at specified indices directly.
Modifies the tensor in-place by replacing or accumulating values at specified indices. More memory-efficient than index_put() when you don't need to preserve the original tensor. Returns self for method chaining.
Use Cases:
- Efficient in-place updates during iteration
- Memory-conscious operations in tight loops
- Cumulative scatter operations
- In-place: Modifies self, no new allocation
- Return value: Returns this for chaining
- Memory efficient: No copy needed (comparedto index_put)
- Same parameters: Same semantics as index_put()
- Destructive: Original values at indices are overwritten/accumulated
- No undo: Changes are permanent unless you save the original
Parameters
indices(Tensor | null)[]- Array of index tensors (one per dimension)
valuesTensor- Values to put/accumulate at the specified indices
optionsIndexPutOptionsoptional
Returns
this– This tensor (modified in-place)Examples
// In-place replacement
const x = torch.zeros(3, 3);
x.index_put_([torch.tensor([0, 2]), torch.tensor([1, 2])], torch.tensor([1, 2]));
// x is now modified
// Cumulative in-place accumulation
const counts = torch.zeros(10);
const indices = torch.tensor([0, 1, 0, 2, 1]);
counts.index_put_([indices], torch.ones(5), true);
// counts[0] = 2, counts[1] = 2, counts[2] = 1See Also
- PyTorch torch.index_put_() (or tensor.index_put_())
- index_put - Non-destructive version
- scatter_ - In-place scatter operation
- put_ - In-place put with flat indexing