ในการจัดเรียงองค์ประกอบของ tensor ใน PyTorch เราสามารถใช้เมธอด torch.sort() เมธอดนี้คืนค่า tensor สองอัน tensor แรกคือ tensor ที่มีค่าที่จัดเรียงแล้วขององค์ประกอบ และ tensor ที่สองคือ tensor ของดัชนีขององค์ประกอบใน tensor ต้นฉบับ เราสามารถคำนวณ 2D tensor แบบแถวและคอลัมน์ได้
ขั้นตอน
นำเข้าไลบรารีที่จำเป็น ในตัวอย่าง Python ทั้งหมดต่อไปนี้ ไลบรารี Python ที่จำเป็นคือ torch ตรวจสอบให้แน่ใจว่าคุณติดตั้งแล้ว
สร้าง PyTorch tensor และพิมพ์มัน
เพื่อจัดเรียงองค์ประกอบของ tensor ข้างต้น ให้คำนวณ torch.sort(input, dim) กำหนดค่านี้ให้ตัวแปรใหม่ "v" ที่นี่ input คือ tensor อินพุต และ dim คือมิติที่จัดเรียงองค์ประกอบตาม ในการจัดเรียงแบบแถว dim ตั้งเป็น 1 และในการจัดเรียงแบบคอลัมน์ dim ตั้งเป็น 0
Tensor ที่มีค่าที่จัดเรียงแล้วสามารถเข้าถึงได้เป็น v[0] และ tensor ของดัชนีของค่าที่จัดเรียงแล้วเป็น v[1]
พิมพ์ Tensor ที่มีค่าที่จัดเรียงแล้ว และ tensor ที่มีดัชนีของค่าที่จัดเรียงแล้ว
ตัวอย่างที่ 1
โปรแกรม Python ต่อไปนี้แสดงวิธีจัดเรียงองค์ประกอบของ 1D tensor
# โปรแกรม Python จัดเรียงองค์ประกอบของ tensor
# นำเข้าไลบรารีที่จำเป็น
import torch
# สร้าง tensor
T = torch.Tensor([2.334, 4.433, -4.33, -0.433, 5, 4.443])
print("Original Tensor:\n", T)
# จัดเรียง tensor T
# มันจัดเรียง tensor ตามลำดับจากน้อยไปมาก
v = torch.sort(T)
# พิมพ์ tensor ของค่าที่จัดเรียงแล้ว
print("Tensor with sorted value:\n", v[0])
# พิมพ์ดัชนีของค่าที่จัดเรียงแล้ว
print("Indices of sorted value:\n", v[1])
ผลลัพธ์
Original Tensor: tensor([ 2.3340, 4.4330, -4.3300, -0.4330, 5.0000, 4.4430]) Tensor with sorted value: tensor([-4.3300, -0.4330, 2.3340, 4.4330, 4.4430, 5.0000]) Indices of sorted value: tensor([2, 3, 0, 1, 5, 4])
ตัวอย่างที่ 2
โปรแกรม Python ต่อไปนี้แสดงวิธีจัดเรียงองค์ประกอบของ 2D tensor
# โปรแกรม Python จัดเรียงองค์ประกอบของ 2-D tensor
# นำเข้าไลบรารี
import torch
# สร้าง 2-D tensor
T = torch.Tensor([[2, 3, -32],
[43, 4, -53],
[4, 37, -4],
[3, -75, 34]])
print("Original Tensor:\n", T)
# จัดเรียง tensor T
# มันจัดเรียง tensor ตามลำดับจากน้อยไปมาก
v = torch.sort(T)
# พิมพ์ tensor ของค่าที่จัดเรียงแล้ว
print("Tensor with sorted value:\n", v[0])
# พิมพ์ดัชนีของค่าที่จัดเรียงแล้ว
print("Indices of sorted value:\n", v[1])
print("Sort tensor Column-wise")
v = torch.sort(T, 0)
# พิมพ์ tensor ของค่าที่จัดเรียงแล้ว
print("Tensor with sorted value:\n", v[0])
# พิมพ์ดัชนีของค่าที่จัดเรียงแล้ว
print("Indices of sorted value:\n", v[1])
print("Sort tensor Row-wise")
v = torch.sort(T, 1)
# พิมพ์ tensor ของค่าที่จัดเรียงแล้ว
print("Tensor with sorted value:\n", v[0])
# พิมพ์ดัชนีของค่าที่จัดเรียงแล้ว
print("Indices of sorted value:\n", v[1])
ผลลัพธ์
Original Tensor:
tensor([[ 2., 3., -32.],
[ 43., 4., -53.],
[ 4., 37., -4.],
[ 3., -75., 34.]])
Tensor with sorted value:
tensor([[-32., 2., 3.],
[-53., 4., 43.],
[ -4., 4., 37.],
[-75., 3., 34.]])
Indices of sorted value:
tensor([[2, 0, 1],
[2, 1, 0],
[2, 0, 1],
[1, 0, 2]])
Sort tensor Column-wise
Tensor with sorted value:
tensor([[ 2., -75., -53.],
[ 3., 3., -32.],
[ 4., 4., -4.],
[ 43., 37., 34.]])
Indices of sorted value:
tensor([[0, 3, 1],
[3, 0, 0],
[2, 1, 2],
[1, 2, 3]])
Sort tensor Row-wise
Tensor with sorted value:
tensor([[-32., 2., 3.],
[-53., 4., 43.],
[ -4., 4., 37.],
[-75., 3., 34.]])
Indices of sorted value:
tensor([[2, 0, 1],
[2, 1, 0],
[2, 0, 1],
[1, 0, 2]])