gpt4 book ai didi

python - 我如何在 Pytorch 中交换 3 个维度?

转载 作者:行者123 更新时间:2023-12-05 02:08:12 24 4
gpt4 key购买 nike

我有一个 a= torch.randn(28, 28, 8) 我想交换张量的维度并将第三个维度移动到第一个位置,第一个到第二个名次和第二名到第三名。我使用了 b = a.transpose(2, 0, 1) ,但我收到了这个错误:

TypeError: transpose() received an invalid combination of arguments - got (int, int, int), but expected one of:
* (name dim0, name dim1)
* (int dim0, int dim1)

我应该多次使用转置,每次只交换两个维度吗?有什么办法可以一次全部交换吗?

谢谢。

最佳答案

你可以使用 Pytorch 的 permute() 函数一次性全部交换,

>>>a = torch.randn(28, 28, 8)
>>>b = a.permute(2, 0, 1)
>>>b.shape
torch.Size([8, 28, 28])

关于python - 我如何在 Pytorch 中交换 3 个维度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61104138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com