📜  如何在 PyTorch 中加入张量?

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

如何在 PyTorch 中加入张量?

在本文中,我们将了解如何在 PyTorch 中连接两个或更多张量。

我们可以使用 torch.cat() 和 torch.stack() 函数在 PyTorch 中加入张量。这两个函数都可以帮助我们连接张量,但 torch.cat() 基本上用于连接给定维度中给定的张量序列。而 torch.stack()函数允许我们堆叠张量,我们可以连接两个或多个不同维度的张量,例如 -1 维和 0 维,

torch.cat()函数: PyTorch 中的 Cat() 用于连接相同维度的两个或多个张量。

torch.stack()函数:此函数还连接一系列张量,但在一个新维度上,这里的张量也应该具有相同的大小。

示例 1:

以下程序是使用 torch.cat()函数连接一系列张量。

Python3
# import torch library
import torch
  
# define tensors
tens_1 = torch.Tensor([[11, 12, 13], [14, 15, 16]])
tens_2 = torch.Tensor([[17, 18, 19], [20, 21, 22]])
  
# print first tensors
print("tens_1 \n", tens_1)
  
# print second tensor
print("tens_2 \n", tens_2)
  
# call torch,cat() function
# join tensor in -1 dimension
tens = torch.cat((tens_1, tens_2), -1)
print("join tensors in the -1 dimension \n", tens)
  
# join tensor in 0 dimension
tens = torch.cat((tens_1, tens_2), 0)
print("join tensors in the 0 dimension \n", tens)


Python3
# import torch library
import torch
  
# define tensors
tens_1 = torch.Tensor([[10,20,30],[40,50,60]])
tens_2 = torch.Tensor([[70,80,90],[100,110,120]])
  
# print first tensors
print("tens_1 \n", tens_1)
  
# print second tensor
print("tens_2 \n", tens_2)
  
# call torch,cat() function
# join tensor in -1 dimension
tens = torch.stack((tens_1, tens_2), -1)
print("join tensors in the -1 dimension \n", tens)
  
# join tensor in 0 dimension
tens = torch.stack((tens_1, tens_2), 0)
print("join tensors in the 0 dimension \n", tens)


Python3
# import required library
import torch
  
# define some tensors
tens_1 = torch.Tensor([[1, 2], [3, 4]])
tens_2 = torch.Tensor([[5, 6], [7, 8]])
tens_3 = torch.Tensor([[9, 10], [11, 12]])
  
# display tensors
print("\n First Tensor :\n", tens_1)
print("\n Second Tensor :\n", tens_2)
print("\n Third Tensor :\n", tens_3)
  
# Join (stacked) tensors in -1 dimension
tens = torch.stack((tens_1, tens_2, tens_3), -1)
print("\n tensors in -1 dimension \n", tens)
  
# Join (stacked) tensors in 0 dimension
tens = torch.stack((tens_1, tens_2, tens_3), 0)
print("\n tensors in 0 dimension \n", tens)


Python3
# import required library
import torch
  
# define some tensors
tens_1 = torch.Tensor([[1, 2], [3, 4]])
tens_2 = torch.Tensor([[5, 6], [7, 8]])
tens_3 = torch.Tensor([[9, 10], [11, 12]])
  
# display tensors
print("First Tensor :\n", tens_1)
print("\nSecond Tensor :\n", tens_2)
print("\nThird Tensor :\n", tens_3)
  
# join tensors in the 0 dimension
tens = torch.cat((tens_1, tens_2, tens_3), 0)
print("\n join tensors in the 0 dimension \n", tens)
  
# join tensors in the -1 dimension
tens = torch.cat((tens_1, tens_2, tens_3), -1)
print("\n join tensors in the -1 dimension \n", tens)


Python3
# import required library
import torch
  
# define some tensors
tens_1 = torch.Tensor([1, 2, 3])
tens_2 = torch.Tensor([4, 5, 6])
tens_3 = torch.Tensor([7, 8, 9])
  
