Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách nối (join) các tensor trong PyTorch

Ta có thể nối hai hoặc nhiều tensor bằng torch.cat() và torch.stack(). torch.cat() nối tensor theo chiều đã có, torch.stack() xếp chồng tensor theo chiều mới. Cả hai đều nhận tham số dimension (0, -1, ...).

Khác biệt cơ bản

  • torch.cat(): Nối dọc theo chiều đã tồn tại → không thay đổi số chiều.

  • torch.stack(): Xếp chồng theo chiều mới → tăng số chiều lên 1.

Các bước

  • Nhập torch.

  • Tạo tensor và in ra.

  • Dùng torch.cat() hoặc torch.stack() với tham số dimension.

  • In tensor sau khi nối.

Ví dụ 1: torch.cat() với tensor 1D

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)

# Nối (concatenate) tensor
T = torch.cat((T1, T2, T3))
print("T:", T)

Kết quả

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.])

Ví dụ 2: torch.cat() với tensor 2D theo chiều 0 và -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("Nối theo chiều 0:")
T = torch.cat((T1, T2, T3), 0)
print("T:\n", T)

print("Nối theo chiều -1:")
T = torch.cat((T1, T2, T3), -1)
print("T:\n", T)

Kết quả

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.]])

Nối chiều 0: tăng số hàng, giữ cột. Nối chiều -1: tăng số cột, giữ hàng.

Ví dụ 3: torch.stack() với tensor 1D

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("Xếp chồng (stack) tensor:")
T = torch.stack((T1, T2, T3))
print("T:\n", T)

print("Stack theo chiều 0:")
T = torch.stack((T1, T2, T3), 0)
print("T:\n", T)

print("Stack theo chiều -1:")
T = torch.stack((T1, T2, T3), -1)
print("T:\n", T)

Kết quả

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.]])

Tensor 1D được stack thành tensor 2D.

Ví dụ 4: torch.stack() với tensor 2D

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("Stack theo chiều 0:")
T = torch.stack((T1, T2, T3), 0)
print("T:\n", T)

print("Stack theo chiều -1:")
T = torch.stack((T1, T2, T3), -1)
print("T:\n", T)

Kết quả

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.]]])

Tensor 2D được stack thành tensor 3D.