tile() in PyTorch
Tags: pytorch, tile, repeat, function
Published: 2024-06-23 03:15:24

*[My post](https://dev.to/hyperkai/repeatinterleave-in-pytorch-201n) explains [repeat_interleave()](https://pytorch.org/docs/stable/generated/torch.repeat_interleave.html).

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

*Memos:
- `tile()` 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 `dims`(Required-Type:`tuple` or `list` of `int`):
  *Memos:
  - If at least one dimension is `0`, an empty tensor is returned.
  - The 1st or more arguments(`int`) with a tensor is also `dims`. *`dims=` mustn't be used. 
  
```python
import torch

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

torch.tile(input=my_tensor, dims=(1,))
my_tensor.tile(dims=(1,))
# tensor([3, 5, 1])

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

torch.tile(input=my_tensor, dims=(3,))
# tensor([3, 5, 1, 3, 5, 1, 3, 5, 1])
etc.

torch.tile(input=my_tensor, dims=(1, 1))
# tensor([[3, 5, 1]])

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

torch.tile(input=my_tensor, dims=(1, 3))
# tensor([[3, 5, 1, 3, 5, 1, 3, 5, 1]])
etc.

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

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

torch.tile(input=my_tensor, dims=(2, 3))
# tensor([[3, 5, 1, 3, 5, 1, 3, 5, 1],
#         [3, 5, 1, 3, 5, 1, 3, 5, 1]])
etc.

torch.tile(input=my_tensor, dims=(3, 1))
# tensor([[3, 5, 1],
#         [3, 5, 1],
#         [3, 5, 1]])
etc.

torch.tile(input=my_tensor, dims=(1, 1, 1))
# tensor([[[3, 5, 1]]])
etc.

torch.tile(input=my_tensor, dims=(1, 0, 1))
# tensor([], size=(1, 0, 3), dtype=torch.int64)

my_tensor.tile(3, 2, 1)
# tensor([[[3, 5, 1], [3, 5, 1]],
#         [[3, 5, 1], [3, 5, 1]],
#         [[3, 5, 1], [3, 5, 1]]])

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

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

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

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

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

torch.tile(input=my_tensor, dims=(2,))
# tensor([True, False, True, True, False, True])

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

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

torch.tile(input=my_tensor, dims=(3,))
# tensor([[3, 5, 1, 3, 5, 1, 3, 5, 1],
#         [6, 0, 5, 6, 0, 5, 6, 0, 5]])
etc.

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

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

torch.tile(input=my_tensor, dims=(1, 3))
# tensor([[3, 5, 1, 3, 5, 1, 3, 5, 1],
#         [6, 0, 5, 6, 0, 5, 6, 0, 5]])
etc.

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

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

torch.tile(input=my_tensor, dims=(2, 3))
# tensor([[3, 5, 1, 3, 5, 1, 3, 5, 1],
#         [6, 0, 5, 6, 0, 5, 6, 0, 5],
#         [3, 5, 1, 3, 5, 1, 3, 5, 1],
#         [6, 0, 5, 6, 0, 5, 6, 0, 5]])
etc.

torch.tile(input=my_tensor, dims=(3, 1))
# tensor([[3, 5, 1],
#         [6, 0, 5],
#         [3, 5, 1],
#         [6, 0, 5],
#         [3, 5, 1],
#         [6, 0, 5]])
etc.

torch.tile(input=my_tensor, dims=(1, 1, 1))
# tensor([[[3, 5, 1],
#          [6, 0, 5]]])
etc.
```