How I can swap 3 dimensions with each other in Pytorch?(如何在 Pytorch 中相互交换 3 个维度?)
问题描述
我有一个 a= torch.randn(28, 28, 8) 并且我想交换张量的维度并将第三个维度移到第一位,第一个移到第二个名次和第二名到第三名.我使用了 b = a.transpose(2, 0, 1) ,但我收到了这个错误:
I have a a= torch.randn(28, 28, 8) and I want to swap the dimensions of the tensor and move the third dimension to the first place, first one to the second place and the second one to the third place. I used b = a.transpose(2, 0, 1) , but I received this error:
TypeError: transpose() received an invalid combination of arguments - got (int, int, int), but expected one of:
* (name dim0, name dim1)
* (int dim0, int dim1)
我应该多次使用转置,每次只交换两个维度吗?有什么办法可以一次性全部换掉吗?
Should I use transpose several times, each time only to swap two dimensions? Is there any way that I can swap all at once?
谢谢.
推荐答案
可以使用Pytorch的permute()函数一次性全部交换,
You can use Pytorch's permute() function to swap all at once,
>>>a = torch.randn(28, 28, 8)
>>>b = a.permute(2, 0, 1)
>>>b.shape
torch.Size([8, 28, 28])
这篇关于如何在 Pytorch 中相互交换 3 个维度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Pytorch 中相互交换 3 个维度?
基础教程推荐
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 包装空间模型 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
