iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >【Pytorch】torch.max() 函数详解
  • 758
分享到

【Pytorch】torch.max() 函数详解

pytorchpython深度学习 2023-09-30 17:09:12 758人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

文章目录 一、一个参数时的 torch.max()1. 函数介绍2. 实例 二、增加指定维度时的 torch.max()1. 函数介绍2. 实例 三、两个输入张量时的 torch.ma

文章目录


一、一个参数时的 torch.max()

1. 函数介绍

torch.max(input)

参数:

  • input (Tensor) – 输入张量
  • 返回输入张量所有元素中的最大值。

2. 实例

import torch# 返回张量中的最大值a = torch.randn(2, 3)print(a)print(torch.max(a))

输出结果:

tensor([[ 0.0031, -0.5391, -0.9214],        [-0.4647, -1.9750,  0.6924]])tensor(0.6924)

二、增加指定维度时的 torch.max()

1. 函数介绍

torch.max(input, dim, max=None, max_indices=None) -> (Tensor, LongTensor)

返回张量 input 在压缩指定维度 dim 时的最大值及其下标。

2. 实例

import torch# 返回张量在压缩指定维度时的最大值及其下标b = torch.randn(4, 4)print(b)print(torch.max(b, 0))  # 指定0维,压缩0维,0维消失,也就是行消失,返回列最大值及其下标print(torch.max(b, 1))  # 指定1维,压缩1维,1维消失,也就是列消失,返回行最大值及其下标

输出结果:

tensor([[-0.8862,  0.3502,  0.0223,  0.6035],        [-2.0135, -0.1346,  2.0575,  1.4203],        [ 1.0107,  0.9302, -0.1321,  0.0704],        [-1.4540, -0.4780,  0.7016,  0.3029]])torch.return_types.max(values=tensor([1.0107, 0.9302, 2.0575, 1.4203]),indices=tensor([2, 2, 1, 1]))torch.return_types.max(values=tensor([0.6035, 2.0575, 1.0107, 0.7016]),indices=tensor([3, 2, 0, 2]))

三、两个输入张量时的 torch.max()

1. 函数介绍

torch.max(input, other_input, out=None) → Tensor

返回两张量 input 和 other_input 在对应位置上的最大值形成的新张量。

2. 实例

import torch# 返回两张量对应位置上的最大值c = torch.randn(4,2)d = torch.randn(4,2)print(c)print(d)print(torch.max(c, d))

输出结果:

tensor([[ 0.6778,  1.2714],        [-0.9020, -1.3789],        [ 0.8541,  1.2193],        [-0.8481, -0.8211]])tensor([[ 2.4616, -1.2502],        [ 0.0173, -0.5501],        [ 1.0224, -1.5892],        [ 1.3325,  0.2587]])tensor([[ 2.4616,  1.2714],        [ 0.0173, -0.5501],        [ 1.0224,  1.2193],        [ 1.3325,  0.2587]])

参考链接

  1. 详解 torch.max 函数

来源地址:https://blog.csdn.net/weixin_44211968/article/details/128224243

--结束END--

本文标题: 【Pytorch】torch.max() 函数详解

本文链接: https://www.lsjlt.com/news/421962.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作