mindspore.nn

神经网络Cell。

用于构建神经网络中的预定义构建块或计算单元。

MindSpore中 mindspore.nn 接口与上一版本相比,新增、删除和支持平台的变化信息请参考 API Updates

基本构成单元

接口名

概述

支持平台

mindspore.nn.Cell

MindSpore中神经网络的基本构成单元。

Ascend GPU CPU

mindspore.nn.GraphCell

运行从MindIR加载的计算图。

Ascend GPU CPU

mindspore.nn.LossBase

损失函数的基类。

Ascend GPU CPU

mindspore.nn.Optimizer

用于参数更新的优化器基类。

Ascend GPU CPU

容器

接口名

概述

支持平台

mindspore.nn.CellList

构造Cell列表。

Ascend GPU CPU

mindspore.nn.SequentialCell

构造Cell顺序容器。

Ascend GPU CPU

封装层

接口名

概述

支持平台

mindspore.nn.DistributedGradReducer

分布式优化器。

Ascend GPU

mindspore.nn.DynamicLossScaleUpdateCell

用于动态更新损失缩放系数(loss scale)的神经元。

Ascend GPU

mindspore.nn.FixedLossScaleUpdateCell

固定损失缩放系数的神经元。

Ascend GPU

mindspore.nn.ForwardValueAndGrad

训练网络的封装。

Ascend GPU CPU

mindspore.nn.GetNextSingleOp

用于获取下一条数据的Cell。

Ascend GPU

mindspore.nn.MicroBatchInterleaved

这个函数的作用是将输入在第零维度拆成 interleave_num 份,然后执行包裹的cell的计算。

Ascend GPU

mindspore.nn.ParameterUpdate

更新参数的Cell。

Ascend GPU CPU

mindspore.nn.PipelineCell

将MiniBatch切分成更细粒度的MicroBatch,用于流水线并行的训练中。

Ascend GPU

mindspore.nn.TimeDistributed

时间序列封装层。

Ascend GPU CPU

mindspore.nn.TrainOneStepCell

训练网络封装类。

Ascend GPU CPU

mindspore.nn.TrainOneStepWithLossScaleCell

使用混合精度功能的训练网络。

Ascend GPU

mindspore.nn.WithEvalCell

封装前向网络和损失函数。

Ascend GPU CPU

mindspore.nn.WithGradCell

Cell that returns the gradients.

Ascend GPU CPU

mindspore.nn.WithLossCell

包含损失函数的Cell。

Ascend GPU CPU

卷积神经网络层

接口名

概述

支持平台

mindspore.nn.Conv1d

对输入Tensor计算一维卷积。

Ascend GPU CPU

mindspore.nn.Conv1dTranspose

计算一维转置卷积,可以视为Conv1d对输入求梯度,也称为反卷积(实际不是真正的反卷积)。

Ascend GPU CPU

mindspore.nn.Conv2d

对输入Tensor计算二维卷积。

Ascend GPU CPU

mindspore.nn.Conv2dTranspose

计算二维转置卷积,可以视为Conv2d对输入求梯度,也称为反卷积(实际不是真正的反卷积)。

Ascend GPU CPU

mindspore.nn.Conv3d

对输入Tensor计算三维卷积。

Ascend GPU CPU

mindspore.nn.Conv3dTranspose

计算三维转置卷积,可以视为Conv3d对输入求梯度,也称为反卷积(实际不是真正的反卷积)。

Ascend GPU CPU

mindspore.nn.Unfold

从图像中提取滑窗的区域块。

Ascend GPU

循环神经网络层

接口名

概述

支持平台

mindspore.nn.RNN

循环神经网络(RNN)层,其使用的激活函数为tanh或relu。

Ascend GPU CPU

mindspore.nn.RNNCell

循环神经网络单元,激活函数是tanh或relu。

Ascend GPU CPU

mindspore.nn.GRU

GRU(Gate Recurrent Unit)称为门控循环单元网络,是循环神经网络(Recurrent Neural Network, RNN)的一种。

