repeat_interleave() in PyTorch

*My post explains tile().

repeat_interleave() can immediately repeat the zero or more elements of a 0D or more D tensor as shown below:

*Memos:

repeat_interleave() can be used with torch or a tensor.
The 1st argument(tensor of int, float, complex …


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

*My post explains tile().

repeat_interleave() can immediately repeat the zero or more elements of a 0D or more D tensor as shown below:

*Memos:

  • repeat_interleave() can be used with torch or a tensor.
  • The 1st argument(tensor of int, float, complex or bool) with torch or using a tensor(tensor of int, float, complex or bool) is input(Required).
  • The 2nd argument(int or tensor of int) with torch or the 1st argument(int or tensor of int) with a tensor is repeats(Required). *repeat_interleave() without repeats argument and input keyword works.
  • The 3rd argument(int) with torch or the 2nd argument(int) with a tensor is dim(Optional).
  • There is output_size argument(int) (Optional) with torch or a tensor. *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.
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]])


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-06-23T04:29:04+00:00) repeat_interleave() in PyTorch. Retrieved from https://www.scien.cx/2024/06/23/repeat_interleave-in-pytorch/

MLA
" » repeat_interleave() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Sunday June 23, 2024, https://www.scien.cx/2024/06/23/repeat_interleave-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Sunday June 23, 2024 » repeat_interleave() in PyTorch., viewed ,<https://www.scien.cx/2024/06/23/repeat_interleave-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » repeat_interleave() in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/23/repeat_interleave-in-pytorch/
CHICAGO
" » repeat_interleave() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/06/23/repeat_interleave-in-pytorch/
IEEE
" » repeat_interleave() in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/06/23/repeat_interleave-in-pytorch/. [Accessed: ]
rf:citation
» repeat_interleave() in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/06/23/repeat_interleave-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.