tensor.permute()
和 tensor.view()
有什么区别?
他们似乎做同样的事情。
In [12]: aten = torch.tensor([[1, 2, 3], [4, 5, 6]])
In [13]: aten
Out[13]:
tensor([[ 1, 2, 3],
[ 4, 5, 6]])
In [14]: aten.shape
Out[14]: torch.Size([2, 3])
torch.view()
将张量重塑为不同但兼容的形状。例如,我们的输入张量 aten
的形状为 (2, 3)
。这可以被视为形状为(6, 1)
、(1, 6)
等形状的张量,
# reshaping (or viewing) 2x3 matrix as a column vector of shape 6x1
In [15]: aten.view(6, -1)
Out[15]:
tensor([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6]])
In [16]: aten.view(6, -1).shape
Out[16]: torch.Size([6, 1])
或者,它也可以被重塑或viewed为形状为
(1, 6)
的行向量,如下所示:
In [19]: aten.view(-1, 6)
Out[19]: tensor([[ 1, 2, 3, 4, 5, 6]])
In [20]: aten.view(-1, 6).shape
Out[20]: torch.Size([1, 6])
而
tensor.permute()
仅用于交换轴。下面的例子会让事情变得清楚:
In [39]: aten
Out[39]:
tensor([[ 1, 2, 3],
[ 4, 5, 6]])
In [40]: aten.shape
Out[40]: torch.Size([2, 3])
# swapping the axes/dimensions 0 and 1
In [41]: aten.permute(1, 0)
Out[41]:
tensor([[ 1, 4],
[ 2, 5],
[ 3, 6]])
# since we permute the axes/dims, the shape changed from (2, 3) => (3, 2)
In [42]: aten.permute(1, 0).shape
Out[42]: torch.Size([3, 2])
您还可以使用负索引来执行与以下相同的操作:
In [45]: aten.permute(-1, 0)
Out[45]:
tensor([[ 1, 4],
[ 2, 5],
[ 3, 6]])
In [46]: aten.permute(-1, 0).shape
Out[46]: torch.Size([3, 2])
视图改变了张量的表示方式。例如:具有 4 个元素的张量可以表示为 4X1 或 2X2 或 1X4,但排列会更改轴。排列时,数据会被移动,但视图数据不会被移动,而只是被重新解释。
下面的代码示例可能会对您有所帮助。
a
是 2x2 张量/矩阵。通过使用视图,您可以将 a
读取为列或行向量(张量)。但你不能转置它。要转置你需要排列。转置是通过交换/排列轴来实现的。
In [7]: import torch
In [8]: a = torch.tensor([[1,2],[3,4]])
In [9]: a
Out[9]:
tensor([[ 1, 2],
[ 3, 4]])
In [11]: a.permute(1,0)
Out[11]:
tensor([[ 1, 3],
[ 2, 4]])
In [12]: a.view(4,1)
Out[12]:
tensor([[ 1],
[ 2],
[ 3],
[ 4]])
In [13]:
奖金:请参阅https://twitter.com/karpathy/status/1013322763790999552
tensor.permute()
排列张量轴的顺序。
tensor.view()
通过减小/扩大每个维度的大小(如果一个维度增加,其他维度必须减少)来重塑张量(类似于 numpy.reshape
)。
链接给出了关于视图、重塑和排列的清晰解释:
view
适用于连续张量。reshape
适用于非连续张量。permute
返回原始张量输入的视图,其维度已排列。它与view
和reshape
有很大不同。这里是类似情况下两种方法的比较。这两个示例都给出了相同的形状 (3x2),它是从原始
boo
张量 (2x3) 修改而来的。请注意,数据的顺序不同。
tensor.view()
将原始数据重塑为兼容的形状。可以查看形状为 6x1、1x6、3x2 的 2x3 张量。这种情况保留了数据连续性(注意顺序没有改变)。
boo = torch.tensor([[1, 2, 3],
[4, 5, 6]])
boo_viewed = boo.view(3, 2)
# Original boo tensor (2x3)
tensor([[1, 2, 3],
[4, 5, 6]])
# View of boo (3x2)
tensor([[1, 2],
[3, 4],
[5, 6]])
tensor.permute()
另一方面,交换给定轴和数据连续性 将不会保留。数据的顺序已更改。矩阵转置是 permute
的一个示例。
boo = torch.tensor([[1, 2, 3],
[4, 5, 6]])
boo_permuted = boo.permute(1, 0)
# Original boo tensor (2x3)
tensor([[1, 2, 3],
[4, 5, 6]])
# Permuted boo (3x2)
tensor([[1, 4],
[2, 5],
[3, 6]])
这里的一个重要概念是关于“连续数据与非连续数据”。它是一种 Torch 内存格式,涉及实际内存分配和张量表示(请参阅这个答案)。