torch.slice_scatter
function slice_scatter<S extends Shape, Dt extends DType, Dev extends DeviceType>(input: Tensor<S, Dt, Dev>, src: Tensor, options?: SliceScatterOptions): Tensor<S, Dt, Dev>function slice_scatter<S extends Shape, Dt extends DType, Dev extends DeviceType>(input: Tensor<S, Dt, Dev>, src: Tensor, dim: number, start: number, end: number, step: number, options?: SliceScatterOptions): Tensor<S, Dt, Dev>Scatters values at a slice range along a dimension.
Embeds a source tensor into a slice of the input tensor. The source size along the specified dimension must match (end - start). Useful for:
- Slice replacement: replacing a range of elements
- Partial updates: updating subsets along dimensions
- Region replacement: replacing regions in tensors
- Tensor composition: building larger tensors from pieces
- Rolling window updates: updating windows in sequences
- Size match: src.shape[dim] must equal (end - start)
- Step=1 only: Currently step must be 1
- Default end: If not provided, computed from src size
- Dimension flexible: Works along any dimension
- Size mismatch: src must fit exactly in [start, end)
- Step limitation: step != 1 not yet supported
- Index validation: start and end must be in valid range
Parameters
inputTensor<S, Dt, Dev>- The input tensor
srcTensor- Source tensor to embed (must fit in slice)
optionsSliceScatterOptionsoptional- Alternative to positional args:
{ dim?, start?, end?, step? }
Returns
Tensor<S, Dt, Dev>– A new tensor with src embedded at the sliceExamples
// Using positional arguments
const matrix = torch.zeros(10, 5);
const new_rows = torch.ones(3, 5);
const updated = torch.slice_scatter(matrix, new_rows, 0, 2, 5);
// Using options object
const updated2 = torch.slice_scatter(matrix, new_rows, { dim: 0, start: 2, end: 5 });
// Mixing positional and options
const updated3 = torch.slice_scatter(matrix, new_rows, 0, { start: 2, end: 5 });
// Temporal slice replacement
const sequence = torch.randn(100, 64);
const replacement = torch.randn(10, 64);
const updated_seq = torch.slice_scatter(sequence, replacement, { dim: 0, start: 40, end: 50 });
// Column range replacement
const grid = torch.zeros(20, 20);
const new_cols = torch.ones(20, 5);
const updated_grid = torch.slice_scatter(grid, new_cols, 1, { start: 10, end: 15 });See Also
- PyTorch torch.slice_scatter()
- select_scatter - Single index scatter (step 1, length 1)
- diagonal_scatter - Diagonal scatter
- slice - Extract slice (inverse operation)
- narrow - Extract narrowed range