📜  如何在 PyTorch 中压缩和解压缩张量?

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

如何在 PyTorch 中压缩和解压缩张量?

在本文中,我们将了解如何压缩和解压缩 PyTorch 张量。

要压缩张量,我们可以应用torch.squeeze()方法,而要取消压缩张量,我们可以使用torch.unsqueeze()方法。让我们详细了解这些方法。

挤压张量:

当我们压缩一个张量时,大小为 1 的维度被删除。原始张量的元素按剩余维度排列。例如,如果输入张量的形状为:(m×1×n×1),则压缩后的输出张量的形状为:(m×n)。以下是 torch.squeeze() 方法的语法。

请注意,我们可以在特定维度dim中压缩输入张量。在这种情况下,其他尺寸为 1 的尺寸将保持不变。我们已经更详细地讨论了示例 2。

示例 1:

在下面的示例中,我们使用 torch.squeeze() 方法挤压 5D 张量。输入张量具有大小为 1 的两个维度。

Python3
# Python program to squeeze the tensor
# importing torch
import torch
  
# creating the input tensor
input = torch.randn(3,1,2,1,4)
# print the input tensaor
print("Input tensor Size:\n",input.size())
  
# squeeze the tensor
output = torch.squeeze(input)
# print the squeezed tensor
print("Size after squeeze:\n",output.size())


Python3
# Python program to squeeze the tensor in 
# different dimensions
  
# importing torch
import torch
# creating the input tensor
input = torch.randn(3,1,2,1,4)
print("Dimension of input tensor:", input.dim())
print("Input tensor Size:\n",input.size())
  
# squeeze the tensor in dimension 0
output = torch.squeeze(input,dim=0)
print("Size after squeeze with dim=0:\n",
      output.size())
  
# squeeze the tensor in dimension 0
output = torch.squeeze(input,dim=1)
print("Size after squeeze with dim=1:\n",
      output.size())
  
# squeeze the tensor in dimension 0
output = torch.squeeze(input,dim=2)
print("Size after squeeze with dim=2:\n",
      output.size())
  
# squeeze the tensor in dimension 0
output = torch.squeeze(input,dim=3)
print("Size after squeeze with dim=3:\n",
      output.size())
  
# squeeze the tensor in dimension 0
output = torch.squeeze(input,dim=4)
print("Size after squeeze with dim=4:\n",
      output.size())
# output = torch.squeeze(input,dim=5) # Error


Python3
# Python program to unsqueeze the input tensor
  
# importing torch
import torch
  
# define the input tensor
input = torch.arange(8, dtype=torch.float)
print("Input tensor:\n", input)
print("Size of input Tensor before unsqueeze:\n",
      input.size())
  
output = torch.unsqueeze(input, dim=0)
print("Tensor after unsqueeze with dim=0:\n", output)
print("Size after unsqueeze with dim=0:\n",
      output.size())
  
output = torch.unsqueeze(input, dim=1)
print("Tensor after unsqueeze with dim=1:\n", output)
print("Size after unsqueeze with dim=1:\n",
      output.size())


输出:

Input tensor Size:
 torch.Size([3, 1, 2, 1, 4])
Size after squeeze:
 torch.Size([3, 2, 4])

请注意,大小为 1 的两个维度都在压缩张量中被删除。

示例 2:

在这个例子中,我们将张量压缩到不同的维度。

Python3

# Python program to squeeze the tensor in 
# different dimensions
  
# importing torch
import torch
# creating the input tensor
input = torch.randn(3,1,2,1,4)
print("Dimension of input tensor:", input.dim())
print("Input tensor Size:\n",input.size())
  
# squeeze the tensor in dimension 0
output = torch.squeeze(input,dim=0)
print("Size after squeeze with dim=0:\n",
      output.size())
  
# squeeze the tensor in dimension 0
output = torch.squeeze(input,dim=1)
print("Size after squeeze with dim=1:\n",
      output.size())
  
# squeeze the tensor in dimension 0
output = torch.squeeze(input,dim=2)
print("Size after squeeze with dim=2:\n",
      output.size())
  
# squeeze the tensor in dimension 0
output = torch.squeeze(input,dim=3)
print("Size after squeeze with dim=3:\n",
      output.size())
  
# squeeze the tensor in dimension 0
output = torch.squeeze(input,dim=4)
print("Size after squeeze with dim=4:\n",
      output.size())
# output = torch.squeeze(input,dim=5) # Error

输出:

Dimension of input tensor: 5
Input tensor Size:
 torch.Size([3, 1, 2, 1, 4])
Size after squeeze with dim=0:
 torch.Size([3, 1, 2, 1, 4])
Size after squeeze with dim=1:
 torch.Size([3, 2, 1, 4])
Size after squeeze with dim=2:
 torch.Size([3, 1, 2, 1, 4])
Size after squeeze with dim=3:
 torch.Size([3, 1, 2, 4])
Size after squeeze with dim=4:
 torch.Size([3, 1, 2, 1, 4])

请注意,当我们在维度 0 中压缩张量时,输出张量的形状没有变化。当我们在维度 1 或维度 3(两者的大小都是 1)中进行压缩时,输出张量中仅移除该维度。当我们在维度 2 或维度 4 中进行压缩时,输出张量的形状没有变化。

解压张量:

当我们解压张量时,在指定位置插入一个大小为 1 的新维度。总是一个 unsqueeze 操作会增加输出张量的维度。例如,如果输入张量的形状为:(m×n),并且我们想在位置 1 处插入一个新维度,那么 unsqueeze 后的输出张量的形状为:(m×1×n)。以下是torch.unsqueeze()方法的语法 -

请注意,我们可以从范围 [-input.dim() – 1, input.dim() + 1) 中选择暗淡值。负的 dim 对应于 dim = dim + input.dim() + 1。

示例 3:

在下面的示例中,我们将一维张量解压缩为二维张量。

Python3

# Python program to unsqueeze the input tensor
  
# importing torch
import torch
  
# define the input tensor
input = torch.arange(8, dtype=torch.float)
print("Input tensor:\n", input)
print("Size of input Tensor before unsqueeze:\n",
      input.size())
  
output = torch.unsqueeze(input, dim=0)
print("Tensor after unsqueeze with dim=0:\n", output)
print("Size after unsqueeze with dim=0:\n",
      output.size())
  
output = torch.unsqueeze(input, dim=1)
print("Tensor after unsqueeze with dim=1:\n", output)
print("Size after unsqueeze with dim=1:\n",
      output.size())

输出:

Input tensor:
 tensor([0., 1., 2., 3., 4., 5., 6., 7.])
Size of input Tensor before unsqueeze:
 torch.Size([8])
Tensor after unsqueeze with dim=0:
 tensor([[0., 1., 2., 3., 4., 5., 6., 7.]])
Size after unsqueeze with dim=0:
 torch.Size([1, 8])
Tensor after unsqueeze with dim=1:
 tensor([[0.],
        [1.],
        [2.],
        [3.],
        [4.],
        [5.],
        [6.],
        [7.]])
Size after unsqueeze with dim=1:
 torch.Size([8, 1])