当前位置:网站首页>Torch in pytoch Max() function analysis
Torch in pytoch Max() function analysis
2022-07-18 08:10:00 【cv_ lhp】
One . torch.max() Function analysis
1. The website links
torch.max, As shown in the figure below :

2. torch.max(input) Function analysis
torch.max(input) → Tensor
Enter input tensor , No matter how many dimensions , First put it reshape Arrange into a one-dimensional vector , Then find the maximum value in this one-dimensional vector
3. The code for
3.1 Input one-dimensional tensor , Return the maximum value in one-dimensional tensor
x = torch.randn(4)
y = torch.max(x)
x,y
The output is as follows :
(tensor([-0.6223, 0.0043, -0.8753, 1.4240]), tensor(1.4240))
3.2 Enter the two-dimensional tensor , Return the maximum value in the two-dimensional tensor
x = torch.randn(3,4)
y = torch.max(x)
x,y
The output is as follows :
(tensor([[-1.1052, 0.1026, 0.9994, -0.3092],
[-0.8400, 0.2004, 0.9212, 0.7807],
[-1.2979, -0.4327, 2.3044, 0.0140]]),
tensor(2.3044))
3.3 Enter two one-dimensional tensors , Output the maximum value of the corresponding element in these two tensors
x = torch.randn(4)
z = torch.randn(4)
max = torch.max(x,z)
x,z,max
The output is as follows :
(tensor([-1.5147, -1.2790, -1.0159, -0.4732]),
tensor([-0.4547, -2.8545, 0.0554, -0.3548]),
tensor([-0.4547, -1.2790, 0.0554, -0.3548]))
3.4 Enter two tensors , A tensor one dimension , A tensor two-dimensional , At this time, one-dimensional tensor will be broadcast into two-dimensional tensor , Then output the maximum value of the corresponding element in these two tensors , The output tensor is two-dimensional .
x = torch.randn(3,4)
z = torch.randn(4)
max = torch.max(x,z)
x,z,max
The output is as follows :
(tensor([[ 1.1917, 0.6338, 0.7590, -0.9802],
[ 0.2247, 0.3635, 1.3743, 1.6229],
[ 1.6165, 0.0634, 0.5259, 0.1285]]),
tensor([3.4765, 0.4480, 0.1502, 0.3738]),
tensor([[3.4765, 0.6338, 0.7590, 0.3738],
[3.4765, 0.4480, 1.3743, 1.6229],
[3.4765, 0.4480, 0.5259, 0.3738]]))
3.5 Enter two two-dimensional tensors , Output the maximum value of the corresponding element in these two tensors , The output tensor is two-dimensional .
x = torch.randn(3,4)
z = torch.randn(3,4)
max = torch.max(x,z)
x,z,max
The output is as follows :
(tensor([[-0.0835, 0.0718, -1.7404, -0.3218],
[ 0.0577, 0.6271, 1.4014, -0.6417],
[ 0.3917, 0.0761, 1.2479, -0.4352]]),
tensor([[-0.0717, 0.3822, 0.7256, 1.4147],
[-0.1271, 0.1503, 0.3934, 1.6760],
[-2.2341, 2.5286, -0.3500, -0.1751]]),
tensor([[-0.0717, 0.3822, 0.7256, 1.4147],
[ 0.0577, 0.6271, 1.4014, 1.6760],
[ 0.3917, 2.5286, 1.2479, -0.1751]]))
4. torch.max(input,dim) Function analysis
torch.max(input, dim, keepdim=False, *, out=None)
Input input( A two-dimensional ) tensor , When dim=0 When, it means to find the maximum value of each column , The function returns two tensor, first tensor Is the maximum per column , the second tensor Is the index of the maximum value of each column ; When dim=1 When, it means to find the maximum value of each line , The function returns two tensor, first tensor Is the maximum per line ; the second tensor Is the index of the maximum value per row .
5. The code for
5.1 dim=0, Find the maximum value of each column , The function returns two tensor, first tensor Is the maximum per column , the second tensor Is the index of the maximum value of each column , Two tensor It's all one-dimensional .
x = torch.randn(3,4)
max,indices = torch.max(x,dim=0)
x,max,indices
(tensor([[ 0.1806, 1.0274, 0.5138, -1.4184],
[ 0.5892, -0.7117, -1.2707, 0.7682],
[ 0.5152, -0.8803, 1.7604, 0.4852]]),
torch.return_types.max(
values=tensor([0.5892, 1.0274, 1.7604, 0.7682]),
indices=tensor([1, 0, 2, 1])))
The output is as follows :
(tensor([[ 0.0190, 0.8180, -1.0463, 1.7940],
[ 0.7537, -1.0291, -2.3431, 0.3906],
[ 0.3715, 1.6940, -1.1200, -0.4580]]),
tensor([ 0.7537, 1.6940, -1.0463, 1.7940]),
tensor([1, 2, 0, 0]))
5.2 dim=1, Find the maximum value for each row , The function returns two tensor, first tensor Is the maximum per line ; the second tensor Is the index of the maximum value per row , Two tensor It's all one-dimensional .
x = torch.randn(3,4)
max,indices = torch.max(x,dim=1)
x,max,indices
The output is as follows :
(tensor([[ 1.4832, 0.1886, -0.3044, -0.6111],
[-0.8998, 0.0610, 0.3388, 1.7176],
[ 1.6153, 0.6864, 2.3225, 1.3818]]),
tensor([1.4832, 1.7176, 2.3225]),
tensor([0, 3, 2]))
边栏推荐
- 李沐动手学深度学习V2-目标检测SSD
- SAR Image: common distribution when fitting clutter
- Unity IoTAR物联网增强现实教程
- Difference between shape parameter and scale parameter in probability density function
- 秋招拿了6offer,总结了20多类1100道面试题含答案解析
- 竞赛·6116·计算布尔二叉树的值·递归
- 338. Bit counting · dynamic programming
- Openpyxl drawing pie chart
- 驳'一切不谈考核的管理都是扯淡'
- LDAP introduction
猜你喜欢

