理解torch.argmax() ,我是错误的

torch.max()

python 复制代码
import torch

# 定义张量 b
b = torch.tensor([[1, 3, 5, 7],
                  [2, 4, 6, 8],
                  [11, 12, 13, 17]])

# 使用 torch.max() 找到最大值
max_indices = torch.max(b, dim=0)

print(max_indices)

输出:>>> print(max_indices)

torch.return_types.max(

values=tensor([11, 12, 13, 17]),

indices=tensor([2, 2, 2, 2]))

分析:张量b是3*4 二维张量,dim=0 得到3个张量分别是【1,2,5,7】,【2,4,6,8】,【11,12,13,17】,最大是谁呢?因此tourch.argmax() 得到indices=2对应第三个。注意啊,是4个2!

python 复制代码
import torch

# 定义张量 b
b = torch.tensor([[1, 3, 5, 7],
                  [2, 4, 6, 8],
                  [11, 12, 13, 17]])

# 使用 torch.max() 找到最大
max_indices = torch.max(b, dim=1)

print(max_indices)

输出:>>> print(max_indices)

torch.return_types.max(

values=tensor([ 7, 8, 17]),

indices=tensor([3, 3, 3]))

分析:张量b是3*4 二维张量,dim=1 得到4个张量分别是【1,2,11】,【3,4,12】,【15,6,13】,【7,8,17】最大是谁呢?因此indices=3对应第4个,因此tourch.argmax() 得到indices=3对应第4个。注意啊,是3个3!

结论:torch.argmax() ,我开始的理解是错误的,通过torch.max() 分析,重新理解argmax() 返回所有元素中的最大值索引!问题来了,索引可能多个,例如indices=tensor([3, 3, 3])),我的疑惑就是索引都是3,能否得到索引indices=tensor([2, 3, 3]))这样的例子呢?如果构造张量b 确保3个索引值是不同的呢?

相关推荐
FreakStudio40 分钟前
全网最适合入门的面向对象编程教程:48 Python函数方法与接口-位置参数、默认参数、可变参数和关键字参数
python·嵌入式·面向对象·电子diy
天下无敌笨笨熊2 小时前
PyQT开发总结
python·pyqt
机器学习Zero2 小时前
让效率飞升的秘密武器:解锁编程高效时代的钥匙
git·python·github·aigc
wjcroom2 小时前
celery-APP在windows平台的发布方法(绿色免安装exe可搭配eventlet)
windows·python·celery
AI让世界更懂你3 小时前
漫谈设计模式 [5]:建造者模式
python·设计模式·建造者模式
FutureUniant3 小时前
GitHub每日最火火火项目(9.13)
人工智能·python·计算机视觉·github·音视频
ai_xiaogui3 小时前
【AIStarter:AI绘画、设计、对话】零基础入门:Llama 3.1 + 千问2快速部署
人工智能·深度学习
liuzhenghua663 小时前
python运维
运维·开发语言·python
学java的小菜鸟啊3 小时前
Java队列详细解释
java·开发语言·经验分享·python
逼子格4 小时前
基于深度学习的线性预测:创新应用与挑战
深度学习·神经网络·线性预测·自适应线性预测