Ascend GPU CPU

mindspore.nn.GRUCell

GRU(Gate Recurrent Unit)称为门控循环单元。

Ascend GPU CPU

mindspore.nn.LSTM

长短期记忆(LSTM)网络,根据输入序列和给定的初始状态计算输出序列和最终状态。

Ascend GPU CPU

mindspore.nn.LSTMCell

长短期记忆网络单元(LSTMCell)。

Ascend GPU CPU

嵌入层

接口名

概述

支持平台

mindspore.nn.Embedding

嵌入层。

Ascend GPU CPU

mindspore.nn.EmbeddingLookup

嵌入查找层。

Ascend GPU CPU

mindspore.nn.MultiFieldEmbeddingLookup

根据指定的索引和字段ID,返回输入Tensor的切片。

Ascend GPU

非线性激活函数层

接口名

概述

支持平台

mindspore.nn.CELU

CELU激活层(CELU Activation Operator)。

Ascend GPU CPU

mindspore.nn.ELU

指数线性单元激活函数(Exponential Linear Unit activation function)。

Ascend GPU CPU

mindspore.nn.FastGelu

快速高斯误差线性单元激活函数(Fast Gaussian Error Linear Units activation function)。

Ascend GPU CPU

mindspore.nn.GELU

高斯误差线性单元激活函数(Gaussian error linear unit activation function)。

Ascend GPU CPU

mindspore.nn.GLU

门线性单元函数(Gated Linear Unit function)。

Ascend GPU CPU

mindspore.nn.get_activation

获取激活函数。

Ascend GPU CPU

mindspore.nn.Hardtanh

按元素计算Hardtanh函数。

Ascend GPU CPU

mindspore.nn.HShrink

Hard Shrink激活函数。

Ascend CPU GPU

mindspore.nn.HSigmoid

Hard Sigmoid激活函数。

Ascend GPU CPU

mindspore.nn.HSwish

对输入的每个元素计算Hard Swish。

Ascend GPU CPU

mindspore.nn.LeakyReLU

Leaky ReLU激活函数。

Ascend GPU CPU

mindspore.nn.LogSigmoid

按元素计算Log Sigmoid激活函数。

Ascend GPU

mindspore.nn.LogSoftmax

按元素计算Log Softmax激活函数。

Ascend GPU CPU

mindspore.nn.LRN

局部响应归一化操作LRN(Local Response Normalization)。

Ascend GPU CPU

mindspore.nn.Mish

逐元素计算输入Tensor的MISH(Self Regularized Non-Monotonic Neural Activation Function 自正则化非单调神经激活函数)。

Ascend GPU CPU

mindspore.nn.Softsign

Softsign激活函数。

Ascend GPU CPU

mindspore.nn.PReLU

PReLU激活层(PReLU Activation Operator)。

Ascend GPU CPU

mindspore.nn.ReLU

修正线性单元激活函数(Rectified Linear Unit activation function)。

Ascend GPU CPU

mindspore.nn.ReLU6

ReLU6激活函数。

Ascend GPU CPU

mindspore.nn.RReLU

Randomized Leaky ReLU激活函数。

Ascend GPU CPU

mindspore.nn.SeLU

激活函数selu(Scaled exponential Linear Unit)。

Ascend GPU CPU

mindspore.nn.SiLU

SiLU激活函数。

Ascend GPU CPU

mindspore.nn.Sigmoid

Sigmoid激活函数。

Ascend GPU CPU

mindspore.nn.Softmin

Softmin函数,它是二分类函数 mindspore.nn.Sigmoid 在多分类上的推广,目的是将多分类的结果以概率的形式展现出来。

Ascend GPU CPU

mindspore.nn.Softmax

Softmax激活函数,它是二分类函数 mindspore.nn.Sigmoid 在多分类上的推广,目的是将多分类的结果以概率的形式展现出来。

Ascend GPU CPU

mindspore.nn.Softmax2d

