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 中将货币解析为数字 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
