เราสามารถรวมทิลเซอร์สองตัวขึ้นไปได้ด้วย torch.cat() และ torch.stack() เมธอด torch.cat() ใช้สำหรับต่อทิลเซอร์ (concatenate) ในขณะที่ torch.stack() ใช้สำหรับซ้อนทิลเซอร์ (stack) สามารถรวมทิลเซอร์ในมิติต่างๆ ได้ เช่น มิติ 0 หรือ -1
ความแตกต่างระหว่าง torch.cat() และ torch.stack()
torch.cat() ต่อทิลเซอร์ตามมิติที่มีอยู่แล้ว จึงไม่เปลี่ยนจำนวนมิติของทิลเซอร์
torch.stack() ซ้อนทิลเซอร์ตามมิติใหม่ ทำให้จำนวนมิติเพิ่มขึ้น
ขั้นตอนการทำงาน
นำเข้าไลบรารี torch
สร้างทิลเซอร์ PyTorch สองตัวขึ้นไปและพิมพ์ค่า
ใช้ torch.cat() หรือ torch.stack() รวมทิลเซอร์ ระบุมิติ (dimension) เช่น 0, -1
พิมพ์ทิลเซอร์ที่รวมแล้ว
ตัวอย่างที่ 1: ต่อทิลเซอร์ 1D ด้วย torch.cat()
import torch
# สร้างทิลเซอร์
T1 = torch.Tensor([1, 2, 3, 4])
T2 = torch.Tensor([0, 3, 4, 1])
T3 = torch.Tensor([4, 3, 2, 5])
print("T1:", T1)
print("T2:", T2)
print("T3:", T3)
# ต่อทิลเซอร์ด้วย torch.cat()
T = torch.cat((T1, T2, T3))
print("T:", T)
ผลลัพธ์
T1: tensor([1., 2., 3., 4.]) T2: tensor([0., 3., 4., 1.]) T3: tensor([4., 3., 2., 5.]) T: tensor([1., 2., 3., 4., 0., 3., 4., 1., 4., 3., 2., 5.])
ตัวอย่างที่ 2: ต่อทิลเซอร์ 2D ในมิติ 0 และ -1
import torch
T1 = torch.Tensor([[1, 2], [3, 4]])
T2 = torch.Tensor([[0, 3], [4, 1]])
T3 = torch.Tensor([[4, 3], [2, 5]])
print("T1:\n", T1)
print("T2:\n", T2)
print("T3:\n", T3)
print("join(concatenate) tensors in the 0 dimension")
T = torch.cat((T1, T2, T3), 0)
print("T:\n", T)
print("join(concatenate) tensors in the -1 dimension")
T = torch.cat((T1, T2, T3), -1)
print("T:\n", T)
ผลลัพธ์
T1: tensor([[1., 2.], [3., 4.]]) T2: tensor([[0., 3.], [4., 1.]]) T3: tensor([[4., 3.], [2., 5.]]) join(concatenate) tensors in the 0 dimension T: tensor([[1., 2.], [3., 4.], [0., 3.], [4., 1.], [4., 3.], [2., 5.]]) join(concatenate) tensors in the -1 dimension T: tensor([[1., 2., 0., 3., 4., 3.], [3., 4., 4., 1., 2., 5.]])
การต่อในมิติ 0 เพิ่มจำนวนแถว คอลัมน์ไม่เปลี่ยน ในมิติ -1 เพิ่มจำนวนคอลัมน์
ตัวอย่างที่ 3: ซ้อนทิลเซอร์ 1D ด้วย torch.stack()
import torch
T1 = torch.Tensor([1, 2, 3, 4])
T2 = torch.Tensor([0, 3, 4, 1])
T3 = torch.Tensor([4, 3, 2, 5])
print("T1:", T1)
print("T2:", T2)
print("T3:", T3)
print("join(stack) tensors")
T = torch.stack((T1, T2, T3))
print("T:\n", T)
print("join(stack) tensors in the 0 dimension")
T = torch.stack((T1, T2, T3), 0)
print("T:\n", T)
print("join(stack) tensors in the -1 dimension")
T = torch.stack((T1, T2, T3), -1)
print("T:\n", T)
ผลลัพธ์
T1: tensor([1., 2., 3., 4.]) T2: tensor([0., 3., 4., 1.]) T3: tensor([4., 3., 2., 5.]) join(stack) tensors T: tensor([[1., 2., 3., 4.], [0., 3., 4., 1.], [4., 3., 2., 5.]]) join(stack) tensors in the 0 dimension T: tensor([[1., 2., 3., 4.], [0., 3., 4., 1.], [4., 3., 2., 5.]]) join(stack) tensors in the -1 dimension T: tensor([[1., 0., 4.], [2., 3., 3.], [3., 4., 2.], [4., 1., 5.]])
ทิลเซอร์ 1D ถูกซ้อนกันกลายเป็นทิลเซอร์ 2D
ตัวอย่างที่ 4: ซ้อนทิลเซอร์ 2D ด้วย torch.stack()
import torch
T1 = torch.Tensor([[1, 2], [3, 4]])
T2 = torch.Tensor([[0, 3], [4, 1]])
T3 = torch.Tensor([[4, 3], [2, 5]])
print("T1:\n", T1)
print("T2:\n", T2)
print("T3:\n", T3)
print("Join (stack) tensors in the 0 dimension")
T = torch.stack((T1, T2, T3), 0)
print("T:\n", T)
print("Join(stack) tensors in the -1 dimension")
T = torch.stack((T1, T2, T3), -1)
print("T:\n", T)
ผลลัพธ์
T1: tensor([[1., 2.], [3., 4.]]) T2: tensor([[0., 3.], [4., 1.]]) T3: tensor([[4., 3.], [2., 5.]]) Join (stack) tensors in the 0 dimension T: tensor([[[1., 2.], [3., 4.]], [[0., 3.], [4., 1.]], [[4., 3.], [2., 5.]]]) Join(stack) tensors in the -1 dimension T: tensor([[[1., 0., 4.], [2., 3., 3.]], [[3., 4., 2.], [4., 1., 5.]]])
ทิลเซอร์ 2D ถูกซ้อนกันกลายเป็นทิลเซอร์ 3D