将 SoftMax 应用于每个空间位置的特征。

Ascend GPU CPU

mindspore.nn.SoftShrink

SoftShrink激活函数。

Ascend CPU GPU

mindspore.nn.Tanh

逐元素计算Tanh函数,返回一个新的Tensor,该Tensor是输入元素的双曲正切值。

Ascend GPU CPU

mindspore.nn.Tanhshrink

Tanhshrink激活函数。

Ascend GPU CPU

mindspore.nn.Threshold

Threshold激活函数,按元素计算输出。

Ascend CPU GPU

线性层

接口名

概述

支持平台

mindspore.nn.Dense

全连接层。

Ascend GPU CPU

mindspore.nn.BiDense

双线性全连接层。

Ascend GPU CPU

Dropout层

接口名

概述

支持平台

mindspore.nn.Dropout

随机丢弃层。

Ascend GPU CPU

mindspore.nn.Dropout1d

在训练期间,以服从伯努利分布的概率 p 随机将输入Tensor的某些通道归零。

Ascend GPU CPU

mindspore.nn.Dropout2d

在训练期间,以服从伯努利分布的概率 p 随机将输入Tensor的某些通道归零(对于格式为 NCHW 的四维Tensor,其通道特征图指的是后两维 HW 格式的二维特征图)。

Ascend GPU CPU

mindspore.nn.Dropout3d

在训练期间,以服从伯努利分布的概率 p 随机将输入Tensor的某些通道归零(对于形状为 NCDHW5D Tensor,其通道特征图指的是后三维 DHW 形状的三维特征图)。

Ascend GPU CPU

归一化层

接口名

概述

支持平台

mindspore.nn.BatchNorm1d

在二维输入(mini-batch 一维输入)上应用批归一化(Batch Normalization Layer),避免内部协变量偏移。

Ascend GPU CPU

mindspore.nn.BatchNorm2d

在四维输入(具有额外通道维度的小批量二维输入)上应用批归一化处理(Batch Normalization Layer),以避免内部协变量偏移。

Ascend GPU CPU

mindspore.nn.BatchNorm3d

对输入的五维数据进行批归一化(Batch Normalization Layer)。

Ascend GPU CPU

mindspore.nn.GroupNorm

在mini-batch输入上进行组归一化。

Ascend GPU CPU

mindspore.nn.InstanceNorm1d

该层在三维输入(带有额外通道维度的mini-batch一维输入)上应用实例归一化。

GPU

mindspore.nn.InstanceNorm2d

该层在四维输入(带有额外通道维度的mini-batch二维输入)上应用实例归一化。

GPU

mindspore.nn.InstanceNorm3d

该层在五维输入(带有额外通道维度的mini-batch三维输入)上应用实例归一化。

GPU

mindspore.nn.LayerNorm

在mini-batch输入上应用层归一化(Layer Normalization)。

Ascend GPU CPU

mindspore.nn.SyncBatchNorm

在N维输入上进行跨设备同步批归一化(Batch Normalization,BN)。

Ascend

池化层

接口名

概述

支持平台

mindspore.nn.AdaptiveAvgPool1d

在一个输入Tensor上应用1D自适应平均池化运算,可视为组成一个1D输入平面。

Ascend GPU CPU

mindspore.nn.AdaptiveAvgPool2d

对输入Tensor,提供二维的自适应平均池化操作。

GPU

mindspore.nn.AdaptiveAvgPool3d

对输入Tensor,提供三维的自适应平均池化操作。

GPU

mindspore.nn.AdaptiveMaxPool1d

在一个输入Tensor上应用1D自适应最大池化运算,可被视为组成一个1D输入平面。

Ascend GPU CPU

mindspore.nn.AdaptiveMaxPool2d

对输入Tensor,提供二维自适应最大池化操作。

Ascend GPU CPU

mindspore.nn.AdaptiveMaxPool3d

对输入Tensor,提供三维自适应最大池化操作。

GPU CPU

mindspore.nn.AvgPool1d

