深入浅出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]))
相关推荐
HavenlonLabs7 分钟前
Havenlon 对抗性完整(十七):安全不是“防住攻击”,而是控制失败方式
网络·人工智能·架构·安全威胁分析·安全架构·havenlon
leoZ2319 分钟前
Claude 全面解析:从基础原理到实战应用指南
人工智能·游戏
doiito(Do It Together)14 分钟前
media_agent 进化之路:把 Gliding Horse 的 Agent 超能力注入 ComfyUI,让图片生成自己“学会”优化
人工智能·架构·rust·knowledge graph
Code_Artist16 分钟前
Trae AI 创造力大赛创意作品:AI 数字克隆人——让你有无数个分身!
人工智能·llm·aigc
涛声依旧-底层原理研究所24 分钟前
Agent 长任务可靠性设计:实现暂停、恢复、续跑与崩溃重启的完整方案
人工智能·python·系统架构
AC赳赳老秦31 分钟前
防火墙规则批量配置实战:OpenClaw 自动生成模板、批量下发与合规性校验全解析
java·开发语言·人工智能·python·github·php·openclaw
8Qi843 分钟前
HelloAgents:RAG——让 Agent 学会检索知识
人工智能·llm·agent·ai编程·vibecoding
触底反弹1 小时前
🔥 从点积到 Transformer:我终于搞懂大模型是怎么"猜"出下一个词的了
人工智能·机器学习·架构
Token炼金师1 小时前
算力显存通信的三角博弈:DP/TP/PP/SP、ZeRO、混合精度与稳定性 —— 训练优化四件套
人工智能·深度学习·dp·sp·pp·zero·tp
无糖可可果1 小时前
MCP(Model Context Protocol)学习分享:从理论到实践
人工智能