pytorch 中 dim 的-1,0,1,2 的意义 详解

对于3维矩阵,dim为-1时  与 dim为2时 的效果是一样的。dim为0时  从0维度,

下图  是三维实例

 图的目的是  可以由一个想象的空间。

下面代码 与上图关系不大

>>> ab = torch.tensor([[[0,1,2,3],[1,2,3,4]],[[2,3,4,5],[4,5,6,7]],[[5,6,7,8],[6,7,8,9]]])
>>> ab
tensor([[[0, 1, 2, 3],
         [1, 2, 3, 4]],

        [[2, 3, 4, 5],
         [4, 5, 6, 7]],

        [[5, 6, 7, 8],
         [6, 7, 8, 9]]])
>>> ab.shape #ab的形状尺寸
torch.Size([3, 2, 4])
>>> ab.size() # ab的形状尺寸
torch.Size([3, 2, 4])
>>> print('no===')  # 没有参数时,表示全局最大值
no===
>>> print(ab.max()) # 输出全局最大值 9 
tensor(9)
>>> print('==-1===') # 显示dim 为 -1时
==-1===
>>> print(ab.max(-1)) # 输出dim为-1时 请看输出内容
torch.return_types.max(
values=tensor([[3, 4],
        [5, 7],
        [8, 9]]),
indices=tensor([[3, 3],
        [3, 3],
        [3, 3]]))
>>> print('== 0 ===')  # 显示dim为0时 
== 0 ===
>>> print(ab.max(0))  # 输出dim为0时 请看输出内容
torch.return_types.max(
values=tensor([[5, 6, 7, 8],
        [6, 7, 8, 9]]),
indices=tensor([[2, 2, 2, 2],
        [2, 2, 2, 2]]))
>>> print('== 1 ===')  # 显示dim为1时 
== 1 ===
>>> print(ab.max(1))   # 输出dim为1时 请看输出内容
torch.return_types.max(
values=tensor([[1, 2, 3, 4],
        [4, 5, 6, 7],
        [6, 7, 8, 9]]),
indices=tensor([[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]]))
>>>
>>> print('== 2 ===')  # 显示dim为2时  
== 2 ===
>>> print(ab.max(2))   # 输出dim为2时 请看输出内容   这个输出结果与 dim为-1 一样
torch.return_types.max(
values=tensor([[3, 4],
        [5, 7],
        [8, 9]]),
indices=tensor([[3, 3],
        [3, 3],
        [3, 3]]))
>>>

猜你喜欢

转载自blog.csdn.net/Vertira/article/details/131465898