📜  如何在 PyTorch 中调整张量的大小?

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

如何在 PyTorch 中调整张量的大小?

在本文中,我们将讨论如何在 Pytorch 中调整张量的大小。 Resize 允许我们改变张量的大小。我们有多种方法可以在 PyTorch 中调整张量的大小。让我们讨论一下可用的方法。

方法一:使用 view() 方法

我们可以使用 view() 方法在 PyTorch 中调整张量的大小。 view() 方法允许我们更改张量的维度,但始终确保张量中的元素总数在调整张量大小之前和之后必须匹配。以下语法用于调整张量的大小。

示例 1:

以下程序是使用 view() 在 PyTorch 中调整一维张量的大小

Python
# Import the torch library
import torch
 
# define a tensor
tens = torch.Tensor([10, 20, 30, 40, 50, 60])
print("Original Tensor: ", tens)
 
# below is the different ways to resize
# tensor to 2x3 using view()
 
# Resize tensor to 2x3
tens_1 = tens.view(2, 3)
print(" Tensor After Resize: \n", tens_1)
 
# other way resize tensor to 2x3
tens_2 = tens.view(2, -1)
print(" Tensor after resize: \n", tens_2)
 
# Other way to resize tensor to 2x3
tens_3 = tens.view(-1, 3)
print(" Tensor after resize: \n", tens_3)


Python
# Import the torch library
import torch
 
# define a tensor
tens = torch.Tensor([[1, 2, 3], [4, 5, 6],
                     [7, 8, 9], [10, 11, 12]])
print("Original Tensor: \n", tens)
 
# below is the different ways to use view()
 
# Resize tensor to 3x4
tens_1 = tens.view(2, -1)
print("\n Tensor After Resize: \n", tens_1)
 
# other way resize tensor to 3x4
tens_2 = tens.view(-1, 4)
print("\n Tensor after resize: \n", tens_2)
 
# Other way to resize tensor to 3x4
tens_3 = tens.view(3, -1)
print("\n Tensor after resize: \n", tens_3)


Python
# import torch module
import torch
 
# Define an 1D tensor
tens = torch.tensor([10, 20, 30, 40, 50, 60, 70, 80])
 
# display tensor
print("\n Original 1D Tensor: ", tens)
 
# resize this tensor into 2x4
tens_1 = tens.reshape([2, 4])
print("\n After Resize this Tensor to 2x4 : \n", tens_1)
 
# resize this tensor into 4x2
tens_2 = tens.reshape([4, 2])
print("\n After Resize this Tensor to 4x2 : \n", tens_2)


Python
# import torch module
import torch
 
# Define an 2D tensor
tens = torch.Tensor([[1, 2, 3], [4, 5, 6],
                     [7, 8, 9], [10, 11, 12]])
 
# display tensor
print(" Original 2D Tensor: \n", tens)
 
# resize this tensor to 2x6
tens_1 = tens.reshape([2, 6])
print("\n After Resize this Tensor to 2x6 : \n", tens_1)
 
# resize this tensor into 6x2
tens_2 = tens.reshape([6, 2])
print("\n After Resize this Tensor to 6x2 : \n", tens_2)


Python
# import torch module
import torch
 
# Define an 1D tensor
tens = torch.Tensor([11, 12, 13, 14, 15, 16, 17, 18])
 
# display tensor
print("\n Original 2D Tensor: \n", tens)
 
# resize the tensor to 4 tensors.
# each tensor with 4 rows and 5 columns
tens_1 = tens.resize_(4, 4, 5)
print("\n After resize tensor: \n", tens_1)


Python
# import torch library
import torch
 
# define a tensor
tens = torch.Tensor([10, 20, 30, 40, 50])
 
# display tensor and it's size
print("\n Original Tensor: ", tens)
 
# Squeeze the tensor in dimension 1
tens_1 = torch.unsqueeze(tens, dim=1)
print("\n After resize tensor to 5x1: \n", tens_1)


输出:

示例 2:

以下程序是使用 view() 在 PyTorch 中调整 2D 张量的大小。

Python

# Import the torch library
import torch
 
# define a tensor
tens = torch.Tensor([[1, 2, 3], [4, 5, 6],
                     [7, 8, 9], [10, 11, 12]])
print("Original Tensor: \n", tens)
 
# below is the different ways to use view()
 
# Resize tensor to 3x4
tens_1 = tens.view(2, -1)
print("\n Tensor After Resize: \n", tens_1)
 
# other way resize tensor to 3x4
tens_2 = tens.view(-1, 4)
print("\n Tensor after resize: \n", tens_2)
 
# Other way to resize tensor to 3x4
tens_3 = tens.view(3, -1)
print("\n Tensor after resize: \n", tens_3)

输出:

方法二:使用 reshape() 方法

此方法也用于调整张量的大小。此方法返回一个具有修改大小的新张量。以下语法用于使用 reshape() 方法调整张量的大小。

示例 3:

下面的程序是要知道如何将一维张量调整为二维张量。

Python

# import torch module
import torch
 
# Define an 1D tensor
tens = torch.tensor([10, 20, 30, 40, 50, 60, 70, 80])
 
# display tensor
print("\n Original 1D Tensor: ", tens)
 
# resize this tensor into 2x4
tens_1 = tens.reshape([2, 4])
print("\n After Resize this Tensor to 2x4 : \n", tens_1)
 
# resize this tensor into 4x2
tens_2 = tens.reshape([4, 2])
print("\n After Resize this Tensor to 4x2 : \n", tens_2)

输出:

示例 4:

以下程序是了解如何使用 reshape() 方法调整 2D 张量的大小。

Python

# import torch module
import torch
 
# Define an 2D tensor
tens = torch.Tensor([[1, 2, 3], [4, 5, 6],
                     [7, 8, 9], [10, 11, 12]])
 
# display tensor
print(" Original 2D Tensor: \n", tens)
 
# resize this tensor to 2x6
tens_1 = tens.reshape([2, 6])
print("\n After Resize this Tensor to 2x6 : \n", tens_1)
 
# resize this tensor into 6x2
tens_2 = tens.reshape([6, 2])
print("\n After Resize this Tensor to 6x2 : \n", tens_2)

输出:

方法 3:使用 resize() 方法

这个方法也用于在 PyTorch 中调整张量的大小,下面的语法帮助我们调整张量的大小。

示例 5:

下面的程序是了解如何使用 resize() 方法调整张量的大小。

Python

# import torch module
import torch
 
# Define an 1D tensor
tens = torch.Tensor([11, 12, 13, 14, 15, 16, 17, 18])
 
# display tensor
print("\n Original 2D Tensor: \n", tens)
 
# resize the tensor to 4 tensors.
# each tensor with 4 rows and 5 columns
tens_1 = tens.resize_(4, 4, 5)
print("\n After resize tensor: \n", tens_1)

输出:

方法四:使用 unsqueeze() 方法

这用于通过在给定位置添加新维度来调整张量的大小。下面的语法用于使用 unsqueeze() 方法调整张量的大小。

示例 6:

以下程序是使用 unsqueeze() 方法调整张量的大小。

Python

# import torch library
import torch
 
# define a tensor
tens = torch.Tensor([10, 20, 30, 40, 50])
 
# display tensor and it's size
print("\n Original Tensor: ", tens)
 
# Squeeze the tensor in dimension 1
tens_1 = torch.unsqueeze(tens, dim=1)
print("\n After resize tensor to 5x1: \n", tens_1)

输出: