Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

จะทำการแบ่งองค์ประกอบตามเทนเซอร์ใน PyTorch ได้อย่างไร?


ในการดำเนินการแบ่งตามองค์ประกอบบนเมตริกซ์สองตัวใน PyTorch เราสามารถใช้ torch.div() กระบวนการ. มันแบ่งแต่ละองค์ประกอบของเทนเซอร์อินพุตแรกด้วยองค์ประกอบที่สอดคล้องกันของเทนเซอร์ที่สอง เราสามารถหารเทนเซอร์ด้วยสเกลาร์ได้ด้วย เทนเซอร์สามารถหารด้วยเมตริกซ์ที่มีมิติเท่ากันหรือต่างกันได้ มิติของเทนเซอร์สุดท้ายจะเหมือนกับมิติของเทนเซอร์มิติที่สูงกว่า หากเราหารเมตริกซ์ 1 มิติด้วยเมตริกซ์ 2 มิติ เมตริกสุดท้ายจะเป็นเมตริกซ์ 2 มิติ

ขั้นตอน

  • นำเข้าไลบรารีที่จำเป็น ในตัวอย่าง Python ทั้งหมดต่อไปนี้ ไลบรารี Python ที่จำเป็นคือ ไฟฉาย . ตรวจสอบให้แน่ใจว่าคุณได้ติดตั้งแล้ว

  • กำหนดเทนเซอร์ PyTorch สองตัวขึ้นไปและพิมพ์ออกมา หากคุณต้องการหารเทนเซอร์ด้วยสเกลาร์ ให้กำหนดสเกลาร์

  • หารเมตริกซ์ด้วยเมตริกซ์หรือสเกลาร์ตัวอื่นโดยใช้ torch.div() และกำหนดค่าให้กับตัวแปรใหม่ การแบ่งเทนเซอร์ด้วยวิธีนี้จะไม่ทำให้เทนเซอร์เดิมเปลี่ยนแปลง

  • พิมพ์เทนเซอร์สุดท้าย

ตัวอย่างที่ 1

# Python program to perform element-wise division
# import the required library
import torch

# Create a tensor
t = torch.Tensor([2, 3, 5, 9])
print("Original Tensor t:\n", t)

# Divide a tensor by a scalar 4
v = torch.div(t, 4)
print("Element-wise division result:\n", v)

# Same result can also be obtained as below
t1 = torch.Tensor([4])
w = torch.div(t, t1)
print("Element-wise division result:\n", w)

# other way to do above operation
t2 = torch.Tensor([4,4,4,4])
x = torch.div(t, t2)
print("Element-wise division result:\n", x)

ผลลัพธ์

Original Tensor t:
   tensor([2., 3., 5., 9.])
Element-wise division result:
   tensor([0.5000, 0.7500, 1.2500, 2.2500])
Element-wise division result:
   tensor([0.5000, 0.7500, 1.2500, 2.2500])
Element-wise division result:
   tensor([0.5000, 0.7500, 1.2500, 2.2500])

ตัวอย่างที่ 2

โปรแกรม Python ต่อไปนี้แสดงวิธีหาร 2D tensor ด้วย 1Dtensor

# import the required library
import torch

# Create a 2D tensor
T1 = torch.Tensor([[3,2],[7,5]])

# Create a 1-D tensor
T2 = torch.Tensor([10, 8])
print("T1:\n", T1)
print("T2:\n", T2)

# Divide 2-D tensor by 1-D tensor
v = torch.div(T1, T2)
print("Element-wise division result:\n", v)

ผลลัพธ์

T1:
tensor([[3., 2.],
         [7., 5.]])
T2:
tensor([10., 8.])
Element-wise division result:
tensor([[0.3000, 0.2500],
         [0.7000, 0.6250]])

ตัวอย่างที่ 3

โปรแกรม Python ต่อไปนี้แสดงวิธีหาร 1D tensor ด้วย 2Dtensor

# Python program to dive a 1D tensor by a 2D tensor
# import the required 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)

# Divide 1-D tensor by 2-D tensor
v = torch.div(T2, T1)
print("Division 1D tensor by 2D tensor result:\n", v)

ผลลัพธ์

T1:
tensor([[8., 7.],
         [4., 5.]])
T2:
tensor([10., 5.])
Division 1D tensor by 2D tensor result:
tensor([[1.2500, 0.7143],
         [2.5000, 1.0000]])

คุณจะสังเกตได้ว่าเมตริกซ์สุดท้ายคือเมตริกซ์ 2 มิติ

ตัวอย่างที่ 4

โปรแกรม Python ต่อไปนี้แสดงวิธีหาร 2Dtensor ด้วย 2Dtensor

# import necessary library
import torch

# Create two 2-D tensors
T1 = torch.Tensor([[8,7],[3,4]])
T2 = torch.Tensor([[0,3],[4,9]])

# Print the above tensors
print("T1:\n", T1)
print("T2:\n", T2)

# Divide T1 by T2
v = torch.div(T1,T2)
print("Element-wise division result:\n", v)

ผลลัพธ์

T1:
tensor([[8., 7.],
         [3., 4.]])
T2:
tensor([[0., 3.],
         [4., 9.]])
Element-wise division result:
tensor([[ inf, 2.3333],
         [0.7500, 0.4444]])