PyTorch torch.tile Function
Pytorch torch Reference Manual
torch.tileIt is a function in PyTorch used to repeat a tensor multiple times.
Function Definition
torch.tile(input, dims)
Usage Examples
Example
import torch
# 1D tensor repetition
x = torch.tensor([1, 2, 3])
print("Original 1D tensor:")
print(x)
result = torch.tile(x, 3)
print("Repeated 3 times:")
print(result)
# 2D tensor repetition
y = torch.tensor([[1, 2], [3, 4]])
print("\nOriginal 2D tensor:")
print(y)
result = torch.tile(y, (2, 3))
print("Repeated 2 times along rows, 3 times along columns:")
print(result)
# 3D tensor repetition
z = torch.tensor([[[1, 2], [3, 4]]])
print("\nOriginal 3D tensor:")
print(z)
result = torch.tile(z, (2, 1, 2))
print("Shape [2, 1, 2] repeated:")
print(result)
print(f"Shape: {result.shape}")
# 1D tensor repetition
x = torch.tensor([1, 2, 3])
print("Original 1D tensor:")
print(x)
result = torch.tile(x, 3)
print("Repeated 3 times:")
print(result)
# 2D tensor repetition
y = torch.tensor([[1, 2], [3, 4]])
print("\nOriginal 2D tensor:")
print(y)
result = torch.tile(y, (2, 3))
print("Repeated 2 times along rows, 3 times along columns:")
print(result)
# 3D tensor repetition
z = torch.tensor([[[1, 2], [3, 4]]])
print("\nOriginal 3D tensor:")
print(z)
result = torch.tile(z, (2, 1, 2))
print("Shape [2, 1, 2] repeated:")
print(result)
print(f"Shape: {result.shape}")
The output is:
原始一维张量:
tensor([1, 2, 3])
重复 3 次:
tensor([1, 2, 3, 1, 2, 3, 1, 2, 3])
原始二维张量:
tensor([[1, 2],
[3, 4]])
沿行重复 2 次,沿列重复 3 次:
tensor([[1, 2, 1, 2, 1, 2],
[3, 4, 3, 4, 3, 4],
[1, 2, 1, 2, 1, 2],
[3, 4, 3, 4, 3, 4]])
原始三维张量:
tensor([[[1, 2],
[3, 4]]])
形状 [2, 1, 2] 重复:
tensor([[[1, 2, 1, 2],
[3, 4, 3, 4]],
[[1, 2, 1, 2],
[3, 4, 3, 4]]])
形状: torch.Size([2, 2, 4])
Other Extensions