如何在 PyTorch 中对张量执行逐元素减法?
在本文中,我们将了解如何在Python中的 PyTorch 中对张量执行逐元素减法。我们可以使用 torch.sub() 方法执行逐元素减法。
torch.sub() 方法允许我们对相同或不同维度的张量进行减法运算。它将两个张量作为输入,并返回一个带有结果的新张量(逐元素减法)。如果张量的维度不同,那么它将返回更高维度的张量。我们还可以使用 torch.sub()函数用张量减去标量。我们可以使用下面的语法来计算元素减法。
Syntax: torch.sub(input, other, *, alpha=1, out=None)
Parameters:
- input: the input tensor.
- other: This is tensor or number to subtract from the input tensor.
- alpha (Number): the parameter is multiplier for other.
- out: it is the output tensor, This is optional parameter.
Return: it will returns a new modified tensor with element-wise subtraction of the tensor input by the tensor other.
示例 1:
下面的程序是对两个单维张量执行逐元素减法。
Python3
# import torch library
import torch
# define two tensors
tens_1 = torch.Tensor([10, 20, 30, 40, 50])
tens_2 = torch.Tensor([1, 2, 3, 4, 5])
# display tensors
print(" First Tensor: ", tens_1)
print(" Second Tensor: ", tens_2)
# multiply tensors
tens = torch.sub(tens_1, tens_2)
# display result after perform element wise
# subtraction
print(" After Element-wise subtraction: ", tens)
Python3
# import torch library
import torch
# define two tensors
tens_1 = torch.Tensor([[10, 20], [30, 40]])
tens_2 = torch.Tensor([[1, 2], [3, 4]])
# display tensors
print("First Tensor:", tens_1)
print("Second Tensor:", tens_2)
# Subtract tensors
tens = torch.sub(tens_1, tens_2)
# display result after perform element wise
# subtraction
print("After Element-wise subtraction:", tens)
Python3
# import torch library
import torch
# define a tensor
tens = torch.Tensor([100, 200, 300, 400, 500])
# display tensors
print(tens)
# Subtract 50 from tensor
tens_result = torch.sub(tens, 50)
# display result after subtract scalar from tensor
print("After subtract scalar from tensor:", tens_result)
Python3
# import torch library
import torch
# define 2D tensor
tens_1 = torch.Tensor([[100, 200], [300, 400]])
# define 1D tensor
tens_2 = torch.Tensor([10, 20])
# display tensors
print("First Tensor:", tens_1)
print("Second Tensor:", tens_2)
# Subtract tensors
tens = torch.sub(tens_1, tens_2)
# display result after perform element wise subtraction
print("After Element-wise subtraction:", tens)
输出:
示例 2:
下面的程序是对两个二维张量执行逐元素减法。
Python3
# import torch library
import torch
# define two tensors
tens_1 = torch.Tensor([[10, 20], [30, 40]])
tens_2 = torch.Tensor([[1, 2], [3, 4]])
# display tensors
print("First Tensor:", tens_1)
print("Second Tensor:", tens_2)
# Subtract tensors
tens = torch.sub(tens_1, tens_2)
# display result after perform element wise
# subtraction
print("After Element-wise subtraction:", tens)
输出:
示例 3:
下面的程序是要知道如何将一个标量减去一个张量。
Python3
# import torch library
import torch
# define a tensor
tens = torch.Tensor([100, 200, 300, 400, 500])
# display tensors
print(tens)
# Subtract 50 from tensor
tens_result = torch.sub(tens, 50)
# display result after subtract scalar from tensor
print("After subtract scalar from tensor:", tens_result)
输出:
示例 4:
下面的程序是为了了解如何对两个不同维度的张量进行逐元素减法。
Python3
# import torch library
import torch
# define 2D tensor
tens_1 = torch.Tensor([[100, 200], [300, 400]])
# define 1D tensor
tens_2 = torch.Tensor([10, 20])
# display tensors
print("First Tensor:", tens_1)
print("Second Tensor:", tens_2)
# Subtract tensors
tens = torch.sub(tens_1, tens_2)
# display result after perform element wise subtraction
print("After Element-wise subtraction:", tens)
输出: