Pytorch Torch Repeat_Interleave
# PyTorch torch.repeat_interleave Function
* * Pytorch torch Reference Manual](#)
`torch.repeat_interleave` is a function in PyTorch used to repeat elements along a specified dimension.
### Function Definition
torch.repeat_interleave(input, repeats, dim=None, *, output_size=None)
* * *
## Usage Examples
## Example
import torch
# No dim specified, repeat along all elements
x = torch.tensor([1,2,3])
result = torch.repeat_interleave(x,2)
print("Each element repeated 2 times:")
print(result)
# Repeat along dim=0
y = torch.tensor([[1,2],[3,4]])
print("nOriginal tensor:")
print(y)
result = torch.repeat_interleave(y,2, dim=0)
print("Repeat along dim=0 2 times:")
print(result)
# Different repeat counts for each element
result = torch.repeat_interleave(y, torch.tensor([1,2]), dim=0)
print("nDifferent repeat counts along dim=0 [1, 2]:")
print(result)
# Repeat along dim=1
result = torch.repeat_interleave(y,3, dim=1)
print("nRepeat along dim=1 3 times:")
print(result)
# Return output_size
result = torch.repeat_interleave(x,2, output_size=9)
print("nSpecified output_size=9:")
print(result)
Output:
Each element repeated 2 times: tensor([1, 1, 2, 2, 3, 3])Original tensor: tensor([[1, 2], [3, 4]])Repeat along dim=0 2 times: tensor([[1, 2], [1, 2], [3, 4], [3, 4]])Different repeat counts along dim=0 [1, 2]: tensor([[1, 2], [3, 4], [3, 4]])Repeat along dim=1 3 times: tensor([[1, 1, 1, 2, 2, 2], [3, 3, 3, 4, 4, 4]])Specified output_size=9: tensor([1, 1, 2, 2, 3, 3, 1, 2, 3])
* * Pytorch torch Reference Manual](#)
YouTip