理解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个索引值是不同的呢?

相关推荐
淼澄研学14 小时前
PyTorch深度学习实战:5个核心方法从0到1构建神经网络
前端·数据库·python
SunnyDays101114 小时前
Python 将 Excel(XLS/XLSX)转换为 JSON:导出工作簿、工作表、单元格区域与自定义 JSON 结构
python·json·excel·excel 转 json·导出 excel 到 json·xlsx 转 json·xls 转 json
天l志14 小时前
Chrome Extension + 本地服务:浏览器页面上下文采集与远程执行技术设计
python·谷歌浏览器
CodexDave14 小时前
Python 自动化接单实战(九):Windows 免环境交付如何打包与诊断
windows·python·自动化·python自动化·pyinstaller·软件交付·windows打包
CTA量化套保14 小时前
2026年量化入门路线,概念规则和简单实现逐步走
人工智能·python
米码收割机14 小时前
【Python】Python Django+Vue3校园自习室预约管理系统(源码+文档+PPT)【独一无二】
开发语言·python·django
AstartesEternal14 小时前
python第二次作业(列表,字典)
开发语言·python
拉特莉的祈祷机14 小时前
绿色测试日志为什么可能已经失效:用 AET 绑定命令与当前代码
python
m沐沐14 小时前
【自然语言处理】NLP分词与Word2Vec词向量——从原理到实战
人工智能·深度学习·opencv·机器学习·计算机视觉·自然语言处理·word2vec
三金1213814 小时前
Python的一些内置模块
python