PyTorch torch.tile-Funktion
PyTorch torch Referenzhandbuch
torch.tileEs ist eine Funktion in PyTorch, mit der ein Tensor mehrfach wiederholt wird.
Funktionsdefinition
torch.tile(input, dims)
Anwendungsbeispiele
Beispiel
import torch
# Wiederholung eines 1D-Tensors
x = torch.tensor([1, 2, 3])
print("Originaler 1D-Tensor:")
print(x)
result = torch.tile(x, 3)
print("3-mal wiederholt:")
print(result)
# Wiederholung eines 2D-Tensors
y = torch.tensor([[1, 2], [3, 4]])
print("\nOriginaler 2D-Tensor:")
print(y)
result = torch.tile(y, (2, 3))
print("2-mal entlang der Zeilen, 3-mal entlang der Spalten wiederholt:")
print(result)
# Wiederholung eines 3D-Tensors
z = torch.tensor([[[1, 2], [3, 4]]])
print("\nUrsprünglicher 3D-Tensor:")
print(z)
result = torch.tile(z, (2, 1, 2))
print("Form [2, 1, 2] wiederholt:")
print(result)
print(f"Form: {result.shape}")
# Wiederholung eines 1D-Tensors
x = torch.tensor([1, 2, 3])
print("Originaler 1D-Tensor:")
print(x)
result = torch.tile(x, 3)
print("3-mal wiederholt:")
print(result)
# Wiederholung eines 2D-Tensors
y = torch.tensor([[1, 2], [3, 4]])
print("\nOriginaler 2D-Tensor:")
print(y)
result = torch.tile(y, (2, 3))
print("2-mal entlang der Zeilen, 3-mal entlang der Spalten wiederholt:")
print(result)
# Wiederholung eines 3D-Tensors
z = torch.tensor([[[1, 2], [3, 4]]])
print("\nUrsprünglicher 3D-Tensor:")
print(z)
result = torch.tile(z, (2, 1, 2))
print("Form [2, 1, 2] wiederholt:")
print(result)
print(f"Form: {result.shape}")
Die Ausgabe ist:
原始一维张量:
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])
Andere Erweiterungen