评价此页

torch.movedim#

torch.movedim(input, source, destination) Tensor#

input 中位于 source 位置的维度移动到 destination 位置。

input 中未明确移动的其他维度将保持其原始顺序,并出现在 destination 中未指定的位置。

参数
  • input (Tensor) – 输入张量。

  • source (intint 元组) – 要移动的维度的原始位置。这些位置必须是唯一的。

  • destination (intint 元组) – 每个原始维度的目标位置。这些位置也必须是唯一的。

示例

>>> t = torch.randn(3,2,1)
>>> t
tensor([[[-0.3362],
        [-0.8437]],

        [[-0.9627],
        [ 0.1727]],

        [[ 0.5173],
        [-0.1398]]])
>>> torch.movedim(t, 1, 0).shape
torch.Size([2, 3, 1])
>>> torch.movedim(t, 1, 0)
tensor([[[-0.3362],
        [-0.9627],
        [ 0.5173]],

        [[-0.8437],
        [ 0.1727],
        [-0.1398]]])
>>> torch.movedim(t, (1, 2), (0, 1)).shape
torch.Size([2, 1, 3])
>>> torch.movedim(t, (1, 2), (0, 1))
tensor([[[-0.3362, -0.9627,  0.5173]],

        [[-0.8437,  0.1727, -0.1398]]])