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

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

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

在本文中,我们将了解如何在Python中的 PyTorch 中对张量执行逐元素加法。我们可以使用 torch.add()函数执行逐元素加法。

这个函数还允许我们对相同或不同维度的张量进行加法。如果张量的维度不同,那么它将返回更高维度的张量。

示例 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("\nFirst Tensor \n", tens_1)
print("\nsecond Tensor \n", tens_2)
  
# Add both the tensor
tens = torch.add(tens_1, tens_2)
print("\n After Element-wise Addition\n", tens)


Python3
# import the torch library
import torch
  
# define 2D tensors
tens_1 = torch.Tensor([[1, 2], [3, 4]])
tens_2 = torch.Tensor([[10, 20], [20, 40]])
  
# display these tensors
print("First Tensor \n", tens_1)
print("second Tensor \n", tens_2)
  
# perform addition on these tensors
tens = torch.add(tens_1, tens_2)
  
# display final result
print("\n After Element-wise Addition\n", tens)


Python3
# import torch library
import torch
  
# define 1D tensor
tens_1 = torch.Tensor([1, 2])
  
# define 2D tensor
tens_2 = torch.Tensor([[10, 20], [20, 40]])
  
# display tensors
print("\nFirst Tensor \n", tens_1)
print("\nsecond Tensor \n", tens_2)
  
# perform addition on tensors
tens = torch.add(tens_1, tens_2)
  
# display final output
print("\n After Element-wise Addition\n", tens)


Python3
# Python program to perform element-wise Addition
# import the required library
import torch
  
# define a tensor
tens_1 = torch.Tensor([1, 2, 3, 4])
  
# define 2D tensor
tens_2 = torch.Tensor([[10, 20], [30, 40]])
  
# display tensors
print("\nFirst Tensor \n", tens_1)
print("second Tensor \n", tens_2)
  
# perform addition on 1D tensors
t1 = torch.add(tens_1, 10)
# display final output
print("\n After adding scalar quantity to 1D tensor: \n", t1)
  
# perform addition on 2D tensors
t2 = torch.add(tens_2, 20)
# display final output
print("\n After adding scalar quantity to 2D tensor: \n", t2)


输出:

示例 2:以下程序是对 2D 张量执行逐元素加法。

Python3

# import the torch library
import torch
  
# define 2D tensors
tens_1 = torch.Tensor([[1, 2], [3, 4]])
tens_2 = torch.Tensor([[10, 20], [20, 40]])
  
# display these tensors
print("First Tensor \n", tens_1)
print("second Tensor \n", tens_2)
  
# perform addition on these tensors
tens = torch.add(tens_1, tens_2)
  
# display final result
print("\n After Element-wise Addition\n", tens)

输出:

示例 3:以下程序将展示如何对两个不同维度的张量执行逐元素加法。

Python3

# import torch library
import torch
  
# define 1D tensor
tens_1 = torch.Tensor([1, 2])
  
# define 2D tensor
tens_2 = torch.Tensor([[10, 20], [20, 40]])
  
# display tensors
print("\nFirst Tensor \n", tens_1)
print("\nsecond Tensor \n", tens_2)
  
# perform addition on tensors
tens = torch.add(tens_1, tens_2)
  
# display final output
print("\n After Element-wise Addition\n", tens)

输出:

示例 4:下面的程序是要知道如何将标量添加到张量。

Python3

# Python program to perform element-wise Addition
# import the required library
import torch
  
# define a tensor
tens_1 = torch.Tensor([1, 2, 3, 4])
  
# define 2D tensor
tens_2 = torch.Tensor([[10, 20], [30, 40]])
  
# display tensors
print("\nFirst Tensor \n", tens_1)
print("second Tensor \n", tens_2)
  
# perform addition on 1D tensors
t1 = torch.add(tens_1, 10)
# display final output
print("\n After adding scalar quantity to 1D tensor: \n", t1)
  
# perform addition on 2D tensors
t2 = torch.add(tens_2, 20)
# display final output
print("\n After adding scalar quantity to 2D tensor: \n", t2)

输出: