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 个维度?


基础教程推荐
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 筛选NumPy数组 2022-01-01