【PyTorch】PyTorch之Reduction Ops

2024-01-20 09:20
文章标签 pytorch reduction ops

本文主要是介绍【PyTorch】PyTorch之Reduction Ops,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 前言
  • 一、ARGMAX
  • 二、ARGMIN
  • 三、AMAX和AMIN
  • 四、ALL和ANY
  • 五、MAX和MIN
  • 六、MEAN
  • 七、MEDIAN
  • 八、NORM
  • 九、PROD
  • 十、STD
  • 十一、SUM
  • 十二、UNIQUE
  • 十三、VAR


前言

介绍pytorch的Reduction Ops。

一、ARGMAX

torch.argmax(input, dim, keepdim=False) → LongTensor
Parameters:
input (Tensor) – the input tensor.
dim (int) – the dimension to reduce. If None, the argmax of the flattened input is returned.
keepdim (bool) – whether the output tensor has dim retained or not. Ignored if dim=None.

返回输入张量中所有元素的最大值的索引。
这是 torch.max() 返回的第二个值。
注意:
如果存在多个最大值,则返回第一个最大值的索引。

在这里插入图片描述
在这里插入图片描述

二、ARGMIN

torch.argmin(input, dim=None, keepdim=False) → LongTensor
Parameters:
input (Tensor) – the input tensor.
dim (int) – the dimension to reduce. If None, the argmin of the flattened input is returned.
keepdim (bool) – whether the output tensor has dim retained or not…

返回输入张量中所有元素的最小值的索引。
这是 torch.min() 返回的第二个值。
注意:
如果存在多个最小值,则返回第一个最小值的索引。
在这里插入图片描述

三、AMAX和AMIN

torch.amax(input, dim, keepdim=False, , out=None) → Tensor
torch.amin(input, dim, keepdim=False, , out=None) → Tensor
Parameters:
input (Tensor) – the input tensor.
dim (int or tuple of ints) – the dimension or dimensions to reduce.
keepdim (bool) – whether the output tensor has dim retained or not.
Keyword Arguments:
out (Tensor, optional) – the output tensor.

返回输入张量在给定维度(维度)dim中的每个切片的最大值/最小值。
注意:
max/min 与 amax/amin 之间的区别为:

  • amax/amin 支持在多个维度上进行减少,
  • amax/amin 不返回索引,
  • amax/amin 在相等的值之间均匀分配梯度,而 max(dim)/min(dim) 仅将梯度传播到源张量中的单个索引。

如果 keepdim 为 True,则输出张量在除了维度(维度)dim的位置大小与输入相同。否则,dim 被挤压(参见 torch.squeeze()),导致输出张量的维度减少1(或 len(dim))。

在这里插入图片描述
在这里插入图片描述

四、ALL和ANY

torch.all(input) → Tensor
torch.any(input) → Tensor

all:输入中是否所有的任何元素评估为 True。
any:测试输入中是否有任何元素评估为 True。
此函数匹配 NumPy 的行为,在除 uint8 外的所有支持的 dtype 上返回 bool 类型的输出。对于 uint8,输出的 dtype 本身是 uint8。
在这里插入图片描述
在这里插入图片描述

五、MAX和MIN

**torch.max(input, dim, keepdim=False, , out=None)
torch.min(input, dim, keepdim=False, , out=None)
Parameters:
input (Tensor) – the input tensor.
dim (int) – the dimension to reduce.
keepdim (bool) – whether the output tensor has dim retained or not. Default: False.
Keyword Arguments:
out (tuple, optional) – the result tuple of two output tensors (max, max_indices)

返回一个命名元组 (values, indices),其中 values 是输入张量在给定维度 dim 中每行的最大值。而 indices 是找到的每个最大值的索引位置(argmax)。
如果 keepdim 为 True,则输出张量在除了维度 dim 的位置大小与输入相同。否则,dim 被挤压(参见 torch.squeeze()),导致输出张量的维度比输入少 1。
注意:
如果在缩减的行中存在多个最大值,则返回第一个最大值的索引。
在这里插入图片描述
在这里插入图片描述
torch.min()用法同torch.max()。

六、MEAN

