repeat_interleave() in PyTorch
Tags: pytorch, repeat, interleave, function
Published: 2024-06-23 04:29:04

*[My post](https://dev.to/hyperkai/tile-in-pytorch-3dna) explains [tile()](https://pytorch.org/docs/stable/generated/torch.tile.html).

[repeat_interleave()](https://pytorch.org/docs/stable/generated/torch.repeat_interleave.html) can get the 1D tensor of immediately repeated zero or more elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:
- `repeat_interleave()` can be used with [torch](https://pytorch.org/docs/stable/torch.html) or a tensor.
- The 1st argument with `torch` or using a tensor is `input`(Required-Type:`tensor` of `int`, `float`, `complex` or `bool`). 
- The 2nd argument with `torch` or the 1st argument with a tensor is `repeats`(Required-Type:`int` or `tensor` of `int`). *`repeat_interleave()` without `repeats` argument and `input` keyword works.
- The 3rd argument with `torch` or the 2nd argument with a tensor is `dim`(Optional-Type:`int`).
- There is `output_size` argument with `torch` or a tensor(Optional-Type:`int`):
  *Memos:
  - Total output size for the given axis (e.g. sum of repeats). If given, it will avoid stream synchronization needed to calculate output shape of the tensor.
  - `output_size=` must be used.

```python
import torch

my_tensor = torch.tensor([3, 5, 1])

torch.repeat_interleave(input=my_tensor, repeats=0)
torch.repeat_interleave(input=my_tensor, repeats=0, dim=0)
torch.repeat_interleave(input=my_tensor, repeats=0, dim=-1)
my_tensor.repeat_interleave(0)
# tensor([], dtype=torch.int64)

torch.repeat_interleave(input=my_tensor, repeats=1)
torch.repeat_interleave(input=my_tensor, repeats=1, dim=0)
torch.repeat_interleave(input=my_tensor, repeats=1, dim=-1)
# tensor([3, 5, 1])

torch.repeat_interleave(input=my_tensor, repeats=2)
torch.repeat_interleave(input=my_tensor, repeats=2, dim=0)
torch.repeat_interleave(input=my_tensor, repeats=2, dim=-1)
# tensor([3, 3, 5, 5, 1, 1])

torch.repeat_interleave(input=my_tensor, repeats=3)
torch.repeat_interleave(input=my_tensor, repeats=3, dim=0)
torch.repeat_interleave(input=my_tensor, repeats=3, dim=-1)
# tensor([3, 3, 3, 5, 5, 5, 1, 1, 1])
etc.

torch.repeat_interleave(input=my_tensor,
                        repeats=torch.tensor([2, 1, 4]))
torch.repeat_interleave(input=my_tensor,
                        repeats=torch.tensor([2, 1, 4]), dim=0)
torch.repeat_interleave(input=my_tensor,
                        repeats=torch.tensor([2, 1, 4]), dim=-1)
# tensor([3, 3, 5, 1, 1, 1, 1])

torch.repeat_interleave(input=my_tensor, repeats=torch.tensor(2))
torch.repeat_interleave(input=my_tensor, repeats=torch.tensor(2), dim=0)
torch.repeat_interleave(input=my_tensor, repeats=torch.tensor(2), dim=-1)
torch.repeat_interleave(input=my_tensor, repeats=torch.tensor([2]))
torch.repeat_interleave(input=my_tensor, repeats=torch.tensor([2]), dim=0)
torch.repeat_interleave(input=my_tensor, repeats=torch.tensor([2]), dim=-1)
# tensor([3, 3, 5, 5, 1, 1])

torch.repeat_interleave(input=my_tensor, repeats=3, dim=0, output_size=9)
# tensor([3, 3, 3, 5, 5, 5, 1, 1, 1])

torch.repeat_interleave(my_tensor)
# tensor([0, 0, 0, 1, 1, 1, 1, 1, 2])

my_tensor = torch.tensor([3., 5., 1.])

torch.repeat_interleave(input=my_tensor, repeats=2)
# tensor([3., 3., 5., 5., 1., 1.])

my_tensor = torch.tensor([3.+0.j, 5.+0.j, 1.+0.j])

torch.repeat_interleave(input=my_tensor, repeats=2)
# tensor([3.+0.j, 3.+0.j, 5.+0.j, 5.+0.j, 1.+0.j, 1.+0.j])

my_tensor = torch.tensor([True, False, True])

torch.repeat_interleave(input=my_tensor, repeats=2)
# tensor([True, True, False, False, True, True])

my_tensor = torch.tensor([[3, 5, 1], [6, 0, 5]])

torch.repeat_interleave(input=my_tensor, repeats=0)
# tensor([], dtype=torch.int64)

torch.repeat_interleave(input=my_tensor, repeats=0, dim=1)
torch.repeat_interleave(input=my_tensor, repeats=0, dim=-1)
# tensor([], size=(2, 0), dtype=torch.int64)

torch.repeat_interleave(input=my_tensor, repeats=0, dim=0)
torch.repeat_interleave(input=my_tensor, repeats=0, dim=-2)
# tensor([], size=(0, 3), dtype=torch.int64)

torch.repeat_interleave(input=my_tensor, repeats=1)
# tensor([3, 5, 1, 6, 0, 5])

torch.repeat_interleave(input=my_tensor, repeats=1, dim=0)
torch.repeat_interleave(input=my_tensor, repeats=1, dim=1)
torch.repeat_interleave(input=my_tensor, repeats=1, dim=-1)
torch.repeat_interleave(input=my_tensor, repeats=1, dim=-2)
# tensor([[3, 5, 1], [6, 0, 5]])

torch.repeat_interleave(input=my_tensor, repeats=2)
# tensor([3, 3, 5, 5, 1, 1, 6, 6, 0, 0, 5, 5])

torch.repeat_interleave(input=my_tensor, repeats=2, dim=0)
torch.repeat_interleave(input=my_tensor, repeats=2, dim=-2)
# tensor([[3, 5, 1], [3, 5, 1], [6, 0, 5], [6, 0, 5]])

torch.repeat_interleave(input=my_tensor, repeats=2, dim=1)
torch.repeat_interleave(input=my_tensor, repeats=2, dim=-1)
# tensor([[3, 3, 5, 5, 1, 1], [6, 6, 0, 0, 5, 5]])

torch.repeat_interleave(input=my_tensor, repeats=3)
# tensor([3, 3, 3, 5, 5, 5, 1, 1, 1, 6, 6, 6, 0, 0, 0, 5, 5, 5])

torch.repeat_interleave(input=my_tensor, repeats=3, dim=0)
torch.repeat_interleave(input=my_tensor, repeats=3, dim=-2)
# tensor([[3, 5, 1], [3, 5, 1], [3, 5, 1], [6, 0, 5], [6, 0, 5], [6, 0, 5]])

torch.repeat_interleave(input=my_tensor, repeats=3, dim=1)
torch.repeat_interleave(input=my_tensor, repeats=3, dim=-1)
# tensor([[3, 3, 3, 5, 5, 5, 1, 1, 1], [6, 6, 6, 0, 0, 0, 5, 5, 5]])
```