ในการลบเมตริกซ์ตามองค์ประกอบ เราสามารถใช้ torch.sub() วิธีการของ PyTorch ลบองค์ประกอบที่เกี่ยวข้องของเทนเซอร์ เราสามารถลบสเกลาร์หรือเทนเซอร์ออกจากเมตริกซ์อื่นได้ เราสามารถลบเทนเซอร์ออกจากเทนเซอร์ที่มีมิติเท่ากันหรือต่างกันได้ มิติของเทนเซอร์สุดท้ายจะเท่ากับมิติของเทนเซอร์มิติที่สูงกว่า
ขั้นตอน
-
นำเข้าไลบรารีที่จำเป็น ในตัวอย่าง Python ทั้งหมดต่อไปนี้ ไลบรารี Python ที่จำเป็นคือ ไฟฉาย . ตรวจสอบให้แน่ใจว่าคุณได้ติดตั้งแล้ว
-
กำหนดเทนเซอร์ PyTorch สองตัวขึ้นไปและพิมพ์ออกมา หากคุณต้องการลบปริมาณสเกลาร์ ให้นิยามมัน
-
ลบสเกลาร์หรือเทนเซอร์ออกจากเมตริกซ์อื่นโดยใช้ torch.sub() และกำหนดค่าให้กับตัวแปรใหม่ คุณยังสามารถลบปริมาณสเกลาร์จากเทนเซอร์ได้อีกด้วย การลบเทนเซอร์ด้วยวิธีนี้จะไม่ทำให้เทนเซอร์เดิมเปลี่ยนแปลง
-
พิมพ์เทนเซอร์สุดท้าย
ตัวอย่างที่ 1
ที่นี่ เราจะมีโปรแกรม Python 3 เพื่อลบปริมาณสเกลาร์จาก atensor เราจะเห็นวิธีการทำงานเดียวกันสามวิธีที่แตกต่างกัน
# Python program to perform element-wise subtraction
# import the required library
import torch
# Create a tensor
t = torch.Tensor([1.5, 2.03, 3.8, 2.9])
print("Original Tensor t:\n", t)
# Subtract a scalar value to a tensor
v = torch.sub(t, 5.60)
print("Element-wise subtraction result:\n", v)
# Same result can also be obtained as below
t1 = torch.Tensor([5.60])
w = torch.sub(t, t1)
print("Element-wise subtraction result:\n", w)
# Other way to do above operation
t2 = torch.Tensor([5.60,5.60,5.60,5.60])
x = torch.sub(t, t2)
print("Element-wise subtraction result:\n", x) ผลลัพธ์
Original Tensor t: tensor([1.5000, 2.0300, 3.8000, 2.9000]) Element-wise subtraction result: tensor([-4.1000, -3.5700, -1.8000, -2.7000]) Element-wise subtraction result: tensor([-4.1000, -3.5700, -1.8000, -2.7000]) Element-wise subtraction result: tensor([-4.1000, -3.5700, -1.8000, -2.7000])
ตัวอย่างที่ 2
โปรแกรมต่อไปนี้แสดงวิธีการลบเทนเซอร์ 1 มิติออกจากเทนเซอร์ 2 มิติ
# Import necessary library
import torch
# Create a 2D tensor
T1 = torch.Tensor([[8,7],[4,5]])
# Create a 1-D tensor
T2 = torch.Tensor([10, 5])
print("T1:\n", T1)
print("T2:\n", T2)
# Subtract 1-D tensor from 2-D tensor
v = torch.sub(T1, T2)
print("Element-wise subtraction result:\n", v) ผลลัพธ์
T1: tensor([[8., 7.], [4., 5.]]) T2: tensor([10., 5.]) Element-wise subtraction result: tensor([[-2., 2.], [-6., 0.]])
ตัวอย่างที่ 3
โปรแกรมต่อไปนี้แสดงวิธีการลบเมตริกซ์ 2 มิติออกจากเมตริกซ์ 1 มิติ
# Python program to subtract 2D tensor from 1D tensor
# Import the library
import torch
# Create a 2D tensor
T1 = torch.Tensor([[1,2],[4,5]])
# Create a 1-D tensor
T2 = torch.Tensor([10, 5])
print("T1:\n", T1)
print("T2:\n", T2)
# Subtract 2-D tensor from 1-D tensor
v = torch.sub(T2, T1)
print("Element-wise subtraction result:\n", v) ผลลัพธ์
T1: tensor([[1., 2.], [4., 5.]]) T2: tensor([10., 5.]) Element-wise subtraction result: tensor([[9., 3.], [6., 0.]])
คุณจะสังเกตได้ว่าเมตริกซ์สุดท้ายคือเมตริกซ์ 2 มิติ
ตัวอย่างที่ 4
โปรแกรมต่อไปนี้แสดงวิธีลบเทนเซอร์ 2 มิติออกจากเมตริกซ์ 2 มิติ
# import the library
import torch
# Create two 2-D tensors
T1 = torch.Tensor([[8,7],[3,4]])
T2 = torch.Tensor([[0,3],[4,9]])
print("T1:\n", T1)
print("T2:\n", T2)
# Subtract above two 2-D tensors
v = torch.sub(T1,T2)
print("Element-wise subtraction result:\n", v) ผลลัพธ์
T1: tensor([[8., 7.], [3., 4.]]) T2: tensor([[0., 3.], [4., 9.]]) Element-wise subtraction result: tensor([[ 8., 4.], [-1., -5.]])