如何在 PyTorch 中对张量执行逐元素乘法?
在本文中,我们将了解如何在Python中的 PyTorch 中对张量执行逐元素乘法。我们可以使用 torch.mul() 方法执行元素相加。
这个函数还允许我们对相同或不同维度的张量进行乘法运算。如果张量的维度不同,那么它将返回更高维度的张量。我们还可以使用 torch.mul()函数将标量与张量相乘。
Syntax: torch.mul(input, other, *, out=None)
Parameters:
- input: This is input tensor.
- other: The value or tensor that is to be multiply to every element of tensor.
- out: it is the output tensor, This is optional parameter.
Return: returns a new modified tensor..
示例 1:
下面的程序是对两个单维张量进行乘法运算。
Python3
# import torch library
import torch
# define two tensors
tens_1 = torch.Tensor([1, 2, 3, 4, 5])
tens_2 = torch.Tensor([10, 20, 30, 40, 50])
# display tensors
print(" First Tensor: ", tens_1)
print(" Second Tensor: ", tens_2)
# multiply tensors
tens = torch.mul(tens_1, tens_2)
# display result after perform element wise multiplication
print(" After Element-wise multiplication: ", tens)
Python3
# import torch library
import torch
# define a tensors
tens_1 = torch.Tensor([100, 200, 300, 400, 500])
# display tensor
print(" First Tensor: ", tens_1)
# multiply a scalar tensors
tens = torch.mul(tens_1, 2)
# display result after perform element wise multiplication
print(" After multiply 2 in tensor: ", tens)
Python3
# import torch
import torch
# Define two 2D 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)
# Multiply above two 2-D tensors
tens = torch.mul(tens_1, tens_2)
print(" After multiply 2D tensors: ", tens)
Python3
# import torch
import torch
# Define two 2D tensors
tens_1 = torch.Tensor([[10, 20], [30, 40]])
tens_2 = torch.Tensor([2, 4])
# display tensors
print(" 2D tensor: ", tens_1)
print(" 1D tensor: ", tens_2)
# Multiply above two 2-D tensors
tens = torch.mul(tens_1, tens_2)
print(" After multiply tensors: ", tens)
输出:
First Tensor: tensor([1., 2., 3., 4., 5.])
Second Tensor: tensor([10., 20., 30., 40., 50.])
After Element-wise multiplication: tensor([ 10., 40., 90., 160., 250.])
示例 2:
下面的程序是要知道如何将一个标量乘以一个张量。
Python3
# import torch library
import torch
# define a tensors
tens_1 = torch.Tensor([100, 200, 300, 400, 500])
# display tensor
print(" First Tensor: ", tens_1)
# multiply a scalar tensors
tens = torch.mul(tens_1, 2)
# display result after perform element wise multiplication
print(" After multiply 2 in tensor: ", tens)
输出:
First Tensor: tensor([100., 200., 300., 400., 500.])
After multiply 2 in tensor: tensor([ 200., 400., 600., 800., 1000.])
示例 3:
下面的程序是在二维张量上执行元素乘法。
Python3
# import torch
import torch
# Define two 2D 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)
# Multiply above two 2-D tensors
tens = torch.mul(tens_1, tens_2)
print(" After multiply 2D tensors: ", tens)
输出:
First tensor: tensor([[10., 20.],[30., 40.]])
Second tensor: tensor([[1., 2.],[3., 4.]])
After multiply 2D tensors: tensor([[ 10., 40.],[ 90., 160.]])
示例 4:
以下程序将展示如何在两个不同维度的张量上执行逐元素乘法。
Python3
# import torch
import torch
# Define two 2D tensors
tens_1 = torch.Tensor([[10, 20], [30, 40]])
tens_2 = torch.Tensor([2, 4])
# display tensors
print(" 2D tensor: ", tens_1)
print(" 1D tensor: ", tens_2)
# Multiply above two 2-D tensors
tens = torch.mul(tens_1, tens_2)
print(" After multiply tensors: ", tens)
输出:
2D tensor: tensor([[10., 20.],
[30., 40.]])
1D tensor: tensor([2., 4.])
After multiply tensors: tensor([[ 20., 80.],
[ 60., 160.]])