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]]))
>>>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值