📜  在 Pytorch 中重塑张量

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

在 Pytorch 中重塑张量

在本文中,我们将讨论如何在 Pytorch 中重塑张量。重塑允许我们使用与 self 相同的数据和元素数量但具有指定的形状来更改形状,这意味着它返回与指定数组相同的数据,但具有不同的指定维度大小。

为演示创建张量:

用于创建一维张量并显示它的Python代码。

Python3
# import torch module
import torch
 
# create an 1 D etnsor with 8 elements
a = torch.tensor([1,2,3,4,5,6,7,8])
 
# display tensor shape
print(a.shape)
 
# display tensor
a


Python3
# import torch module
import torch
 
# create an 1 D etnsor with 8 elements
a = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8])
 
# display tensor shape
print(a.shape)
 
# display  actual tensor
print(a)
 
# reshape tensor into 4 rows and 2 columns
print(a.reshape([4, 2]))
 
# display shape of reshaped tensor
print(a.shape)


Python3
# import torch module
import torch
 
# create an 1 D etnsor with 8 elements
a = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8])
 
# display tensor shape
print(a.shape)
 
# display  actual tensor
print(a)
 
# reshape tensor into 4 rows and 2 columns
print(a.reshape([4, 2]))
 
# display shape
print(a.shape)


Python3
# import torch module
import torch
 
# create an 1 D etnsor with 8 elements
a = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8])
 
# display tensor shape
print(a.shape)
 
# display  actual tensor
print(a)
 
# reshape tensor into 8 rows and 1 column
print(a.reshape([8, 1]))
 
# display shape
print(a.shape)


Python3
# import torch module
import torch
 
# create an 2 D tensor with 8 elements each
a = torch.tensor([[1,2,3,4,5,6,7,8],
                  [1,2,3,4,5,6,7,8]])
 
# display actual tensor
print(a)
 
# flatten a tensor with flatten() function
print(torch.flatten(a))


Python3
# import torch module
import torch
 
# create an 3 D tensor with 8 elements each
a = torch.tensor([[[1,2,3,4,5,6,7,8],
                 [1,2,3,4,5,6,7,8]],
                [[1,2,3,4,5,6,7,8],
                 [1,2,3,4,5,6,7,8]]])
 
# display actual tensor
print(a)
 
# flatten a tensor with flatten() function
print(torch.flatten(a))


Python3
# importing torch module
import torch
 
# create one dimensional tensor 12 elements
a=torch.FloatTensor([24, 56, 10, 20, 30,
                     40, 50, 1, 2, 3, 4, 5]) 
 
# view tensor in 4 rows and 3 columns
print(a.view(4, 3))
  
# view tensor in 3 rows and 4 columns
print(a.view(3, 4))


Python3
# importing torch module
import torch
 
# create one dimensional tensor 10 elements
a = torch.FloatTensor([24, 56, 10, 20, 30,
                     40, 50, 1, 2, 3]) 
 
# view tensor in 10 rows and 1 column
print(a.view(10, 1))
  
# view tensor in 1 row and 10 columns
print(a.view(1, 10))


Python3
# importing torch module
import torch
 
# create one dimensional tensor
a = torch.Tensor() 
 
# resize the tensor to 4 tensors.
# each tensor with 4 rows and 5 columns
print(a.resize_(4, 4, 5))


Python3
# importing torch module
import torch
 
# create one dimensional
a = torch.Tensor() 
 
# resize the tensor to 2 tensors.
# each tensor with 4 rows and 2 columns
print(a.resize_(2, 4, 2))


Python3
# importing torch module
import torch
 
# create two dimensional tensor
a = torch.Tensor([[2,3], [1,2]]) 
 
# display shape
print(a.shape)
 
# add dimension at 0 position
added = a.unsqueeze(0)
 
print(added.shape)


Python3
# importing torch module
import torch
 
# create one dimensional tensor
a = torch.Tensor([1, 2, 3, 4, 5]) 
 
# display shape
print(a.shape)
 
# add dimension at 0 position
added = a.unsqueeze(0)
 
print(added.shape)
 
# add dimension at 1 position
added = a.unsqueeze(1)
 
print(added.shape)


输出:



torch.Size([8])
tensor([1, 2, 3, 4, 5, 6, 7, 8])

方法 1:使用 reshape() 方法

此方法用于将给定的张量重塑为给定的形状(更改尺寸)

示例 1:将一维张量重塑为二维张量的Python程序。

蟒蛇3

# import torch module
import torch
 
# create an 1 D etnsor with 8 elements
a = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8])
 
# display tensor shape
print(a.shape)
 
# display  actual tensor
print(a)
 
# reshape tensor into 4 rows and 2 columns
print(a.reshape([4, 2]))
 
# display shape of reshaped tensor
print(a.shape)

输出:

torch.Size([8])
tensor([1, 2, 3, 4, 5, 6, 7, 8])
tensor([[1, 2],
        [3, 4],
        [5, 6],
        [7, 8]])
torch.Size([8])

示例 2:将张量重塑为 4 行 2 列的Python代码

蟒蛇3



# import torch module
import torch
 
# create an 1 D etnsor with 8 elements
a = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8])
 
# display tensor shape
print(a.shape)
 
# display  actual tensor
print(a)
 
# reshape tensor into 4 rows and 2 columns
print(a.reshape([4, 2]))
 
# display shape
print(a.shape)

输出:

