mindspore.nn
神经网络Cell。
用于构建神经网络中的预定义构建块或计算单元。
MindSpore中 mindspore.nn 接口与上一版本相比,新增、删除和支持平台的变化信息请参考 API Updates。
基本构成单元
| 接口名 | 概述 | 支持平台 | 
| MindSpore中神经网络的基本构成单元。 | 
 | |
| 运行从MindIR加载的计算图。 | 
 | |
| 损失函数的基类。 | 
 | |
| 用于参数更新的优化器基类。 | 
 | 
容器
| 接口名 | 概述 | 支持平台 | 
| 构造Cell列表。 | 
 | |
| 构造Cell顺序容器。 | 
 | 
封装层
| 接口名 | 概述 | 支持平台 | 
| 分布式优化器。 | 
 | |
| 用于动态更新损失缩放系数(loss scale)的神经元。 | 
 | |
| 固定损失缩放系数的神经元。 | 
 | |
| 训练网络的封装。 | 
 | |
| 用于获取下一条数据的Cell。 | 
 | |
| Wrap the network with Batch Size. | 
 | |
| 更新参数的Cell。 | 
 | |
| 将MiniBatch切分成更细粒度的MicroBatch,用于流水线并行的训练中。 | 
 | |
| The time distributed layer. | 
 | |
| 训练网络封装类。 | 
 | |
| 使用混合精度功能的训练网络。 | 
 | |
| 封装前向网络和损失函数,返回用于计算评估指标的损失函数值、前向输出和标签。 | 
 | |
| Cell that returns the gradients. | 
 | |
| 包含损失函数的Cell。 | 
 | 
卷积神经网络层
| 接口名 | 概述 | 支持平台 | 
| 一维卷积层。 | 
 | |
| 一维转置卷积层。 | 
 | |
| 二维卷积层。 | 
 | |
| 二维转置卷积层。 | 
 | |
| 三维卷积层。 | 
 | |
| 三维转置卷积层。 | 
 | |
| 从图像中提取滑窗的区域块。 | 
 | 
循环神经网络层
| 接口名 | 概述 | 支持平台 | 
| 循环神经网络(RNN)层,其使用的激活函数为tanh或relu。 | 
 | |
| 循环神经网络单元,激活函数是tanh或relu。 | 
 | |
| GRU(Gate Recurrent Unit)称为门控循环单元网络,是循环神经网络(Recurrent Neural Network, RNN)的一种。 | 
 | |
| GRU(Gate Recurrent Unit)称为门控循环单元。 | 
 | |
| 长短期记忆(LSTM)网络,根据输出序列和给定的初始状态计算输出序列和最终状态。 | 
 | |
| 长短期记忆网络单元(LSTMCell)。 | 
 | 
嵌入层
| 接口名 | 概述 | 支持平台 | 
| 嵌入层。 | 
 | |
| 嵌入查找层。 | 
 | |
| Returns a slice of input tensor based on the specified indices and the field ids. | 
 | 
非线性激活函数层
| 接口名 | 概述 | 支持平台 | 
| Continuously differentiable exponential linear units activation function. | 
 | |
| 指数线性单元激活函数(Exponential Linear Unit activation function)。 | 
 | |
| 快速高斯误差线性单元激活函数(Fast Gaussian Error Linear Units activation function)。 | 
 | |
| 高斯误差线性单元激活函数(Gaussian error linear unit activation function)。 | 
 | |
| Hard Shrink激活函数,按输入元素计算输出,公式定义如下:。 | 
 | |
| Hard Sigmoid激活函数,按元素计算输出。 | 
 | |
| Hard Swish激活函数。 | 
 | |
| Leaky ReLU激活函数。 | 
 | |
| Log Sigmoid激活函数。 | 
 | |
| Log Softmax激活函数。 | 
 | |
| PReLU激活层(PReLU Activation Operator)。 | 
 | |
| 修正线性单元激活函数(Rectified Linear Unit activation function)。 | 
 | |
| ReLU6激活函数。 | 
 | |
| Sigmoid激活函数。 | 
 | |
| Softmax函数,它是二分类函数  | 
 | |
| Applies the SoftShrink function element-wise. | 
 | |
| Tanh激活函数。 | 
 | 
线性层
| 接口名 | 概述 | 支持平台 | 
| 全连接层。 | 
 | 
Dropout层
| 接口名 | 概述 | 支持平台 | 
| 随机丢弃层。 | 
 | 
归一化层
| 接口名 | 概述 | 支持平台 | 
| 对输入的二维数据进行批归一化(Batch Normalization Layer)。 | 
 | |