在一个输入Tensor上应用1D平均池化运算,可被视为组成一个1D输入平面。

Ascend GPU CPU

mindspore.nn.AvgPool2d

在输入Tensor上应用2D平均池化运算,可视为二维输入平面的组合。

Ascend GPU CPU

mindspore.nn.AvgPool3d

在一个输入Tensor上应用3D平均池化运算,输入Tensor可看成是由一系列3D平面组成的。

Ascend CPU

mindspore.nn.FractionalMaxPool2d

对多个输入平面组成的输入上应用2D分数最大池化。

CPU

mindspore.nn.FractionalMaxPool3d

对多个输入平面组成的输入上应用3D分数最大池化。

GPU CPU

mindspore.nn.LPPool1d

在一个输入Tensor上应用1D LP池化运算,可被视为组成一个1D输入平面。

Ascend GPU CPU

mindspore.nn.LPPool2d

在一个输入Tensor上应用2D LP池化运算,可被视为组成一个2D输入平面。

Ascend GPU CPU

mindspore.nn.MaxPool1d

在一个输入Tensor上应用1D最大池化运算,该Tensor可被视为一维平面的组合。

Ascend GPU CPU

mindspore.nn.MaxPool2d

在一个输入Tensor上应用2D最大池化运算,可被视为组成一个2D平面。

Ascend GPU CPU

mindspore.nn.MaxPool3d

在一个输入Tensor上应用3D最大池化运算,输入Tensor可看成是由一系列3D平面组成的。

GPU

mindspore.nn.MaxUnpool1d

MaxPool1d 的部分逆过程。

GPU CPU

mindspore.nn.MaxUnpool2d

MaxPool2d 的部分逆过程。

GPU CPU

mindspore.nn.MaxUnpool3d

MaxPool3d 的部分逆过程。

GPU CPU

填充层

接口名

概述

支持平台

mindspore.nn.Pad

根据 paddingsmode 对输入进行填充。

Ascend GPU CPU

mindspore.nn.ConstantPad1d

将给定的常量填充到多维输入数据的最后一维。

Ascend GPU CPU

mindspore.nn.ConstantPad2d

将给定的常量填充到多维输入数据的最后两维。

Ascend GPU CPU

mindspore.nn.ConstantPad3d

将给定的常量填充到多维输入数据的最后三维。

Ascend GPU CPU

mindspore.nn.ReflectionPad1d

根据 padding 对输入 x 进行填充。

Ascend GPU CPU

mindspore.nn.ReflectionPad2d

根据 padding 对输入 x 进行填充。

Ascend GPU CPU

mindspore.nn.ReplicationPad1d

根据 padding 对输入 x 的W维度上进行填充。

GPU

mindspore.nn.ReplicationPad2d

根据 padding 对输入 x 的HW维度上进行填充。

GPU

mindspore.nn.ReplicationPad3d

根据 padding 对输入 x 的DHW维度上进行填充。

GPU

mindspore.nn.ZeroPad2d

将零填充到多维输入数据的最后两维。

Ascend GPU CPU

损失函数

接口名

概述

支持平台

mindspore.nn.BCELoss

计算目标值和预测值之间的二值交叉熵损失值。

Ascend GPU CPU

mindspore.nn.BCEWithLogitsLoss

输入经过sigmoid激活函数后作为预测值,BCEWithLogitsLoss 计算预测值和目标值之间的二值交叉熵损失。

Ascend GPU CPU

mindspore.nn.CosineEmbeddingLoss

余弦相似度损失函数,用于测量两个Tensor之间的相似性。

Ascend GPU CPU

mindspore.nn.CrossEntropyLoss

计算预测值和目标值之间的交叉熵损失。

Ascend GPU CPU

mindspore.nn.CTCLoss

CTCLoss损失函数。

Ascend CPU

mindspore.nn.DiceLoss

Dice系数是一个集合相似性loss,用于计算两个样本之间的相似性。

Ascend GPU CPU

mindspore.nn.FocalLoss