torch.Size([8])
tensor([1, 2, 3, 4, 5, 6, 7, 8])
tensor([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])
torch.Size([8])

示例 3:将张量重塑为 8 行和 1 列的Python代码。

蟒蛇3

# import torch module
import torch
 
# create an 1 D etnsor with 8 elements
a = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8])
 
# display tensor shape
print(a.shape)
 
# display  actual tensor
print(a)
 
# reshape tensor into 8 rows and 1 column
print(a.reshape([8, 1]))
 
# display shape
print(a.shape)

输出:

torch.Size([8])
tensor([1, 2, 3, 4, 5, 6, 7, 8])
tensor([[1],
       [2],
       [3],
       [4],
       [5],
       [6],
       [7],
       [8]])
torch.Size([8])

方法二:使用 flatten() 方法

flatten() 用于将 N 维张量展平为一维张量。

示例 1:用于创建具有 2D 元素的张量并将此向量展平的Python代码

蟒蛇3

# import torch module
import torch
 
# create an 2 D tensor with 8 elements each
a = torch.tensor([[1,2,3,4,5,6,7,8],
                  [1,2,3,4,5,6,7,8]])
 
# display actual tensor
print(a)
 
# flatten a tensor with flatten() function
print(torch.flatten(a))

输出:



tensor([[1, 2, 3, 4, 5, 6, 7, 8],
       [1, 2, 3, 4, 5, 6, 7, 8]])
tensor([1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8])

示例 2:用于创建具有 3D 元素的张量并将此向量展平的Python代码

蟒蛇3

# import torch module
import torch
 
# create an 3 D tensor with 8 elements each
a = torch.tensor([[[1,2,3,4,5,6,7,8],
                 [1,2,3,4,5,6,7,8]],
                [[1,2,3,4,5,6,7,8],
                 [1,2,3,4,5,6,7,8]]])
 
# display actual tensor
print(a)
 
# flatten a tensor with flatten() function
print(torch.flatten(a))

输出:

方法 3:使用 view() 方法

view() 用于在二维格式 IE 行和列中更改张量。我们必须指定要查看的行数和列数。



示例 1: Python程序,用于创建具有 12 个元素的张量和具有 3 行 4 列的视图,反之亦然。

蟒蛇3

# importing torch module
import torch
 
# create one dimensional tensor 12 elements
a=torch.FloatTensor([24, 56, 10, 20, 30,
                     40, 50, 1, 2, 3, 4, 5]) 
 
# view tensor in 4 rows and 3 columns
print(a.view(4, 3))
  
# view tensor in 3 rows and 4 columns
print(a.view(3, 4))

输出:

tensor([[24., 56., 10.],
       [20., 30., 40.],
       [50.,  1.,  2.],
       [ 3.,  4.,  5.]])
tensor([[24., 56., 10., 20.],
       [30., 40., 50.,  1.],
       [ 2.,  3.,  4.,  5.]])

示例 2:将张量的视图更改为 10 行和 1 列的Python代码,反之亦然。

蟒蛇3

# importing torch module
import torch
 
# create one dimensional tensor 10 elements
a = torch.FloatTensor([24, 56, 10, 20, 30,
                     40, 50, 1, 2, 3]) 
 
# view tensor in 10 rows and 1 column
print(a.view(10, 1))
  
# view tensor in 1 row and 10 columns
print(a.view(1, 10))

输出:

tensor([[24.],
       [56.],
       [10.],
       [20.],
       [30.],
       [40.],
       [50.],
       [ 1.],
       [ 2.],
       [ 3.]])
tensor([[24., 56., 10., 20., 30., 40., 50.,  1.,  2.,  3.]])

方法 4:使用 resize() 方法

这用于调整给定张量的尺寸。

示例 1: Python代码创建一个空的 D 张量并创建 4 个具有 4 行 5 列的新张量

蟒蛇3

# importing torch module
import torch
 
# create one dimensional tensor
a = torch.Tensor() 
 
# resize the tensor to 4 tensors.
# each tensor with 4 rows and 5 columns
print(a.resize_(4, 4, 5))

输出:

示例 2:创建一个包含元素的 1 D 张量并将其调整为 3 个 2 行 2 列的张量

蟒蛇3

# importing torch module
import torch
 
# create one dimensional
a = torch.Tensor() 
 
# resize the tensor to 2 tensors.
# each tensor with 4 rows and 2 columns
print(a.resize_(2, 4, 2))

输出:

方法 5:使用 unsqueeze() 方法

这用于通过在给定位置添加新维度来重塑张量。



示例 1:用于创建 2 D 张量并在 0 维度中添加维度的Python代码。

蟒蛇3

# importing torch module
import torch
 
# create two dimensional tensor
a = torch.Tensor([[2,3], [1,2]]) 
 
# display shape
print(a.shape)
 
# add dimension at 0 position
added = a.unsqueeze(0)
 
print(added.shape)

输出:

torch.Size([2, 2])
torch.Size([1, 2, 2])

示例 2:创建一维张量并添加维度的Python代码

蟒蛇3

# importing torch module
import torch
 
# create one dimensional tensor
a = torch.Tensor([1, 2, 3, 4, 5]) 
 
# display shape
print(a.shape)
 
# add dimension at 0 position
added = a.unsqueeze(0)
 
print(added.shape)
 
# add dimension at 1 position
added = a.unsqueeze(1)
 
print(added.shape)

输出:

torch.Size([5])
torch.Size([1, 5])
torch.Size([5, 1])