# display tensors
print("First Tensor :\n", tens_1)
print("\nSecond Tensor :\n", tens_2)
print("\nThird Tensor :\n", tens_3)
  
# join tensors in the 0 dimension
tens = torch.stack((tens_1, tens_2, tens_3), 0)
print("\n join tensors in the 0 dimension \n", tens)
  
# join tensors in the -1 dimension
tens = torch.stack((tens_1, tens_2, tens_3), -1)
print("\n join tensors in the -1 dimension \n", tens)


输出:

示例 2:

以下程序是使用 torch.stack()函数连接一系列张量。

Python3

# import torch library
import torch
  
# define tensors
tens_1 = torch.Tensor([[10,20,30],[40,50,60]])
tens_2 = torch.Tensor([[70,80,90],[100,110,120]])
  
# print first tensors
print("tens_1 \n", tens_1)
  
# print second tensor
print("tens_2 \n", tens_2)
  
# call torch,cat() function
# join tensor in -1 dimension
tens = torch.stack((tens_1, tens_2), -1)
print("join tensors in the -1 dimension \n", tens)
  
# join tensor in 0 dimension
tens = torch.stack((tens_1, tens_2), 0)
print("join tensors in the 0 dimension \n", tens)

输出:

示例 3:

以下程序用于连接(堆叠)2D 张量以创建 3D 张量。

Python3

# import required library
import torch
  
# define some tensors
tens_1 = torch.Tensor([[1, 2], [3, 4]])
tens_2 = torch.Tensor([[5, 6], [7, 8]])
tens_3 = torch.Tensor([[9, 10], [11, 12]])
  
# display tensors
print("\n First Tensor :\n", tens_1)
print("\n Second Tensor :\n", tens_2)
print("\n Third Tensor :\n", tens_3)
  
# Join (stacked) tensors in -1 dimension
tens = torch.stack((tens_1, tens_2, tens_3), -1)
print("\n tensors in -1 dimension \n", tens)
  
# Join (stacked) tensors in 0 dimension
tens = torch.stack((tens_1, tens_2, tens_3), 0)
print("\n tensors in 0 dimension \n", tens)

输出:

示例 4:

下面的程序是要知道二维张量是如何沿着 0 和 -1 维度连接的。在 0 维中连接会增加行数。

Python3

# import required library
import torch
  
# define some tensors
tens_1 = torch.Tensor([[1, 2], [3, 4]])
tens_2 = torch.Tensor([[5, 6], [7, 8]])
tens_3 = torch.Tensor([[9, 10], [11, 12]])
  
# display tensors
print("First Tensor :\n", tens_1)
print("\nSecond Tensor :\n", tens_2)
print("\nThird Tensor :\n", tens_3)
  
# join tensors in the 0 dimension
tens = torch.cat((tens_1, tens_2, tens_3), 0)
print("\n join tensors in the 0 dimension \n", tens)
  
# join tensors in the -1 dimension
tens = torch.cat((tens_1, tens_2, tens_3), -1)
print("\n join tensors in the -1 dimension \n", tens)

输出:

示例 5:

下面的程序是要知道一维张量是如何堆叠的,最终的张量是一个二维张量。

Python3

# import required library
import torch
  
# define some tensors
tens_1 = torch.Tensor([1, 2, 3])
tens_2 = torch.Tensor([4, 5, 6])
tens_3 = torch.Tensor([7, 8, 9])
  
# display tensors
print("First Tensor :\n", tens_1)
print("\nSecond Tensor :\n", tens_2)
print("\nThird Tensor :\n", tens_3)
  
# join tensors in the 0 dimension
tens = torch.stack((tens_1, tens_2, tens_3), 0)
print("\n join tensors in the 0 dimension \n", tens)
  
# join tensors in the -1 dimension
tens = torch.stack((tens_1, tens_2, tens_3), -1)
print("\n join tensors in the -1 dimension \n", tens)

输出: