PyTorch:.movedim() 与.moveaxis() 与.permute()

akl*_*gam 4 python pytorch tensor

我对 PyTorch 完全陌生,我想知道在.moveaxis().movedim()方法方面是否缺少任何内容。对于相同的参数,输出完全相同。这两种方法都不能被替换吗.permute()


参考示例:

import torch

mytensor = torch.randn(3,6,3,1,7,21,4)

t_md = torch.movedim(mytensor, 2, 5)
t_ma = torch.moveaxis(mytensor, 2, 5)

print(t_md.shape, t_ma.shape)
print(torch.allclose(t_md, t_ma))

t_p = torch.permute(mytensor, (0, 1, 3, 4, 5, 2, 6))

print(t_p.shape)
print(torch.allclose(t_md, t_p))
Run Code Online (Sandbox Code Playgroud)

uke*_*emi 5

是的,是(类似于和)moveaxis的别名。1movedimswapaxesswapdims

\n

是的,可以使用 来实现此功能permute,但是移动一个轴同时保持所有其他轴的相对位置是一种足够常见的用例,足以保证其自己的语法糖。

\n
\n \n
    \n
  1. 该术语取自numpy:\n
    \n

    别名为torch.movedim().

    \n

    该函数相当于 NumPy\xe2\x80\x99s moveaxis 函数。

    \n
    \n
  2. \n
\n
\n