*torch.mean(input, dim, keepdim=False, , dtype=None, out=None) → Tensor
Parameters:
input (Tensor) – the input tensor.
dim (int or tuple of ints) – the dimension or dimensions to reduce.
keepdim (bool) – whether the output tensor has dim retained or not.
Keyword Arguments:
dtype (torch.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.
out (Tensor, optional) – the output tensor.

返回输入张量在给定维度 dim 中每行的均值。如果 dim 是维度的列表,则对所有维度进行缩减。
如果 keepdim 为 True,则输出张量在除了维度(维度)dim的位置大小与输入相同。否则,dim 被挤压(参见 torch.squeeze()),导致输出张量的维度减少1(或 len(dim))。

torch.nanmean() 计算非nan元素的平均值。

在这里插入图片描述

七、MEDIAN

*torch.median(input, dim=-1, keepdim=False, , out=None)
Parameters:
input (Tensor) – the input tensor.
dim (int) – the dimension to reduce.
keepdim (bool) – whether the output tensor has dim retained or not.
Keyword Arguments:
out ((Tensor, Tensor), optional) – The first tensor will be populated with the median values and the second tensor, which must have dtype long, with their indices in the dimension dim of input.

返回一个命名元组 (values, indices),其中 values 包含输入在维度 dim 中每行的中位数,而 indices 包含在维度 dim 中找到的中位数的索引。
默认情况下,dim 是输入张量的最后一个维度。
如果 keepdim 为 True,则输出张量在除了维度 dim 的位置大小与输入相同。否则,dim 被挤压(参见 torch.squeeze()),导致输出张量的维度比输入少 1。
注意:
对于在维度 dim 中元素数为偶数的输入张量,中位数不是唯一的。在这种情况下,返回两个中位数中较小的一个。要计算输入中两个中位数的平均值,请使用 torch.quantile() 并将 q 设为 0.5。
警告:
indices 不一定包含找到的每个中位数值的第一个出现,除非它是唯一的。确切的实现细节是特定于设备的。一般而言,不要期望在 CPU 和 GPU 上运行时获得相同的结果。出于同样的原因,不要期望梯度是确定性的。
在这里插入图片描述
torch.nanmedian() 返回输入数据的中值,忽略NAN值

八、NORM

九、PROD

*torch.prod(input, dim, keepdim=False, , dtype=None) → Tensor
Parameters:
input (Tensor) – the input tensor.
dim (int) – the dimension to reduce.
keepdim (bool) – whether the output tensor has dim retained or not.
Keyword Arguments:
dtype (torch.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

返回给定维度dim中输入张量的每一行的乘积。

在这里插入图片描述

十、STD

*torch.std(input, dim=None, , correction=1, keepdim=False, out=None) → Tensor
Parameters:
input (Tensor) – the input tensor.
dim (int or tuple of ints) – the dimension or dimensions to reduce.
Keyword Arguments:
correction (int)
difference between the sample size and sample degrees of freedom. Defaults to Bessel’s correction, correction=1.
keepdim (bool) – whether the output tensor has dim retained or not.
out (Tensor, optional) – the output tensor.

计算沿指定维度 dim 的标准差。dim 可以是单个维度、维度列表或 None(在所有维度上进行缩减)。
标准差(σ)的计算方式是对每个维度的元素进行以下步骤:

  • 计算该维度上的平均值(μ)。
  • 对每个元素,计算其与平均值的差值,然后取平方。
  • 对所有差值的平方求和。
  • 将总和除以元素的数量。
  • 取结果的平方根,得到标准差。

在这里插入图片描述

如果 keepdim 为 True,则输出张量在除了维度 dim 的位置大小与输入相同。否则,dim 被挤压(参见 torch.squeeze()),导致输出张量的维度比输入少 1(或 len(dim))。

在这里插入图片描述

十一、SUM

**torch.sum(input, dim, keepdim=False, , dtype=None) → Tensor
Parameters:
input (Tensor) – the input tensor.
dim (int or tuple of ints, optional) – the dimension or dimensions to reduce. If None, all dimensions are reduced.
keepdim (bool) – whether the output tensor has dim retained or not.
Keyword Arguments:
dtype (torch.dtype, optional) – the desired data type of returned tensor. If specified, the input tensor is casted to dtype before the operation is performed. This is useful for preventing data type overflows. Default: None.

返回给定维度dim中输入张量的每行之和。如果dim是一个维度列表,则对所有维度进行约简。

在这里插入图片描述
torch.nansum() 返回所有元素的和,将非数字(nan)视为零。

十二、UNIQUE

torch.unique(input, sorted=True, return_inverse=False, return_counts=False, dim=None) → Tuple[Tensor, Tensor, Tensor]
Parameters:
input (Tensor) – the input tensor
sorted (bool) – Whether to sort the unique elements in ascending order before returning as output.
return_inverse (bool) – Whether to also return the indices for where elements in the original input ended up in the returned unique list.
return_counts (bool) – Whether to also return the counts for each unique element.
dim (int, optional) – the dimension to operate upon. If None, the unique of the flattened input is returned. Otherwise, each of the tensors indexed by the given dimension is treated as one of the elements to apply the unique operation upon. See examples for more details. Default: None
Returns:
A tensor or a tuple of tensors containing
output (Tensor): the output list of unique scalar elements.
inverse_indices (Tensor): (optional) if return_inverse is True, there will be an additional returned tensor (same shape as input) representing the indices for where elements in the original input map to in the output; otherwise, this function will only return a single tensor.
counts (Tensor): (optional) if return_counts is True, there will be an additional returned tensor (same shape as output or output.size(dim), if dim was specified) representing the number of occurrences for each unique value or tensor.
Return type:
(Tensor, Tensor (optional), Tensor (optional))

返回输入张量的唯一元素。
注意:
此函数与 torch.unique_consecutive() 不同,因为此函数还会消除非连续的重复值。
当前在 CUDA 实现和 CPU 实现中,当指定 dim 时,torch.unique 无论 sort 参数如何,都会在开始时对张量进行排序。排序可能会很慢,因此如果您的输入张量已经排序,建议使用 torch.unique_consecutive(),它避免了排序操作。

在这里插入图片描述
在这里插入图片描述

十三、VAR

*torch.var(input, dim=None, , correction=1, keepdim=False, out=None) → Tensor
Parameters:
input (Tensor) – the input tensor.
dim (int or tuple of ints, optional) – the dimension or dimensions to reduce. If None, all dimensions are reduced.
Keyword Arguments:
correction (int)
difference between the sample size and sample degrees of freedom. Defaults to Bessel’s correction, correction=1.
keepdim (bool) – whether the output tensor has dim retained or not.
out (Tensor, optional) – the output tensor.

计算沿指定维度 dim 的方差。dim 可以是单个维度、维度列表或 None(在所有维度上进行缩减)。计算公式如下:
在这里插入图片描述
在这里插入图片描述

这篇关于【PyTorch】PyTorch之Reduction Ops的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/625525

相关文章

Nn criterions don’t compute the gradient w.r.t. targets error「pytorch」 (debug笔记)

Nn criterions don’t compute the gradient w.r.t. targets error「pytorch」 ##一、 缘由及解决方法 把这个pytorch-ddpg|github搬到jupyter notebook上运行时,出现错误Nn criterions don’t compute the gradient w.r.t. targets error。注:我用

【超级干货】2天速成PyTorch深度学习入门教程,缓解研究生焦虑

3、cnn基础 卷积神经网络 输入层 —输入图片矩阵 输入层一般是 RGB 图像或单通道的灰度图像,图片像素值在[0,255],可以用矩阵表示图片 卷积层 —特征提取 人通过特征进行图像识别,根据左图直的笔画判断X,右图曲的笔画判断圆 卷积操作 激活层 —加强特征 池化层 —压缩数据 全连接层 —进行分类 输出层 —输出分类概率 4、基于LeNet

pytorch torch.nn.functional.one_hot函数介绍

torch.nn.functional.one_hot 是 PyTorch 中用于生成独热编码(one-hot encoding)张量的函数。独热编码是一种常用的编码方式,特别适用于分类任务或对离散的类别标签进行处理。该函数将整数张量的每个元素转换为一个独热向量。 函数签名 torch.nn.functional.one_hot(tensor, num_classes=-1) 参数 t

pytorch计算网络参数量和Flops

from torchsummary import summarysummary(net, input_size=(3, 256, 256), batch_size=-1) 输出的参数是除以一百万(/1000000)M, from fvcore.nn import FlopCountAnalysisinputs = torch.randn(1, 3, 256, 256).cuda()fl

Python(TensorFlow和PyTorch)两种显微镜成像重建算法模型(显微镜学)

🎯要点 🎯受激发射损耗显微镜算法模型:🖊恢复嘈杂二维和三维图像 | 🖊模型架构:恢复上下文信息和超分辨率图像 | 🖊使用嘈杂和高信噪比的图像训练模型 | 🖊准备半合成训练集 | 🖊优化沙邦尼尔损失和边缘损失 | 🖊使用峰值信噪比、归一化均方误差和多尺度结构相似性指数量化结果 | 🎯训练荧光显微镜模型和对抗网络图形转换模型 🍪语言内容分比 🍇Python图像归一化

Pytorch环境搭建时的各种问题

1 问题 1.一直soving environment,跳不出去。网络解决方案有:配置清华源,更新conda等,没起作用。2.下载完后,有3个要done的东西,最后那个exe开头的(可能吧),总是报错。网络解决方案有:用管理员权限打开prompt等,没起作用。3.有时候配置完源,安装包的时候显示什么https之类的东西,去c盘的用户那个文件夹里找到".condarc"文件把里面的网址都改成htt

【PyTorch】使用容器(Containers)进行网络层管理(Module)

文章目录 前言一、Sequential二、ModuleList三、ModuleDict四、ParameterList & ParameterDict总结 前言 当深度学习模型逐渐变得复杂,在编写代码时便会遇到诸多麻烦,此时便需要Containers的帮助。Containers的作用是将一部分网络层模块化,从而更方便地管理和调用。本文介绍PyTorch库常用的nn.Sequen

【python pytorch】Pytorch实现逻辑回归

pytorch 逻辑回归学习demo: import torchimport torch.nn as nnimport torchvision.datasets as dsetsimport torchvision.transforms as transformsfrom torch.autograd import Variable# Hyper Parameters input_si

【python pytorch】Pytorch 基础知识

包含知识点: 张量数学操作数理统计比较操作 #-*-coding:utf-8-*-import numpy as npnp.set_printoptions(suppress=True)import torch# 构造一个4*5 的矩阵z=torch.Tensor(4,5)print(z)# 两个矩阵进行加法操作y=torch.rand(4,5)print(z+y)# 另一种表示

【python pytorch】windows 10 深度学习框架pytorch安装

Python3.5+pip安装cpu版本 pip install http://download.pytorch.org/whl/cpu/torch-0.4.0-cp35-cp35m-win_amd64.whlpip install torchvision Python3.6+pip安装cpu版本 pip install http://download.pytorch.org/whl/cp