栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

每天讲解一点PyTorch 【2】transpose

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

每天讲解一点PyTorch 【2】transpose

今天我们学习transpose函数
transpose函数,它实现的功能是交换维度,也就是矩阵转置功能,不支持高维度

>>> m = torch.tensor([[1,2],[3,4]])
>>> m
tensor([[1, 2],
        [3, 4]])

>>> m.transpose(0,1) 
tensor([[1, 3],
        [2, 4]])
>>> 
>>> n = torch.tensor([[2,3],[4,5]])
>>> n
tensor([[2, 3],
        [4, 5]])
>>> n.transpose(0,1) 
tensor([[2, 4],
        [3, 5]])
>>> 

进一步分析:

```python
>>> a = torch.rand(1,2,3)
>>> a
tensor([
   [
     [0.5346, 0.1114, 0.8110],
     [0.3542, 0.7874, 0.8278]
   ]
])

>>> a.transpose(1,0) #不支持高维度,一次只能2个维度
tensor([
		[
		  [0.5346, 0.1114, 0.8110]
		],
		[
		  [0.3542, 0.7874, 0.8278]
		]
	])

>>> a.transpose(0,1)
tensor([
		[
			[0.5346, 0.1114, 0.8110]
		],
        [
        	[0.3542, 0.7874, 0.8278]
        ]
        ])
        
>>> a.transpose(0,2)
tensor([
 		[
		 [0.5346],[0.3542]
        ],
        [
         [0.1114],[0.7874]
        ],
        [
         [0.8110],[0.8278]
        ]
      ])

>>> a.transpose(2,0)
tensor([[[0.5346],
         [0.3542]],

        [[0.1114],
         [0.7874]],

        [[0.8110],
         [0.8278]]])


>>> a.permute(1,0,2)
tensor([
         [
           [0.5346, 0.1114, 0.8110]
         ],
         [
           [0.3542, 0.7874, 0.8278]
         ]
       ])

>>> a
tensor([[[0.5346, 0.1114, 0.8110],
         [0.3542, 0.7874, 0.8278]]])
>>> 
>>> a.shape
torch.Size([1, 2, 3])
>>> 
>>> a.transpose(-2,-1)
tensor([[[0.5346, 0.3542],
         [0.1114, 0.7874],
         [0.8110, 0.8278]]])
>>> 
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498716.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号