| 对输入的四维数据进行批归一化(Batch Normalization Layer)。 | 
 | |
| 对输入的五维数据进行批归一化(Batch Normalization Layer)。 | 
 | |
| The GlobalBatchNorm interface is deprecated, please use the  | 弃用 | |
| 在mini-batch输入上进行组归一化。 | 
 | |
| 对四维输入实现实例归一化(Instance Normalization Layer)。 | 
 | |
| 在mini-batch输入上应用层归一化(Layer Normalization)。 | 
 | |
| Sync Batch Normalization layer over a N-dimension input. | 
 | 
池化层
| 接口名 | 概述 | 支持平台 | 
| 对输入的多维数据进行一维平面上的平均池化运算。 | 
 | |
| 对输入的多维数据进行二维的平均池化运算。 | 
 | |
| 对输入的多维数据进行一维平面上的最大池化运算。 | 
 | |
| 对输入的多维数据进行二维的最大池化运算。 | 
 | 
填充层
| 接口名 | 概述 | 支持平台 | 
| 根据 paddings 和 mode 对输入进行填充。 | 
 | 
损失函数
| 接口名 | 概述 | 支持平台 | 
| 计算目标值和预测值之间的二值交叉熵损失值。 | 
 | |
| 输入经过sigmoid激活函数后作为预测值,BCEWithLogitsLoss计算预测值和目标值之间的二值交叉熵损失。 | 
 | |
| CosineEmbeddingLoss creates a criterion to measure the similarity between two tensors using cosine distance. | 
 | |
| Dice系数是一个集合相似性loss,用于计算两个样本之间的相似性。 | 
 | |
| FocalLoss函数。 | 
 | |
| L1Loss用于计算预测值和目标值之间的平均绝对误差。 | 
 | |
| 用于计算预测值与标签值之间的均方误差。 | 
 | |
| When there are multiple classifications, label is transformed into multiple binary classifications by one hot. | 
 | |
| RMSELoss用来测量 \(x\) 和 \(y\) 元素之间的均方根误差,其中 \(x\) 是输入Tensor, \(y\) 是目标值。 | 
 | |
| 抽样交叉熵损失函数。 | 
 | |
| SmoothL1损失函数,如果预测值和目标值的逐个元素绝对误差小于设定阈值 beta 则用平方项,否则用绝对误差项。 | 
 | |
| 针对二分类问题的损失函数。 | 
 | |
| 计算预测值与真实值之间的交叉熵。 | 
 | 
优化器
| 接口名 | 概述 | 支持平台 | 
| Adagrad算法的实现。 | 
 | |
| Adaptive Moment Estimation (Adam)算法的实现。 | 
 | |
| 此优化器在主机CPU上运行Adam优化算法,设备上仅执行网络参数的更新,最大限度地降低内存成本。 | 
 | |
| 权重衰减Adam算法的实现。 | 
 | |
| Adaptive Summation (AdaSum)算法的实现,根据更新前后的参数差计算。 | 
 | |
| Adaptive Summation (AdaSum)算法的实现,根据梯度计算。 | 
 | |
| Implements Average Stochastic Gradient Descent. | 
 | |
| FTRL算法实现。 | 
 | |
| LAMB(Layer-wise Adaptive Moments optimizer for Batching training,用于批训练的分层自适应矩优化器)算法的实现。 | 
 | |
| LARS算法的实现。 | 
 | |
| Adaptive Moment Estimation (Adam)算法的实现。 | 
 | |
| Momentum算法的实现。 | 
 | |
| ProximalAdagrad算法的实现。 | 
 | |
| 均方根传播(RMSProp)算法的实现。 | 
 | |
| Implements Resilient backpropagation. | 
 | |
| 随机梯度下降的实现。 | 
 | |
| 通过二阶算法THOR更新参数。 | 
 | 
评价指标
| 接口名 | 概述 | 支持平台 | 
| 计算数据分类的正确率,包括二分类和多分类。 | 
 | |
| 使用梯形法则计算曲线下面积AUC(Area Under the Curve,AUC)。 | 
 | |
| 计算具有一个或多个引用的机器翻译文本的BLEU分数。 | 
 | |
| 计算混淆矩阵(confusion matrix),通常用于评估分类模型的性能,包括二分类和多分类场景。 | 
 | |
| 计算与混淆矩阵相关的度量。 | 
 | |
| 计算余弦相似度。 | 
 | |
| 集合相似性度量。 | 
 | |
| 计算F1 score。 | 
 | |
| 计算Fbeta评分。 | 
 | |
| 计算Hausdorff距离。 | 
 | |