FocalLoss函数解决了类别不平衡的问题。

Ascend

mindspore.nn.GaussianNLLLoss

服从高斯分布的负对数似然损失。

Ascend GPU CPU

mindspore.nn.HingeEmbeddingLoss

Hinge Embedding 损失函数。

Ascend GPU CPU

mindspore.nn.HuberLoss

HuberLoss计算预测值和目标值之间的误差。

Ascend GPU CPU

mindspore.nn.KLDivLoss

计算输入 logitslabels 的KL散度。

Ascend GPU CPU

mindspore.nn.L1Loss

L1Loss用于计算预测值和目标值之间的平均绝对误差。

Ascend GPU CPU

mindspore.nn.MarginRankingLoss

排序损失函数,用于创建一个衡量给定损失的标准。

Ascend GPU CPU

mindspore.nn.MSELoss

用于计算预测值与标签值之间的均方误差。

Ascend GPU CPU

mindspore.nn.MultiClassDiceLoss

对于多标签问题,可以将标签通过one-hot编码转换为多个二分类标签。

Ascend GPU CPU

mindspore.nn.NLLLoss

计算预测值和目标值之间的负对数似然损失。

Ascend GPU CPU

mindspore.nn.RMSELoss

RMSELoss用来测量 \(x\)\(y\) 元素之间的均方根误差,其中 \(x\) 是输入Tensor, \(y\) 是目标值。

Ascend GPU CPU

mindspore.nn.SampledSoftmaxLoss

抽样交叉熵损失函数。

GPU

mindspore.nn.SmoothL1Loss

SmoothL1损失函数,如果预测值和目标值的逐个元素绝对误差小于设定阈值 beta 则用平方项,否则用绝对误差项。

Ascend GPU CPU

mindspore.nn.SoftMarginLoss

针对二分类问题的损失函数。

Ascend GPU

mindspore.nn.SoftmaxCrossEntropyWithLogits

计算预测值与真实值之间的交叉熵。

Ascend GPU CPU

优化器

接口名

概述

支持平台

mindspore.nn.Adadelta

Adadelta算法的实现。

Ascend GPU CPU

mindspore.nn.Adagrad

Adagrad算法的实现。

Ascend CPU GPU

mindspore.nn.Adam

Adaptive Moment Estimation (Adam)算法的实现。

Ascend GPU CPU

mindspore.nn.AdaMax

AdaMax算法是基于无穷范数的Adam的一种变体。

Ascend GPU CPU

mindspore.nn.AdamOffload

此优化器在主机CPU上运行Adam优化算法,设备上仅执行网络参数的更新,最大限度地降低内存成本。

Ascend GPU CPU

mindspore.nn.AdamWeightDecay

权重衰减Adam算法的实现。

Ascend GPU CPU

mindspore.nn.AdaSumByDeltaWeightWrapCell

Adaptive Summation (AdaSum)算法的实现,根据更新前后的参数差计算。

Ascend GPU

mindspore.nn.AdaSumByGradWrapCell

Adaptive Summation (AdaSum)算法的实现,根据梯度计算。

Ascend GPU

mindspore.nn.ASGD

随机平均梯度下降(ASGD)算法的实现。

Ascend GPU CPU

mindspore.nn.FTRL

FTRL算法实现。

Ascend GPU

mindspore.nn.Lamb

LAMB(Layer-wise Adaptive Moments optimizer for Batching training,用于批训练的分层自适应矩优化器)算法的实现。

Ascend GPU

mindspore.nn.LARS

LARS算法的实现。

Ascend

mindspore.nn.LazyAdam

Adaptive Moment Estimation (Adam)算法的实现。

Ascend GPU CPU

mindspore.nn.Momentum

Momentum算法的实现。

Ascend GPU CPU

mindspore.nn.ProximalAdagrad

ProximalAdagrad算法的实现。

Ascend GPU CPU

mindspore.nn.RMSProp

均方根传播(RMSProp)算法的实现。

