📌  相关文章
📜  如何在 PyTorch 中对张量执行逐元素除法?

📅  最后修改于: 2022-05-13 01:54:53.103000             🧑  作者: Mango

如何在 PyTorch 中对张量执行逐元素除法?

在本文中,我们将了解如何在 PyTorch 中执行两个张量的元素除法。要执行张量的元素划分,我们可以应用torch.div()方法。它以两个张量(除数和除数)作为输入,并返回一个具有元素除法结果的新张量。我们可以使用下面的语法来计算元素除法-

例子:

Inputs:
tensor([10., 25., 30.])
tensor([2., -5., 10.])

Output:
tensor([5., -5., 3.])

Inputs:
tensor([3., 0., 23.])
tensor([2., -5., 0.])

Output:
tensor([1.5, -0., inf])

让我们借助一些Python示例来了解张量的元素划分是如何工作的。

示例 1:

在下面的示例中,我们使用 PyTorch 方法torch.div()执行两个一维张量的元素除法。

Python3
# Python program to divide two 1D tensors
# element-wise using torch.div() method
# importing torch
import torch
 
# creating first tensor
A = torch.tensor([0.0312,  0.3401,  0.1645, -1.0781])
print("Tensor A:\n", A)
 
# creating second tensor
B = torch.tensor([-1.8584,  0.5706, -0.8994,  2.2492])
print("\nTensor B:\n", B)
 
# divide A by B
result = torch.div(A, B)
print("\nElement-wise Division Output:\n", result)


Python3
# Python program to divide two
# 2D tensors element-wise
# importing torch
import torch
 
# defining first 2D tensor
a = torch. tensor([[-1.8665,  0.6341,  0.8920],
                   [-0.1712,  0.3949,  1.9414],
                   [-1.2088, -1.0375, -1.3087],
                   [0.9161, -0.2972,  1.5289]])
print("Tensor a:\n", a)
 
# defining second 2D tensor
# b = torch.randn(4,3)
b = torch. tensor([[-0.2187,  0.5252, -0.5840],
                   [1.5293, -0.4514,  1.8490],
                   [-0.7269, -0.1561, -0.0629],
                   [-0.5379, -0.9751,  0.6541]])
print("\nTensor b:\n", b)
 
# computing element-wise division
print("\nElement-wise Division:")
result1 = torch.div(a, b)
print("\nResult:\n", result1)
result2 = torch.div(a, b, rounding_mode='trunc')
print("\nResult with rounding_mode='trunc':\n", result2)
result3 = torch.div(a, b, rounding_mode='floor')
print("\nResult with rounding_mode='floor':\n", result3)


Python3
# Python program to divide a 3D tensor by
# a 1D tensor element-wise
# Importing torch
import torch
 
# defining tensors
a = torch.randn(3, 2, 2)
b = torch.randn(2)
 
# printing the matrices
print("Tensor a :\n", a)
print("\nTensor b :\n", b)
 
# divide tensor a by b
result = torch.div(a, b)
print("\nElementwise Division Output :\n", result)


输出:

Tensor A:
 tensor([ 0.0312,  0.3401,  0.1645, -1.0781])

Tensor B:
 tensor([-1.8584,  0.5706, -0.8994,  2.2492])

Element-wise Division Output:
 tensor([-0.0168,  0.5960, -0.1829, -0.4793])

示例 2:

在下面的示例中,我们使用 PyTorch 方法torch.div()执行 2D 张量与 2D 张量的元素划分。我们还应用了不同的舍入模式。

Python3

# Python program to divide two
# 2D tensors element-wise
# importing torch
import torch
 
# defining first 2D tensor
a = torch. tensor([[-1.8665,  0.6341,  0.8920],
                   [-0.1712,  0.3949,  1.9414],
                   [-1.2088, -1.0375, -1.3087],
                   [0.9161, -0.2972,  1.5289]])
print("Tensor a:\n", a)
 
# defining second 2D tensor
# b = torch.randn(4,3)
b = torch. tensor([[-0.2187,  0.5252, -0.5840],
                   [1.5293, -0.4514,  1.8490],
                   [-0.7269, -0.1561, -0.0629],
                   [-0.5379, -0.9751,  0.6541]])
print("\nTensor b:\n", b)
 
# computing element-wise division
print("\nElement-wise Division:")
result1 = torch.div(a, b)
print("\nResult:\n", result1)
result2 = torch.div(a, b, rounding_mode='trunc')
print("\nResult with rounding_mode='trunc':\n", result2)
result3 = torch.div(a, b, rounding_mode='floor')
print("\nResult with rounding_mode='floor':\n", result3)

输出:

Tensor a:
 tensor([[-1.8665,  0.6341,  0.8920],
        [-0.1712,  0.3949,  1.9414],
        [-1.2088, -1.0375, -1.3087],
        [ 0.9161, -0.2972,  1.5289]])

Tensor b:
 tensor([[-0.2187,  0.5252, -0.5840],
        [ 1.5293, -0.4514,  1.8490],
        [-0.7269, -0.1561, -0.0629],
        [-0.5379, -0.9751,  0.6541]])

Element-wise Division:

Result:
 tensor([[ 8.5345,  1.2073, -1.5274],
        [-0.1119, -0.8748,  1.0500],
        [ 1.6630,  6.6464, 20.8060],
        [-1.7031,  0.3048,  2.3374]])

Result with rounding_mode='trunc':
 tensor([[ 8.,  1., -1.],
        [-0., -0.,  1.],
        [ 1.,  6., 20.],
        [-1.,  0.,  2.]])

Result with rounding_mode='floor':
 tensor([[ 8.,  1., -2.],
        [-1., -1.,  1.],
        [ 1.,  6., 20.],
        [-2.,  0.,  2.]])

示例 3:

在下面的示例中,我们使用 PyTorch 方法torch.div()执行 3-D 张量与 1-D 张量的元素除法。

Python3

# Python program to divide a 3D tensor by
# a 1D tensor element-wise
# Importing torch
import torch
 
# defining tensors
a = torch.randn(3, 2, 2)
b = torch.randn(2)
 
# printing the matrices
print("Tensor a :\n", a)
print("\nTensor b :\n", b)
 
# divide tensor a by b
result = torch.div(a, b)
print("\nElementwise Division Output :\n", result)

输出:

Tensor a :
 tensor([[[-0.7549,  1.8301],
         [-0.5545,  1.3180]],

        [[ 0.1159,  0.8394],
         [ 0.0452, -1.2860]],

        [[ 0.3850, -0.9654],
         [-1.5530, -0.8627]]])

Tensor b :
 tensor([-1.2378,  0.2153])

Elementwise Division Output :
 tensor([[[ 0.6098,  8.5011],
         [ 0.4480,  6.1226]],

        [[-0.0937,  3.8991],
         [-0.0365, -5.9739]],

        [[-0.3110, -4.4844],
         [ 1.2546, -4.0074]]])