欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > 理解torch.argmax() ,我是错误的

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

2024/10/25 5:16:34 来源:https://blog.csdn.net/wu_huashan/article/details/141551233  浏览:    关键词:理解torch.argmax() ,我是错误的

torch.max()

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!

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com