2022-04-25 12:55:14 +08:00
mindspore.nn
=============
Neural Network Cell
For building predefined building blocks or computational units in neural networks.
2022-11-08 09:24:43 +08:00
Compared with the previous version, the added, deleted and supported platforms change information of `mindspore.nn` operators in MindSpore, please refer to the link `API Updates <https://gitee.com/mindspore/docs/blob/master/resource/api_updates/nn_api_updates.md> `_ .
2022-04-25 12:55:14 +08:00
2022-05-19 16:41:45 +08:00
Basic Block
-----------
2022-04-25 12:55:14 +08:00
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.Cell
mindspore.nn.GraphCell
mindspore.nn.LossBase
mindspore.nn.Optimizer
Container
---------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.CellList
mindspore.nn.SequentialCell
2022-05-19 16:41:45 +08:00
Wrapper Layer
-------------
2022-04-25 12:55:14 +08:00
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.DistributedGradReducer
mindspore.nn.DynamicLossScaleUpdateCell
mindspore.nn.FixedLossScaleUpdateCell
mindspore.nn.ForwardValueAndGrad
mindspore.nn.GetNextSingleOp
mindspore.nn.MicroBatchInterleaved
mindspore.nn.ParameterUpdate
mindspore.nn.PipelineCell
mindspore.nn.TimeDistributed
mindspore.nn.TrainOneStepCell
mindspore.nn.TrainOneStepWithLossScaleCell
mindspore.nn.WithEvalCell
mindspore.nn.WithGradCell
mindspore.nn.WithLossCell
Convolutional Neural Network Layer
----------------------------------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.Conv1d
mindspore.nn.Conv1dTranspose
mindspore.nn.Conv2d
mindspore.nn.Conv2dTranspose
mindspore.nn.Conv3d
mindspore.nn.Conv3dTranspose
mindspore.nn.Unfold
Recurrent Neural Network Layer
------------------------------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.RNN
mindspore.nn.RNNCell
mindspore.nn.GRU
mindspore.nn.GRUCell
mindspore.nn.LSTM
mindspore.nn.LSTMCell
2022-06-16 21:43:25 +08:00
2022-04-25 12:55:14 +08:00
Embedding Layer
---------------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.Embedding
mindspore.nn.EmbeddingLookup
mindspore.nn.MultiFieldEmbeddingLookup
Nonlinear Activation Function Layer
-----------------------------------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.CELU
mindspore.nn.ELU
mindspore.nn.FastGelu
mindspore.nn.GELU
2022-10-10 14:22:11 +08:00
mindspore.nn.GLU
2022-09-05 16:31:41 +08:00
mindspore.nn.get_activation
2022-05-11 14:23:11 +08:00
mindspore.nn.Hardtanh
2022-04-25 12:55:14 +08:00
mindspore.nn.HShrink
mindspore.nn.HSigmoid
mindspore.nn.HSwish
mindspore.nn.LeakyReLU
mindspore.nn.LogSigmoid
mindspore.nn.LogSoftmax
2022-07-15 14:47:15 +08:00
mindspore.nn.LRN
mindspore.nn.Mish
2022-05-06 16:39:07 +08:00
mindspore.nn.Softsign
2022-04-25 12:55:14 +08:00
mindspore.nn.PReLU
mindspore.nn.ReLU
mindspore.nn.ReLU6
2022-06-28 17:51:39 +08:00
mindspore.nn.RReLU
2022-07-15 14:47:15 +08:00
mindspore.nn.SeLU
2022-05-05 16:57:29 +08:00
mindspore.nn.SiLU
2022-04-25 12:55:14 +08:00
mindspore.nn.Sigmoid
2022-05-05 16:57:29 +08:00
mindspore.nn.Softmin
2022-04-25 12:55:14 +08:00
mindspore.nn.Softmax
2022-10-09 17:34:21 +08:00
mindspore.nn.Softmax2d
2022-04-25 12:55:14 +08:00
mindspore.nn.SoftShrink
mindspore.nn.Tanh
2022-06-22 14:52:28 +08:00
mindspore.nn.Tanhshrink
2022-05-23 14:51:53 +08:00
mindspore.nn.Threshold
2022-04-25 12:55:14 +08:00
Linear Layer
------------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.Dense
2022-05-23 11:14:52 +08:00
mindspore.nn.BiDense
2022-04-25 12:55:14 +08:00
Dropout Layer
-------------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.Dropout
2022-10-25 16:18:46 +08:00
mindspore.nn.Dropout1d
2022-07-08 15:23:45 +08:00
mindspore.nn.Dropout2d
mindspore.nn.Dropout3d
2022-04-25 12:55:14 +08:00
Normalization Layer
-------------------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.BatchNorm1d
mindspore.nn.BatchNorm2d
mindspore.nn.BatchNorm3d
mindspore.nn.GroupNorm
2022-06-22 12:15:53 +08:00
mindspore.nn.InstanceNorm1d
2022-04-25 12:55:14 +08:00
mindspore.nn.InstanceNorm2d
2022-06-22 12:15:53 +08:00
mindspore.nn.InstanceNorm3d
2022-04-25 12:55:14 +08:00
mindspore.nn.LayerNorm
mindspore.nn.SyncBatchNorm
Pooling Layer
-------------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
2022-05-16 16:19:24 +08:00
mindspore.nn.AdaptiveAvgPool1d
2022-07-13 17:03:50 +08:00
mindspore.nn.AdaptiveAvgPool2d
2022-08-29 09:40:07 +08:00
mindspore.nn.AdaptiveAvgPool3d
2022-05-18 15:50:44 +08:00
mindspore.nn.AdaptiveMaxPool1d
2022-05-28 12:01:53 +08:00
mindspore.nn.AdaptiveMaxPool2d
2022-07-13 17:03:50 +08:00
mindspore.nn.AdaptiveMaxPool3d
2022-04-25 12:55:14 +08:00
mindspore.nn.AvgPool1d
mindspore.nn.AvgPool2d
2022-10-08 16:17:20 +08:00
mindspore.nn.AvgPool3d
2022-10-09 14:57:46 +08:00
mindspore.nn.FractionalMaxPool2d
mindspore.nn.FractionalMaxPool3d
2022-10-19 14:59:03 +08:00
mindspore.nn.LPPool1d
mindspore.nn.LPPool2d
2022-04-25 12:55:14 +08:00
mindspore.nn.MaxPool1d
mindspore.nn.MaxPool2d
2022-10-08 16:17:20 +08:00
mindspore.nn.MaxPool3d
2022-10-12 20:15:45 +08:00
mindspore.nn.MaxUnpool1d
mindspore.nn.MaxUnpool2d
mindspore.nn.MaxUnpool3d
2022-04-25 12:55:14 +08:00
2022-05-19 16:41:45 +08:00
Padding Layer
2022-04-25 12:55:14 +08:00
-------------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.Pad
2022-05-09 20:32:42 +08:00
mindspore.nn.ConstantPad1d
mindspore.nn.ConstantPad2d
mindspore.nn.ConstantPad3d
2022-06-06 15:28:35 +08:00
mindspore.nn.ReflectionPad1d
mindspore.nn.ReflectionPad2d
2022-10-09 15:48:06 +08:00
mindspore.nn.ReplicationPad1d
mindspore.nn.ReplicationPad2d
mindspore.nn.ReplicationPad3d
2022-05-09 20:32:42 +08:00
mindspore.nn.ZeroPad2d
2022-04-25 12:55:14 +08:00
Loss Function
-------------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.BCELoss
mindspore.nn.BCEWithLogitsLoss
mindspore.nn.CosineEmbeddingLoss
2022-05-30 10:27:57 +08:00
mindspore.nn.CrossEntropyLoss
2022-10-17 16:27:44 +08:00
mindspore.nn.CTCLoss
2022-04-25 12:55:14 +08:00
mindspore.nn.DiceLoss
mindspore.nn.FocalLoss
2022-10-17 14:24:17 +08:00
mindspore.nn.GaussianNLLLoss
2022-10-22 14:26:45 +08:00
mindspore.nn.HingeEmbeddingLoss
2022-05-11 15:39:02 +08:00
mindspore.nn.HuberLoss
2022-09-20 15:51:49 +08:00
mindspore.nn.KLDivLoss
2022-04-25 12:55:14 +08:00
mindspore.nn.L1Loss
2022-10-14 15:26:42 +08:00
mindspore.nn.MarginRankingLoss
2022-04-25 12:55:14 +08:00
mindspore.nn.MSELoss
mindspore.nn.MultiClassDiceLoss
2022-05-30 10:27:57 +08:00
mindspore.nn.NLLLoss
2022-04-25 12:55:14 +08:00
mindspore.nn.RMSELoss
mindspore.nn.SampledSoftmaxLoss
mindspore.nn.SmoothL1Loss
mindspore.nn.SoftMarginLoss
mindspore.nn.SoftmaxCrossEntropyWithLogits
Optimizer
---------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
2022-06-19 08:01:14 +08:00
mindspore.nn.Adadelta
2022-04-25 12:55:14 +08:00
mindspore.nn.Adagrad
mindspore.nn.Adam
2022-06-16 21:43:25 +08:00
mindspore.nn.AdaMax
2022-04-25 12:55:14 +08:00
mindspore.nn.AdamOffload
mindspore.nn.AdamWeightDecay
mindspore.nn.AdaSumByDeltaWeightWrapCell
mindspore.nn.AdaSumByGradWrapCell
mindspore.nn.ASGD
mindspore.nn.FTRL
mindspore.nn.Lamb
mindspore.nn.LARS
mindspore.nn.LazyAdam
mindspore.nn.Momentum
mindspore.nn.ProximalAdagrad
mindspore.nn.RMSProp
mindspore.nn.Rprop
mindspore.nn.SGD
mindspore.nn.thor
Dynamic Learning Rate
---------------------
LearningRateSchedule Class
^^^^^^^^^^^^^^^^^^^^^^^^^^
The dynamic learning rates in this module are all subclasses of LearningRateSchedule. Pass the instance of
LearningRateSchedule to an optimizer. During the training process, the optimizer calls the instance taking current step
as input to get the current learning rate.
.. code-block ::
import mindspore.nn as nn
2022-06-16 21:43:25 +08:00
2022-04-25 12:55:14 +08:00
min_lr = 0.01
max_lr = 0.1
decay_steps = 4
cosine_decay_lr = nn.CosineDecayLR(min_lr, max_lr, decay_steps)
2022-06-16 21:43:25 +08:00
2022-04-25 12:55:14 +08:00
net = Net()
optim = nn.Momentum(net.trainable_params(), learning_rate=cosine_decay_lr, momentum=0.9)
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.CosineDecayLR
mindspore.nn.ExponentialDecayLR
mindspore.nn.InverseDecayLR
mindspore.nn.NaturalExpDecayLR
mindspore.nn.PolynomialDecayLR
mindspore.nn.WarmUpLR
Dynamic LR Function
^^^^^^^^^^^^^^^^^^^
The dynamic learning rates in this module are all functions. Call the function and pass the result to an optimizer.
During the training process, the optimizer takes result[current step] as current learning rate.
.. code-block ::
import mindspore.nn as nn
2022-06-16 21:43:25 +08:00
2022-04-25 12:55:14 +08:00
min_lr = 0.01
max_lr = 0.1
total_step = 6
step_per_epoch = 1
decay_epoch = 4
2022-06-16 21:43:25 +08:00
2022-04-25 12:55:14 +08:00
lr= nn.cosine_decay_lr(min_lr, max_lr, total_step, step_per_epoch, decay_epoch)
2022-06-16 21:43:25 +08:00
2022-04-25 12:55:14 +08:00
net = Net()
optim = nn.Momentum(net.trainable_params(), learning_rate=lr, momentum=0.9)
2022-06-16 21:43:25 +08:00
2022-04-25 12:55:14 +08:00
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
mindspore.nn.cosine_decay_lr
mindspore.nn.exponential_decay_lr
mindspore.nn.inverse_decay_lr
mindspore.nn.natural_exp_decay_lr
mindspore.nn.piecewise_constant_lr
mindspore.nn.polynomial_decay_lr
mindspore.nn.warmup_lr
Image Processing Layer
----------------------
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
2022-10-22 15:19:19 +08:00
mindspore.nn.PixelShuffle
mindspore.nn.PixelUnshuffle
2022-04-25 12:55:14 +08:00
mindspore.nn.ResizeBilinear
Tools
-----
.. msplatformautosummary ::
:toctree: nn
:nosignatures:
:template: classtemplate.rst
2022-10-20 20:05:15 +08:00
mindspore.nn.ChannelShuffle
2022-04-25 12:55:14 +08:00
mindspore.nn.Flatten