Ascend GPU CPU

mindspore.nn.Rprop

弹性反向传播(Rprop)算法的实现。

Ascend GPU CPU

mindspore.nn.SGD

随机梯度下降的实现。

Ascend GPU CPU

mindspore.nn.thor

通过二阶算法THOR更新参数。

Ascend GPU

动态学习率

LearningRateSchedule类

本模块中的动态学习率都是LearningRateSchedule的子类,将LearningRateSchedule的实例传递给优化器。在训练过程中,优化器以当前step为输入调用该实例,得到当前的学习率。

import mindspore.nn as nn

min_lr = 0.01
max_lr = 0.1
decay_steps = 4
cosine_decay_lr = nn.CosineDecayLR(min_lr, max_lr, decay_steps)

net = Net()
optim = nn.Momentum(net.trainable_params(), learning_rate=cosine_decay_lr, momentum=0.9)

接口名

概述

支持平台

mindspore.nn.CosineDecayLR

基于余弦衰减函数计算学习率。

Ascend GPU

mindspore.nn.ExponentialDecayLR

基于指数衰减函数计算学习率。

Ascend GPU CPU

mindspore.nn.InverseDecayLR

基于逆时衰减函数计算学习率。

Ascend GPU CPU

mindspore.nn.NaturalExpDecayLR

基于自然指数衰减函数计算学习率。

Ascend GPU CPU

mindspore.nn.PolynomialDecayLR

基于多项式衰减函数计算学习率。

Ascend GPU

mindspore.nn.WarmUpLR

预热学习率。

Ascend GPU

Dynamic LR函数

本模块中的动态学习率都是function,调用function并将结果传递给优化器。在训练过程中,优化器将result[current step]作为当前学习率。

import mindspore.nn as nn

min_lr = 0.01
max_lr = 0.1
total_step = 6
step_per_epoch = 1
decay_epoch = 4

lr= nn.cosine_decay_lr(min_lr, max_lr, total_step, step_per_epoch, decay_epoch)

net = Net()
optim = nn.Momentum(net.trainable_params(), learning_rate=lr, momentum=0.9)

接口名

概述

支持平台

mindspore.nn.cosine_decay_lr

基于余弦衰减函数计算学习率。

Ascend GPU CPU

mindspore.nn.exponential_decay_lr

基于指数衰减函数计算学习率。

Ascend GPU CPU

mindspore.nn.inverse_decay_lr

基于逆时衰减函数计算学习率。

Ascend GPU CPU

mindspore.nn.natural_exp_decay_lr

基于自然指数衰减函数计算学习率。

Ascend GPU CPU

mindspore.nn.piecewise_constant_lr

获取分段常量学习率。

Ascend GPU CPU

mindspore.nn.polynomial_decay_lr

基于多项式衰减函数计算学习率。

Ascend GPU CPU

mindspore.nn.warmup_lr

预热学习率。

Ascend GPU CPU

图像处理层

接口名

概述

支持平台

mindspore.nn.PixelShuffle

在多个输入平面组成的输入上面应用PixelShuffle算法。

Ascend GPU CPU

mindspore.nn.PixelUnshuffle

在多个输入平面组成的输入上面应用PixelUnshuffle算法。

Ascend GPU CPU

mindspore.nn.ResizeBilinear

使用双线性插值调整输入Tensor为指定的大小。

Ascend CPU GPU

工具

接口名

概述

支持平台

mindspore.nn.ChannelShuffle

将shape的为 \((*, C, H, W)\) 的Tensor的通道划分成 \(g\) 组,并将其以 \((*, C \frac g, g, H, W)\) 的shape重新排列, 同时保持Tensor原有的shape。

Ascend GPU CPU

mindspore.nn.Flatten

对输入Tensor的第零维之外的维度进行展平操作。

Ascend GPU CPU

数学运算

接口名

概述

支持平台

mindspore.nn.Moments

沿指定轴 axis 计算输入 x 的均值和方差。

Ascend GPU CPU