torch.repeat_interleave
function repeat_interleave<S extends Shape, Dt extends DType, Dev extends DeviceType, Dim extends number, Repeats extends number>(input: Tensor<S, Dt, Dev>, repeats: Repeats, dim: Dim, options?: RepeatInterleaveOptions): Tensor<ScaleDim<S, Dim, Repeats>, Dt, Dev>function repeat_interleave(input: Tensor, repeats: Tensor, dim: number, output_size: number, options?: RepeatInterleaveOptions): Promise<Tensor>Repeats tensor elements a specified number of times along a dimension.
Each element along the specified dimension is repeated (interleaved) the given number of times. Useful for:
- Data replication: duplicating samples for augmentation
- Upsampling: increasing resolution/temporal length
- One-hot encoding expansion: repeating categories
- Batch processing: expanding samples to larger batch
- Feature duplication: repeating features for alignment
Can repeat with a constant number of times for all elements, or with a tensor specifying different repeat counts per element. The tensor version is async (promise-based).
- Interleaving order: Elements are repeated in sequence, not interleaved
- Dimension reduction: If dim not specified, input is flattened first
- Async with tensor repeats: Using tensor repeats returns a Promise
- Memory allocation: Output size = sum of all repeats
- Empty tensor repeats: Zero repeats create empty dimension
- Negative repeats: Not allowed in scalar form; tensor form checks each element
- Async handling: Tensor repeats require await/Promise handling
Parameters
inputTensor<S, Dt, Dev>- Input tensor
repeatsRepeats- Number of times to repeat each element (scalar) or tensor of repeat counts per element
dimDim- Dimension along which to repeat (default: if not specified, tensor is flattened)
optionsRepeatInterleaveOptionsoptional- Optional settings for repeat_interleave
Returns
Tensor<ScaleDim<S, Dim, Repeats>, Dt, Dev>– Tensor with repeated elements (or Promise if repeats is tensor)Examples
// Basic scalar repeat
const x = torch.tensor([1, 2, 3]);
torch.repeat_interleave(x, 2); // [1, 1, 2, 2, 3, 3]
// Repeat along specific dimension
const y = torch.tensor([[1, 2], [3, 4]]);
torch.repeat_interleave(y, 2, 0); // [[1,2], [1,2], [3,4], [3,4]] - repeat rows
torch.repeat_interleave(y, 2, 1); // [[1,1,2,2], [3,3,4,4]] - repeat columns
// Upsampling: increase temporal resolution
const time_series = torch.randn(100, 64); // [time_steps, features]
const upsampled = torch.repeat_interleave(time_series, 2, 0); // [200, 64]
// One-hot encoding expansion
const categories = torch.tensor([0, 1, 2]);
const expanded = torch.repeat_interleave(categories, 3, 0); // [0,0,0,1,1,1,2,2,2]
// Variable repeats per element (async)
const data = torch.tensor([10, 20, 30]);
const repeat_counts = torch.tensor([1, 3, 2]); // Different repeats
const result = await torch.repeat_interleave(data, repeat_counts, 0);
// [10, 20, 20, 20, 30, 30]See Also
- PyTorch torch.repeat_interleave()
- torch.repeat - Different repeating semantics (broadcasts vs interleaves)
- torch.tile - Tile/repeat along multiple dimensions
- torch.expand - View-based expansion (memory efficient)
- torch.unsqueeze - Add dimensions before tiling