This year, how many war investment departments have become "decorations"

One question per day · 873 The length of the longest Fibonacci subsequence Mnemonic search

(zero six) flask is OK if you have hands - configure static files
![[untitled] slow SQL analysis and optimization](/img/0c/24200bc7052e87ae4d93b309b12c5d.png)
[untitled] slow SQL analysis and optimization

Calculate the distance between two points according to longitude and latitude

337. Looting · dynamic planning

openGauss 联合产业界创新,共建开源数据库根社区

Supplementary cases and knowledge points in the first six chapters (I)

数据传输:同构异IP数据源批量抽取实践

软件架构与设计(十)-----架构技术
随机推荐
GeoServer complete tutorial
337. Looting · dynamic planning
990.等式方程的可满足性·并查集
Several things I want to understand about being laid off at the age of 30
概率密度函数中形状参数和尺度参数的区别
338. Bit counting · dynamic programming
SAR图像:拟合杂波时常用的分布
Data transmission: Practice of batch extraction of isomorphic and heterogeneous IP data sources
Matlab:图像增强 imageDataAugmenter() 的用法
Matlab: usage of split dataset spliteachlabel()
软件架构与设计(四)-----数据流架构
[detailed tutorial] a thorough article on mongodb aggregation query
What security risks need to be considered in cloud platforms and infrastructure
2. Trend technology 2017 school recruitment development post test questions
Pytorch中torch.unsqueeze()和torch.squeeze()函数解析
One question per day · 1252 Number of odd cells · simulation optimization
Matlab:trainingoptions() explain training options in detail
(high frequency interview questions) computer network
进阶测试人必看:自动化测试入门基础知识
Matlab: usage of image enhancement imagedataaugmenter()