PyTorch:.movedim() 对比 .moveaxis() 对比 .permute()
PyTorch: .movedim() vs. .moveaxis() vs. .permute()
我是 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))
是的,moveaxis
是movedim
的别名(类似于和swapdims
)。1
是的,可以使用 permute
实现此功能,但是在保持所有其他轴的相对位置的同时移动一个轴是 common enough use-case 以保证其自身的语法糖。
- 术语取自numpy:
Alias for torch.movedim()
.
This function is equivalent to NumPy’s moveaxis function.
我是 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))
是的,moveaxis
是movedim
的别名(类似于swapdims
)。1
是的,可以使用 permute
实现此功能,但是在保持所有其他轴的相对位置的同时移动一个轴是 common enough use-case 以保证其自身的语法糖。
- 术语取自numpy:
Alias for
torch.movedim()
.This function is equivalent to NumPy’s moveaxis function.