torch.Tensor.permute(*dims)方法、torch.transpose(input, dim0, dim1)函数、torch.t(input)函和 torch.Tensor.T属性

参考链接: permute(*dims)
参考链接: T
参考链接: torch.t(input)
参考链接: torch.transpose(input, dim0, dim1)

在这里插入图片描述对张量的维度按照所指定的要求进行重新排列,实验1:

(base) PS C:\Users\chenxuqi> python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x0000021DDF6B2A90>
>>> x = torch.randn(2, 3, 5)
>>> x.size()
torch.Size([2, 3, 5])
>>> x.shape
torch.Size([2, 3, 5])
>>> x.stride()
(15, 5, 1)
>>> x.permute(2, 0, 1).size()
torch.Size([5, 2, 3])
>>> x.shape
torch.Size([2, 3, 5])
>>>
>>> y = x.permute(2, 0, 1)
>>> y.shape
torch.Size([5, 2, 3])
>>> y.stride()
(1, 15, 5)
>>>
>>>
>>>  

在这里插入图片描述
将张量的所有维度逆转,相当于permute(n-1, n-2, ..., 0),实验2:

(base) PS C:\Users\chenxuqi> python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x000002809EBB2A90>
>>>
>>> x = torch.randn(2, 3, 5)
>>> x.shape
torch.Size([2, 3, 5])
>>>
>>> y = x.T
>>> y.shape
torch.Size([5, 3, 2])
>>>
>>>
>>>
>>> x = torch.randn(2, 3, 4, 5, 6, 7)
>>> x.shape
torch.Size([2, 3, 4, 5, 6, 7])
>>> x.stride()
(2520, 840, 210, 42, 7, 1)
>>>
>>> y = x.T
>>> y.shape
torch.Size([7, 6, 5, 4, 3, 2])
>>> y.stride()
(1, 7, 42, 210, 840, 2520)
>>>
>>>
>>>
>>>
>>> x.shape
torch.Size([2, 3, 4, 5, 6, 7])
>>> x.stride()
(2520, 840, 210, 42, 7, 1)
>>>
>>> z = permute(5,4,3,2,1,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'permute' is not defined
>>>
>>> z = x.permute(5,4,3,2,1,0)
>>> z.shape
torch.Size([7, 6, 5, 4, 3, 2])
>>> z.stride()
(1, 7, 42, 210, 840, 2520)
>>>
>>>
>>>

在这里插入图片描述
该函数接收一个输入张量,其维度只能是1、2或者0,并且,对于0维和1维的输入张量,返回该张量本身.对于2维张量,返回其转置,相当于transpose(input, 0, 1).

代码实验3:

(base) PS C:\Users\chenxuqi> python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x0000016680498090>
>>>
>>> x = torch.randn(())
>>> x
tensor(0.2824)
>>> x.shape
torch.Size([])
>>> x.stride()
()
>>>
>>> y = torch.t(x)
>>> y
tensor(0.2824)
>>> y.shape
torch.Size([])
>>> y.stride()
()
>>>
>>>
>>>
>>>
>>> x = torch.randn(3)
>>> x
tensor([-0.3715,  0.9088, -1.7601])
>>> x.shape
torch.Size([3])
>>> x.stride()
(1,)
>>> y = torch.t(x)
>>> y
tensor([-0.3715,  0.9088, -1.7601])
>>> y.shape
torch.Size([3])
>>> y.stride()
(1,)
>>>
>>>
>>> x = torch.randn(2, 3)
>>> x
tensor([[-0.1806,  2.0937,  1.0406],
        [-1.7651,  1.1216,  0.8440]])
>>> x.shape
torch.Size([2, 3])
>>> x.stride()
(3, 1)
>>>
>>> y = torch.t(x)
>>> y
tensor([[-0.1806, -1.7651],
        [ 2.0937,  1.1216],
        [ 1.0406,  0.8440]])
>>> y.shape
torch.Size([3, 2])
>>> y.stride()
(1, 3)
>>>
>>>    

在这里插入图片描述
功能: 对给定的张量,将其某两个指定的维度进行对调.
实验4:

(base) PS C:\Users\chenxuqi> python
Python 3.7.4 (default, Aug  9 2019, 18:34:13) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x0000025C47D03A90>
>>>
>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.2824, -0.3715,  0.9088],
        [-1.7601, -0.1806,  2.0937]])
>>> torch.transpose(x, 0, 1)
tensor([[ 0.2824, -1.7601],
        [-0.3715, -0.1806],
        [ 0.9088,  2.0937]])
>>> x
tensor([[ 0.2824, -0.3715,  0.9088],
        [-1.7601, -0.1806,  2.0937]])
>>> torch.transpose(x, 1, 0)
tensor([[ 0.2824, -1.7601],
        [-0.3715, -0.1806],
        [ 0.9088,  2.0937]])
>>>
>>>
>>>
>>> torch.manual_seed(seed=20200910)
<torch._C.Generator object at 0x0000025C47D03A90>
>>>
>>>
>>> x = torch.randn(2, 3, 4, 5, 6, 7, 8)
>>> x.shape
torch.Size([2, 3, 4, 5, 6, 7, 8])
>>> x.stride()
(20160, 6720, 1680, 336, 56, 8, 1)
>>>
>>> y = torch.transpose(x, 6, 5)
>>> y.shape
torch.Size([2, 3, 4, 5, 6, 8, 7])
>>> y.stride()
(20160, 6720, 1680, 336, 56, 1, 8)
>>>
>>> x.shape
torch.Size([2, 3, 4, 5, 6, 7, 8])
>>> x.stride()
(20160, 6720, 1680, 336, 56, 8, 1)
>>>
>>>
>>> z = torch.transpose(x, 1, 4)
>>> z.shape
torch.Size([2, 6, 4, 5, 3, 7, 8])
>>> z.stride()
(20160, 56, 1680, 336, 6720, 8, 1)
>>>
>>>
>>> torch.transpose(x, 1, 4).shape
torch.Size([2, 6, 4, 5, 3, 7, 8])
>>> torch.transpose(x, 4, 1).shape
torch.Size([2, 6, 4, 5, 3, 7, 8])
>>>
>>>
>>> torch.transpose(x, 0, 6).shape
torch.Size([8, 3, 4, 5, 6, 7, 2])
>>> torch.transpose(x, 6, 0).shape
torch.Size([8, 3, 4, 5, 6, 7, 2])
>>>
>>>
>>>

版权声明:本文为m0_46653437原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。