DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

repeat in PyTorch

Buy Me a Coffee

*Memos:

repeat() can get the 0D or more D tensor of zero or more repeated elements from the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • repeat() can be used with a tensor but not with torch.
  • Using a tensor(Required-Type:tensor of int, float, complex or bool).
  • The 1st or more arguments with a tensor are repeats(Required-Type:int, tuple of int, list of int or size()): *Memos:
    • Its D must be more than or equal to the tensor's D.
    • If at least one dimension is 0, an empty tensor is returned.
    • repeats= mustn't be used for the one or more dimensions without a tuple, list or size().
import torch

my_tensor = torch.tensor([7, 4, 2])

my_tensor.repeat(repeats=(0,))
my_tensor.repeat(0)
my_tensor.repeat(repeats=torch.tensor([]).size())
# tensor([], dtype=torch.int64)

my_tensor.repeat(repeats=(1,))
my_tensor.repeat(1)
my_tensor.repeat(repeats=torch.tensor([5]).size())
# tensor([7, 4, 2])

my_tensor.repeat(repeats=(2,))
my_tensor.repeat(2)
my_tensor.repeat(repeats=torch.tensor([5, 8]).size())
# tensor([7, 4, 2, 7, 4, 2])

my_tensor.repeat(repeats=(3,))
my_tensor.repeat(3)
my_tensor.repeat(repeats=torch.tensor([5, 8, 1]).size())
# tensor([7, 4, 2, 7, 4, 2, 7, 4, 2])
etc.

my_tensor.repeat(repeats=(3, 2))
my_tensor.repeat(3, 2)
my_tensor.repeat(repeats=torch.tensor([[5, 8], [1, 9], [3, 0]]).size())
# tensor([[7, 4, 2, 7, 4, 2],
#         [7, 4, 2, 7, 4, 2],
#         [7, 4, 2, 7, 4, 2]])
etc.

my_tensor.repeat(repeats=(3, 2, 1))
my_tensor.repeat(3, 2, 1)
# tensor([[[7, 4, 2], [7, 4, 2]],
#         [[7, 4, 2], [7, 4, 2]],
#         [[7, 4, 2], [7, 4, 2]]])
etc.

my_tensor.repeat(repeats=(3, 0, 1))
my_tensor.repeat(3, 0, 1)
# tensor([], size=(3, 0, 3), dtype=torch.int64)

my_tensor = torch.tensor([[7, 4, 2], [5, 1, 6]])

my_tensor.repeat(repeats=(3, 2))
my_tensor.repeat(3, 2)
my_tensor.repeat(repeats=torch.tensor([[5, 8], [1, 9], [3, 0]]).size())
# tensor([[7, 4, 2, 7, 4, 2],
#         [5, 1, 6, 5, 1, 6],
#         [7, 4, 2, 7, 4, 2],
#         [5, 1, 6, 5, 1, 6],
#         [7, 4, 2, 7, 4, 2],
#         [5, 1, 6, 5, 1, 6]])

my_tensor = torch.tensor([[7., 4., 2.], [5., 1., 6.]])

my_tensor.repeat(repeats=(3, 2))
# tensor([[7., 4., 2., 7., 4., 2.],
#         [5., 1., 6., 5., 1., 6.],
#         [7., 4., 2., 7., 4., 2.],
#         [5., 1., 6., 5., 1., 6.],
#         [7., 4., 2., 7., 4., 2.],
#         [5., 1., 6., 5., 1., 6.]])

my_tensor = torch.tensor([[7.+0.j, 4.+0.j, 2.+0.j],
                          [5.+0.j, 1.+0.j, 6.+0.j]])
my_tensor.repeat(repeats=(3, 2))
# tensor([[7.+0.j, 4.+0.j, 2.+0.j, 7.+0.j, 4.+0.j, 2.+0.j],
#         [5.+0.j, 1.+0.j, 6.+0.j, 5.+0.j, 1.+0.j, 6.+0.j],
#         [7.+0.j, 4.+0.j, 2.+0.j, 7.+0.j, 4.+0.j, 2.+0.j],
#         [5.+0.j, 1.+0.j, 6.+0.j, 5.+0.j, 1.+0.j, 6.+0.j],
#         [7.+0.j, 4.+0.j, 2.+0.j, 7.+0.j, 4.+0.j, 2.+0.j],
#         [5.+0.j, 1.+0.j, 6.+0.j, 5.+0.j, 1.+0.j, 6.+0.j]])

my_tensor = torch.tensor([[True, False, True],
                          [False, True, False]])
my_tensor.repeat(repeats=(3, 2))
# tensor([[True, False, True, True, False, True],
#         [False, True, False, False, True, False],
#         [True, False, True, True, False, True],
#         [False True, False, False, True, False],
#         [True, False, True, True, False, True],
#         [False, True, False, False, True, False]])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)