深入浅出Pytorch函数——torch.max

分类目录:《深入浅出Pytorch函数》总目录

相关文章:

· 深入浅出Pytorch函数------torch.max

· 深入浅出Pytorch函数------torch.maximum


torch.max有三种输入形式,根据其输入形式及参数的不同有下列三种返回形式:

  • torch.max(input):返回输入张量所有元素的最大值。
  • torch.max(input, dim, keepdim=False, *, out=None):返回输入张量给定维度上每行的最大值,并同时返回每个最大值的位置索引。如果keepdimTrue,则输出张量的大小与输入张量的大小相同,但尺寸为1的维度dim除外。否则,dim会被挤压(请参见torch.squeeze()),即输出张量比输入少1个维度。
  • torch.max(input, other, *, out=None):参考torch.maximum

语法

复制代码
torch.max(input) -> Tensor
torch.max(input, dim, keepdim=False, *, out=None) -> (values, indices) 
torch.max(input, other, *, out=None) -> Tensor

参数

  • input:[Tensor] 输入张量
  • dim:[int] 待求最大值维度的索引,即返回值中被收缩维度的索引
  • keepdim:[bool] 是否保持输出张量与输入张量的形状一致,默认为False

实例

复制代码
>>> a = torch.randn(1, 3)
>>> a
tensor([[ 0.6763,  0.7445, -2.2369]])
>>> torch.max(a)
tensor(0.7445)

>>> a = torch.randn(4, 5)
>>> a
tensor([[ 1.1299, -1.2838, -1.0533, -1.8278,  0.1653],
        [ 0.6461,  0.4583,  1.5229, -1.0642, -1.8352],
        [-0.9679,  1.1227, -0.2506, -0.4781, -0.2027],
        [ 0.2576,  0.7588, -0.1484, -0.0256,  0.7012]])

>>> torch.max(a, 0)
torch.return_types.max(
values=tensor([ 1.1299,  1.1227,  1.5229, -0.0256,  0.7012]),
indices=tensor([0, 2, 1, 3, 3]))

>>> torch.max(a, 1)
torch.return_types.max(
values=tensor([1.1299, 1.5229, 1.1227, 0.7588]),
indices=tensor([0, 2, 1, 1]))
相关推荐
小趴菜不能喝20 小时前
Spring AI 基础实践
数据库·人工智能·spring
zhangfeng113320 小时前
KTransformers / 简称 Kt 让超大模型(如 DeepSeek-V3)能够在消费级硬件(单卡 24GB 显存 + 大内存)跑
人工智能·语言模型·自然语言处理
AI智能观察20 小时前
构建品牌AI认知资产:企业GEO实战四步框架,提升品牌AI推荐率
人工智能·geo·智能营销·geo优化·geo工具·geo平台·流量运营、
sali-tec20 小时前
C# 基于OpenCv的视觉工作流-章24-SURF特征点
图像处理·人工智能·opencv·算法·计算机视觉
hillstream320 小时前
从这次xAI重组说开去--用类比的思维来理解
人工智能·算法·xai·elon.mask
ccLianLian20 小时前
计算机基础·cs336·推理和训练
人工智能·深度学习
鹅是开哥20 小时前
Spring AI Alibaba + DashScope 调用超时彻底解决(SocketTimeoutException / read timeout)
java·人工智能·spring
网易伏羲20 小时前
以数据驱动工程机械智能化,网易灵动入选杭州国家语料库首批高质量数据集榜单
人工智能·具身智能·网易伏羲·网易灵动
够快云库20 小时前
2026信创架构实战:制造业非结构化数据的深度治理之道
人工智能·架构·企业文件管理
lisw0520 小时前
机器人系统:化学研究的超空间引擎——从自动化到智能化的范式革命
大数据·人工智能·科技·机器学习·机器人