| 根据输入的 name 获取metric的方法。 | 
 | |
| 计算loss的平均值。 | 
 | |
| 计算平均绝对误差MAE(Mean Absolute Error)。 | 
 | |
| 计算从 y_pred 到 y 的平均表面距离。 | 
 | |
| 用于计算评估指标的基类。 | 
 | |
| 测量均方差MSE(Mean Squared Error)。 | 
 | |
| 获取所有metric的名称。 | 
 | |
| 用于计算神经网络对给定图像的遮挡灵敏度(Occlusion Sensitivity),表示了图像的哪些部分对神经网络的分类决策最重要。 | 
 | |
| 计算困惑度(perplexity)。 | 
 | |
| 计算数据分类的精度,包括单标签场景和多标签场景。 | 
 | |
| 计算数据分类的召回率,包括单标签场景和多标签场景。 | 
 | |
| 计算ROC曲线。 | 
 | |
| 计算从 y_pred 到 y 的均方根表面距离。 | 
 | |
| 此装饰器用于根据类的 indexes 属性对输入重新排列。 | 
 | |
| 计算top-1分类正确率。 | 
 | |
| 计算top-5分类正确率。 | 
 | |
| 计算top-k分类正确率。 | 
 | 
动态学习率
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)
| 接口名 | 概述 | 支持平台 | 
| 基于余弦衰减函数计算学习率。 | 
 | |
| 基于指数衰减函数计算学习率。 | 
 | |
| 基于逆时衰减函数计算学习率。 | 
 | |
| 基于自然指数衰减函数计算学习率。 | 
 | |
| 基于多项式衰减函数计算学习率。 | 
 | |
| 预热学习率。 | 
 | 
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)
| 接口名 | 概述 | 支持平台 | 
| 基于余弦衰减函数计算学习率。 | 
 | |
| 基于指数衰减函数计算学习率。 | 
 | |
| 基于逆时衰减函数计算学习率。 | 
 | |
| 基于自然指数衰减函数计算学习率。 | 
 | |
| 获取分段常量学习率。 | 
 | |
| 基于多项式衰减函数计算学习率。 | 
 | |
| 预热学习率。 | 
 | 
稀疏层
| 接口名 | 概述 | 支持平台 | 
| Multiplies sparse matrix a and dense matrix b. | 
 | |
| Converts a sparse tensor(COOTensor) into dense. | 
 | 
图像处理层
| 接口名 | 概述 | 支持平台 | 
| Crops the central region of the images with the central_fraction. | 
 | |
| Returns two tensors, the first is along the height dimension and the second is along the width dimension. | 
 | |
| Returns MS-SSIM index between two images. | 
 | |
| Returns Peak Signal-to-Noise Ratio of two image batches. | 
 | |
| 使用双线性插值调整输入Tensor为指定的大小。 | 
 | |
| Returns SSIM index between two images. | 
 | 
矩阵处理
| 接口名 | 概述 | 支持平台 | 
| Returns a batched diagonal tensor with a given batched diagonal values. | 
 | |
| Returns the batched diagonal part of a batched tensor. | 
 | |
| Modifies the batched diagonal part of a batched tensor. | 
 | 
工具
| 接口名 | 概述 | 支持平台 | 
| 对输入Tensor的值进行裁剪,使用 \(L_2\) 范数控制梯度。 | 
 | |
| 对输入Tensor的第0维之外的维度进行展平操作。 | 
 | |
| 获取激活函数。 | 
 | |
| 对权重计算L1正则化。 | 
 | |
| Computes the norm of vectors, currently including Euclidean norm, i.e., \(L_2\)-norm. | 
 | |
| 对输入进行one-hot编码并返回。 | 
 | |
| Creates a sequence of numbers in range [start, limit) with step size delta. | 
 | |
| Rolls the elements of a tensor along an axis. | 
 | |
| 返回一个Tensor,指定主对角线以上的元素被置为零。 | 
 | |
| 返回一个Tensor,指定主对角线以下的元素被置为0。 | 
 | 
数学运算
| 接口名 | 概述 | 支持平台 | 
| The nn.MatMul interface is deprecated, please use the  | 弃用 | |
| 沿指定轴 axis 计算输入 x 的均值和方差。 | 
 | |
| Reduces a dimension of a tensor by calculating exponential for all elements in the dimension, then calculate logarithm of the sum. | 
 | 
梯度
| 接口名 | 概述 | 支持平台 | 
| 计算给定网络的雅可比向量积(Jacobian-vector product, JVP)。 | 
 | |
| 计算给定网络的向量雅可比积(vector-Jacobian product, VJP)。 | 
 |