This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)
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
ofint
,float
,complex
orbool
) withtorch
or using a tensor(tensor
ofint
,float
,complex
orbool
) isinput
(Required). - The 2nd argument(
int
ortensor
ofint
) withtorch
or the 1st argument(int
ortensor
ofint
) with a tensor isrepeats
(Required). *repeat_interleave()
withoutrepeats
argument andinput
keyword works. - The 3rd argument(
int
) withtorch
or the 2nd argument(int
) with a tensor isdim
(Optional). - There is
output_size
argument(int
) (Optional) withtorch
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)
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.