mindspore.nn

Neural Networks Cells.

Pre-defined building blocks or computing units to construct Neural Networks.

  • class mindspore.nn.Accuracy(eval_type='classification')[source]
  • Calculates the accuracy for classification and multilabel data.

The accuracy class creates two local variables, correct number and total number that are used to compute thefrequency with which predictions matches labels. This frequency is ultimately returned as the accuracy: anidempotent operation that simply divides correct number by total number.

mindspore.nn - 图1

  • Parameters
  • eval_type (str) – Metric to calculate the accuracy over a dataset, forclassification (single-label), and multilabel (multilabel classification).Default: ‘classification’.

Examples

  1. Copy>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mindspore.float32)
  2. >>> y = Tensor(np.array([1, 0, 1]), mindspore.float32)
  3. >>> metric = nn.Accuracy('classification')
  4. >>> metric.clear()
  5. >>> metric.update(x, y)
  6. >>> accuracy = metric.eval()
  • clear()[source]
  • Clears the internal evaluation result.

  • eval()[source]

  • Computes the accuracy.

    • Returns
    • Float, the computed result.

    • Raises

    • RuntimeError – If the sample size is 0.
  • update(*inputs)[source]

  • Updates the internal evaluation result

mindspore.nn - 图2
and
mindspore.nn - 图3
.

  1. - Parameters
  2. -

inputs – Input y_pred and y. y_pred and y are a Tensor, a list or an array.y_pred is in most cases (not strictly) a list of floating numbers in range

mindspore.nn - 图4
and the shape is
mindspore.nn - 图5
, where
mindspore.nn - 图6
is the number of cases and
mindspore.nn - 图7
is the number of categories. For ‘multilabel’ evaluation type, y_pred can only be one-hotencoding with values 0 or 1. Indices with 1 indicate positive category. y contains valuesof integers. The shape is
mindspore.nn - 图8
if one-hot encoding is used. One-hot encodingshould be used when ‘eval_type’ is ‘multilabel’. Shape can also be
mindspore.nn - 图9
if categoryindex is used in ‘classification’ evaluation type.

  1. - Raises
  2. -

ValueError – If the number of the input is not 2.

  • class mindspore.nn.Adam(params, learning_rate=0.001, beta1=0.9, beta2=0.999, eps=1e-08, use_locking=False, use_nesterov=False, weight_decay=0.0, loss_scale=1.0)[source]
  • Updates gradients by Adaptive Moment Estimation (Adam) algorithm.

The Adam algorithm is proposed in Adam: A Method for Stochastic Optimization.

The updating formulas are as follows,

mindspore.nn - 图10

mindspore.nn - 图11
represents the 1st moment vector moment1,
mindspore.nn - 图12
represents the 2nd moment vector moment2,
mindspore.nn - 图13
represents gradients,
mindspore.nn - 图14
represents scaling factor lr,
mindspore.nn - 图15
representbeta1 and beta2,
mindspore.nn - 图16
represents updating step while
mindspore.nn - 图17
and
mindspore.nn - 图18
representbeta1_power and beta2_power,
mindspore.nn - 图19
represents learning_rate,
mindspore.nn - 图20
represents params,
mindspore.nn - 图21
represents eps.

  • Parameters
    • params (list[Parameter]) – A list of parameter, which will be updated. The element in _params_should be class mindspore.Parameter.

    • learning_rate (float) – The Learning rate.

    • beta1 (float) – The exponential decay rate for the 1st moment estimates. Should be in range (0.0, 1.0).

    • beta2 (float) – The exponential decay rate for the 2nd moment estimates. Should be in range (0.0, 1.0).

    • eps (float) – Term added to the denominator to improve numerical stability. Should be greater than 0.

    • use_locking (bool) – Whether to enable a lock to protect updating variable tensors.If True, updating of the var, m, and v tensors will be protected by a lock.If False, the result is unpredictable. Default: False.

    • use_nesterov (bool) – Whether to use Nesterov Accelerated Gradient (NAG) algorithm to update the gradients.If True, updates the gradients using NAG.If False, updates the gradients without using NAG. Default: False.

    • weight_decay (float) – Weight decay (L2 penalty). Default: 0.0.

    • loss_scale (float) – A floating point value for the loss scale. Default: 1.0.Should be equal to or greater than 1.

  • Inputs:

    • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.
  • Outputs:

  • Tensor[bool], the value is True.

Examples

  1. Copy>>> net = Net()
  2. >>> loss = nn.SoftmaxCrossEntropyWithLogits()
  3. >>> optim = nn.Adam(params=net.trainable_params())
  4. >>> model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)
  • class mindspore.nn.AdamWeightDecay(params, learning_rate=0.001, beta1=0.9, beta2=0.999, eps=1e-06, weight_decay=0.0)[source]
  • Implements Adam algorithm weight decay fix.

    • Parameters
      • params (list[Parameter]) – A list of parameter, which will be updated. The element in _params_should be class mindspore.Parameter.

      • learning_rate (float) – A floating point value for the learning rate. Default: 1e-3.

      • beta1 (float) – The exponential decay rate for the 1st moment estimates. Default: 0.9.Should be in range (0.0, 1.0).

      • beta2 (float) – The exponential decay rate for the 2nd moment estimates. Default: 0.999.Should be in range (0.0, 1.0).

      • eps (float) – Term added to the denominator to improve numerical stability. Default: 1e-6.Should be greater than 0.

      • weight_decay (float) – Weight decay (L2 penalty). Default: 0.0.

    • Inputs:

      • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.
    • Outputs:

    • tuple[Parameter], the updated velocity value, the shape is the same as params.

Examples

  1. Copy>>> net = Net()
  2. >>> loss = nn.SoftmaxCrossEntropyWithLogits()
  3. >>> optim = nn.AdamWeightDecay(params=net.trainable_params())
  4. >>> model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)
  • class mindspore.nn.AdamWeightDecayDynamicLR(params, decay_steps, learning_rate=0.001, end_learning_rate=0.0001, power=10.0, beta1=0.9, beta2=0.999, eps=1e-06, weight_decay=0.0)[source]
  • Adam Weight Decay Dynamic Learning Rate (LR).

    • Parameters
      • params (list[Parameter]) – A list of parameter, which will be updated. The element in _params_should be class mindspore.Parameter.

      • decay_steps (int) – The steps of the decay.

      • learning_rate (float) – A floating point value for the learning rate. Default: 0.001.

      • end_learning_rate (float) – A floating point value for the end learning rate. Default: 0.0001.

      • power (float) – Power. Default: 10.0.

      • beta1 (float) – The exponential decay rate for the 1st moment estimates. Default: 0.9.Should be in range (0.0, 1.0).

      • beta2 (float) – The exponential decay rate for the 2nd moment estimates. Default: 0.999.Should be in range (0.0, 1.0).

      • eps (float) – Term added to the denominator to improve numerical stability. Default: 1e-6.Should be greater than 0.

      • weight_decay (float) – Weight decay (L2 penalty). Default: 0.0.

    • Inputs:

      • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.
    • Outputs:

    • tuple[Parameter], the updated velocity value, the shape is the same as params.

Examples

  1. Copy>>> net = Net()
  2. >>> loss = nn.SoftmaxCrossEntropyWithLogits()
  3. >>> optim = nn.AdamWeightDecayDynamicLR(params=net.trainable_params(), decay_steps=10)
  4. >>> model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)
  • class mindspore.nn.AvgPool2d(kernel_size=1, stride=1, pad_mode='VALID', padding=0)[source]
  • Average pooling for temporal data.

Applies a 2D average pooling over an input Tensor which can be regarded as a composition of 2D input planes.

Typically the input is of shape

mindspore.nn - 图22
, AvgPool2d outputsregional average in the
mindspore.nn - 图23
-dimension. Given kernel size
mindspore.nn - 图24
and stride
mindspore.nn - 图25
, the operation is as follows.

mindspore.nn - 图26

Note

pad_mode for training only supports “same” and “valid”.

  • Parameters
    • kernel_size (int) – Size of the window to take a max over.

    • stride (int) – Stride size of the window. Default: None.

    • pad_mode (str) –

Select the mode of the pad. The optional values are“same”, “valid”. Default: “valid”.

  1. -

same: Adopts the way of completion. Output height and width will be the same asthe input. Total number of padding will be calculated for horizontal and verticaldirection and evenly distributed to top and bottom, left and right if possible. Otherwise, thelast extra padding will be done from the bottom and the right side.

  1. -

valid: Adopts the way of discarding. The possibly largest height and width of output will be returnwithout padding. Extra pixels will be discarded.

  1. -

padding (int) – Now is not supported, implicit zero padding to be added on both sides. Default: 0.

  • Inputs:
    • input (Tensor) - Tensor of shape

mindspore.nn - 图27
.

  • Outputs:
  • Tensor of shape

mindspore.nn - 图28
.

Examples

  1. Copy>>> pool = AvgPool2d(kernel_size=3, stride=1)
  2. >>> x = Tensor(np.random.randint(0, 10, [1, 2, 4, 4]), mindspore.float32)
  3. [[[[5. 5. 9. 9.]
  4. [8. 4. 3. 0.]
  5. [2. 7. 1. 2.]
  6. [1. 8. 3. 3.]]
  7. [[6. 8. 2. 4.]
  8. [3. 0. 2. 1.]
  9. [0. 8. 9. 7.]
  10. [2. 1. 4. 9.]]]]
  11. >>> output = pool(x)
  12. >>> output.shape()
  13. (1, 2, 2, 2)
  14. >>> output
  15. [[[[4.888889 4.4444447]
  16. [4.111111 3.4444444]]
  17. [[4.2222223 4.5555553]
  18. [3.2222223 4.5555553]]]]
  • class mindspore.nn.BatchNorm1d(num_features, eps=1e-05, momentum=0.9, affine=True, gamma_init='ones', beta_init='zeros', moving_mean_init='zeros', moving_var_init='ones', use_batch_statistics=True)[source]
  • Batch normalization layer over a 2D input.

Batch Normalization is widely used in convolutional networks. This layerapplies Batch Normalization over a 2D input (a mini-batch of 1D inputs) toreduce internal covariate shift as described in the paperBatch Normalization: Accelerating Deep Network Training byReducing Internal Covariate Shift. Itrescales and recenters the feature using a mini-batch of data andthe learned parameters which can be described in the following formula.

mindspore.nn - 图29

  • Parameters
    • num_features (int) – C from an expected input of size (N, C).

    • eps (float) – A value added to the denominator for numerical stability. Default: 1e-5.

    • momentum (float) – A floating hyperparameter of the momentum for therunning_mean and running_var computation. Default: 0.9.

    • gamma_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma weight.The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’,‘he_uniform’, etc. Default: ‘ones’.

    • beta_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta weight.The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’,‘he_uniform’, etc. Default: ‘zeros’.

    • moving_mean_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving mean.The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’,‘he_uniform’, etc. Default: ‘zeros’.

    • moving_var_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving variance.The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’,‘he_uniform’, etc. Default: ‘ones’.

    • use_batch_statistics (bool) – If true, use the mean value and variance value of current batch data, else usethe mean value and variance value of specified value. Default: True.

  • Inputs:

    • input (Tensor) - Tensor of shape

mindspore.nn - 图30
.

  • Outputs:
  • Tensor, the normalized, scaled, offset tensor, of shape

mindspore.nn - 图31
.

Examples

  1. Copy>>> net = nn.BatchNorm1d(num_features=16)
  2. >>> input = Tensor(np.random.randint(0, 255, [3, 16]), mindspore.float32)
  3. >>> net(input)
  • class mindspore.nn.BatchNorm2d(num_features, eps=1e-05, momentum=0.9, affine=True, gamma_init='ones', beta_init='zeros', moving_mean_init='zeros', moving_var_init='ones', use_batch_statistics=True)[source]
  • Batch normalization layer over a 4D input.

Batch Normalization is widely used in convolutional networks. This layerapplies Batch Normalization over a 4D input (a mini-batch of 2D inputs withadditional channel dimension) to avoid internal covariate shift as describedin the paper Batch Normalization: Accelerating Deep Network Training byReducing Internal Covariate Shift. Itrescales and recenters the feature using a mini-batch of data andthe learned parameters which can be described in the following formula.

mindspore.nn - 图32

  • Parameters
    • num_features (int) – C from an expected input of size (N, C, H, W).

    • eps (float) – A value added to the denominator for numerical stability. Default: 1e-5.

    • momentum (float) – A floating hyperparameter of the momentum for therunning_mean and running_var computation. Default: 0.9.

    • gamma_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma weight.The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’,‘he_uniform’, etc. Default: ‘ones’.

    • beta_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta weight.The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’,‘he_uniform’, etc. Default: ‘zeros’.

    • moving_mean_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving mean.The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’,‘he_uniform’, etc. Default: ‘zeros’.

    • moving_var_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the moving variance.The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’,‘he_uniform’, etc. Default: ‘ones’.

    • use_batch_statistics (bool) – If true, use the mean value and variance value of current batch data, else usethe mean value and variance value of specified value. Default: True.

  • Inputs:

    • input (Tensor) - Tensor of shape

mindspore.nn - 图33
.

  • Outputs:
  • Tensor, the normalized, scaled, offset tensor, of shape

mindspore.nn - 图34
.

Examples

  1. Copy>>> net = nn.BatchNorm2d(num_features=3)
  2. >>> input = Tensor(np.random.randint(0, 255, [1, 3, 224, 224]), mindspore.float32)
  3. >>> net(input)
  • class mindspore.nn.Cell(auto_prefix=True)[source]
  • Base class for all neural network.

A ‘Cell’ could be a single neural network cell, such as conv2d, relu, batch_norm, etc. or a composition ofcells to constructing a network.

Note

In general, the autograd algorithm will automatically generate the implementation of the gradient function,but if bprop method is implemented, the gradient functionwill be replaced by the bprop. The bprop implementation will receive a Tensor dout containing the gradientof the loss w.r.t. the output, and a Tensor out containing the forward result. The bprop need to compute thegradient of the loss w.r.t. the inputs, gradient of the loss w.r.t. Parameter variables is not supportedcurrently.

  • Parameters
  • auto_prefix (bool) – Recursively generate namespaces. Default: True.

Examples

  1. Copy>>> class MyCell(Cell):
  2. >>> def __init__(self):
  3. >>> super(MyCell, self).__init__()
  4. >>> self.relu = P.ReLU()
  5. >>>
  6. >>> def construct(self, x):
  7. >>> return self.relu(x)
  • cells()[source]
  • Returns an iterator over immediate cells.

  • cellsand_names(_cells=None, name_prefix='')[source]

  • Returns an iterator over all cells in the network.

Includes the cell’s name and itself.

  1. - Parameters
  2. -
  3. -

cells (str) – Cells to iterate over. Default: None.

  1. -

name_prefix (str) – Namespace. Default: ‘’.

Examples

  1. Copy>>> n = Net()
  2. >>> names = []
  3. >>> for m in n.cells_and_names():
  4. >>> if m[0]:
  5. >>> names.append(m[0])
  • compileand_run(*inputs_)[source]
  • Compiles and runs cell.

    • Parameters
    • inputs (tuple) – Input parameters.

    • Returns

    • Object, the result of executing.
  • construct(*inputs)[source]

  • Defines the computation to be performed.

This method should be overridden by all subclasses.

Note

The inputs of the top cell only allow Tensor.Other types (tuple, list, int etc.) are forbidden.

  1. - Returns
  2. -

Tensor, returns the computed result.

  • exec_checkpoint_graph()[source]
  • Executes saving checkpoint graph operation.

  • extend_repr()[source]

  • Sets the extended representation of the Cell.

To print customized extended information, re-implement this method in your own cells.

  • generate_scope()[source]
  • Generate the scope for every cell object in the network.

  • get_func_graph_proto()[source]

  • Return graph binary proto.

  • getparameters(_expand=True)[source]

  • Returns an iterator over cell parameters.

Yields parameters of this cell. If expand is True, yield parameters of this cell and all subcells.

  1. - Parameters
  2. -

expand (bool) – If True, yields parameters of this cell and all subcells. Otherwise, yields only parametersthat are direct members of this cell. Default: True.

Examples

  1. Copy>>> net = Net()
  2. >>> for item in net.get_parameters():
  3. >>> print(item)
  • get_scope()[source]
  • Returns the scope of a cell object in one network.

  • insertchild_to_cell(_child_name, child)[source]

  • Adds a child cell to the current cell.

Inserts a subcell with given name to current cell.

  1. - Parameters
  2. -
  3. -

child_name (str) – Name of the child cell.

  1. -

child (Cell) – The child cell to be inserted.

  1. - Raises
  2. -
  3. -

KeyError – Child Cell’s name is incorrect or duplicated with the other child name.

  1. -

TypeError – Child Cell’s type is incorrect.

  • insertparam_to_cell(_param_name, param, check_name=True)[source]
  • Adds a parameter to the current cell.

Inserts a parameter with given name to the cell. Please refer to the usage insource code of mindspore.nn.Cell.setattr.

  1. - Parameters
  2. -
  3. -

param_name (str) – Name of the parameter.

  1. -

param (Parameter) – Parameter to be inserted to the cell.

  1. -

check_name (bool) – Determines whether the name input is compatible. Default: True.

  1. - Raises
  2. -
  3. -

KeyError – If the name of parameter is null or contains dot.

  1. -

AttributeError – If user did not call init() first.

  1. -

TypeError – If the type of parameter is not Parameter.

  • loadparameter_slice(_params)[source]
  • Replace parameters with sliced tensors by parallel strategies.

Please refer to the usage in source code of mindspore.common._Executor.compile.

  1. - Parameters
  2. -

params (dict) – The parameters dictionary used for init data graph.

  • name_cells()[source]
  • Returns an iterator over all cells in the network.

Include name of the cell and cell itself.

  • parametersand_names(_name_prefix='', expand=True)[source]
  • Returns an iterator over cell parameters.

Includes the parameter’s name and itself.

  1. - Parameters
  2. -
  3. -

name_prefix (str) – Namespace. Default: ‘’.

  1. -

expand (bool) – If True, yields parameters of this cell and all subcells. Otherwise, yields only parametersthat are direct members of this cell. Default: True.

Examples

  1. Copy>>> n = Net()
  2. >>> names = []
  3. >>> for m in n.parameters_and_names():
  4. >>> if m[0]:
  5. >>> names.append(m[0])
  • parametersdict(_recurse=True)[source]
  • Gets parameters dictionary.

Gets the parameters dictionary of this cell.

  1. - Parameters
  2. -

recurse (bool) – Whether contains the parameters of subcells. Default: True.

  1. - Returns
  2. -

OrderedDict, return parameters dictionary.

  • setbroadcast_flag(_mode=True)[source]
  • Set the cell to data_parallel mode.

The cell can be accessed as an attribute using the given name.

  1. - Parameters
  2. -

mode (bool) – Specifies whether the model is data_parallel. Default: True.

  • settrain(_mode=True)[source]
  • Sets the cell to training mode.

The cell itself and all children cells will be set to training mode.

  1. - Parameters
  2. -

mode (bool) – Specifies whether the model is training. Default: True.

  • tofloat(_dst_type)[source]
  • Add cast on all inputs of cell and child cells to run with certain float type.

If dst_type is mindspore.dtype.float16, all the inputs of Cell including input, Parameter, Tensoras const will be cast to float16. Please refer to the usage in source code ofmindspore.train.amp.build_train_network.

Note

Call multiple times will overwrite the previous.

  1. - Parameters
  2. -

dst_type (mindspore.dtype) – Transfer Cell to Run with dsttype.dst_type can be _mindspore.dtype.float16 or mindspore.dtype.float32.

  1. - Raises
  2. -

ValueError – If dst_type is not float32 or float16.

  • trainableparams(_recurse=True)[source]
  • Returns all trainable parameters.

Returns a list of all trainable parmeters.

  1. - Parameters
  2. -

recurse (bool) – Whether contains the trainable parameters of subcells. Default: True.

  1. - Returns
  2. -

List, the list of trainable parameters.

  • untrainableparams(_recurse=True)[source]
  • Returns all untrainable parameters.

Returns a list of all untrainable parmeters.

  1. - Parameters
  2. -

recurse (bool) – Whether contains the untrainable parameters of subcells. Default: True.

  1. - Returns
  2. -

List, the list of untrainable parameters.

  • updateparameters_name(_prefix='', recurse=True)[source]
  • Updates the names of parameters with given prefix string.

Adds the given prefix to the names of parameters.

  1. - Parameters
  2. -
  3. -

prefix (str) – The prefix string.

  1. -

recurse (bool) – Whether contains the parameters of subcells. Default: True.

  • class mindspore.nn.CellList(*args)[source]
  • Holds Cells in a list.

CellList can be indexed like a regular Python list, but cells itcontains are properly registered, and will be visible by all Cell methods.

  • Parameters
  • args (list, __optional) – List of subclass of Cell.

Examples

  1. Copy>>> conv = nn.Conv2d(100, 20, 3)
  2. >>> bn = nn.BatchNorm2d(20)
  3. >>> relu = nn.ReLU()
  4. >>> cell_ls = nn.CellList([bn])
  5. >>> cell_ls.insert(0, conv)
  6. >>> cell_ls.append(relu)
  7. >>> x = Tensor(np.random.random((1, 3, 4, 4)), dtype=mindspore.float32)
  8. >>> # not same as nn.SequentialCell, `cell_ls(x)` is not correct
  9. >>> cell_ls
  10. CellList< (0): Conv2d<input_channels=100, ..., bias_init=None>
  11. (1): BatchNorm2d<num_features=20, ..., moving_variance=Parameter (name=variance)>
  12. (2): ReLU<> >
  • append(cell)[source]
  • Appends a given cell to the end of the list.

  • extend(cells)[source]

  • Appends cells from a Python iterable to the end of the list.

    • Raises
    • TypeError – If the cells is not a list of subcells.
  • insert(index, cell)[source]

  • Inserts a given cell before a given index in the list.
  • class mindspore.nn.ClipByNorm[source]
  • Clips tensor values to a maximum

mindspore.nn - 图35
-norm.

The output of this layer remains the same if the

mindspore.nn - 图36
-norm of the input tensoris not greater than the argument clip_norm. Otherwise the tensor will be normalized as:

mindspore.nn - 图37

where

mindspore.nn - 图38
is the
mindspore.nn - 图39
-norm of
mindspore.nn - 图40
.

  • Inputs:
    • input (Tensor) - Tensor of shape N-D.

    • clip_norm (Tensor) - A scalar Tensor of shape

mindspore.nn - 图41
or
mindspore.nn - 图42
and ofthe same type as the input Tensor.

  • Outputs:
  • Tensor, clipped tensor with the same shape as the input.

Examples

  1. Copy>>> net = nn.ClipByNorm()
  2. >>> input = Tensor(np.random.randint(0, 10, [4, 16]), mindspore.float32)
  3. >>> clip_norm = Tensor(np.array([100]).astype(np.float32))
  4. >>> net(input, clip_norm)
  • class mindspore.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, weight_init='normal', bias_init='zeros')[source]
  • 2D convolution layer.

Applies a 2D convolution over an input tensor which is typically of shape

mindspore.nn - 图43
,where
mindspore.nn - 图44
is batch size and
mindspore.nn - 图45
is channel number. For each batch of shape
mindspore.nn - 图46
, the formula is defined as:

mindspore.nn - 图47

where

mindspore.nn - 图48
is cross correlation operator,
mindspore.nn - 图49
is the input channel number,
mindspore.nn - 图50
rangesfrom
mindspore.nn - 图51
to
mindspore.nn - 图52
,
mindspore.nn - 图53
corresponds to
mindspore.nn - 图54
-th channel of the
mindspore.nn - 图55
-thfilter and
mindspore.nn - 图56
corresponds to the
mindspore.nn - 图57
-th channel of the output.
mindspore.nn - 图58
is a sliceof kernel and it has shape
mindspore.nn - 图59
, where
mindspore.nn - 图60
and
mindspore.nn - 图61
are height and width of the convolution kernel. The full kernel has shape
mindspore.nn - 图62
, where group is the group numberto split the input in the channel dimension.

If the ‘pad_mode’ is set to be “valid”, the output height and width will be

mindspore.nn - 图63
and
mindspore.nn - 图64
respectively.

The first introduction can be found in paper Gradient Based Learning Applied to Document Recognition.

  • Parameters
    • in_channels (int) – The number of input channel

mindspore.nn - 图65
.

  1. -

out_channels (int) – The number of output channel

mindspore.nn - 图66
.

  1. -

kernel_size (Union__[int, tuple]) – The data type is int or tuple with 2 integers. Specifies the heightand width of the 2D convolution window. Single int means the value if for both height and width ofthe kernel. A tuple of 2 ints means the first value is for the height and the other is for thewidth of the kernel.

  1. -

stride (int) – Specifies stride for all spatial dimensions with the same value. Value of stride should begreater or equal to 1 but bounded by the height and width of the input. Default: 1.

  1. -

pad_mode (str) –

Specifies padding mode. The optional values are“same”, “valid”, “pad”. Default: “same”.

  1. -

same: Adopts the way of completion. Output height and width will be the same as the input.Total number of padding will be calculated for horizontal and verticaldirection and evenly distributed to top and bottom, left and right if possible. Otherwise, thelast extra padding will be done from the bottom and the right side. If this mode is set, _padding_must be 0.

  1. -

valid: Adopts the way of discarding. The possibly largest height and width of output will be returnwithout padding. Extra pixels will be discarded. If this mode is set, _padding_must be 0.

  1. -

pad: Implicit paddings on both sides of the input. The number of padding will be padded to the inputTensor borders. padding should be greater than or equal to 0.

  1. -

padding (int) – Implicit paddings on both sides of the input. Default: 0.

  1. -

dilation (int) – Specifying the dilation rate to use for dilated convolution. If set to be

mindspore.nn - 图67
,there will be
mindspore.nn - 图68
pixels skipped for each sampling location. Its value should be greateror equal to 1 and bounded by the height and width of the input. Default: 1.

  1. -

group (int) – Split filter into groups, in channels and _out_channels should bedivisible by the number of groups. Default: 1.

  1. -

has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  1. -

weight_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel.It can be a Tensor, a string, an Initializer or a numbers.Number. When a string is specified,values from ‘TruncatedNormal’, ‘Normal’, ‘Uniform’, ‘HeUniform’ and ‘XavierUniform’ distributions as wellas constant ‘One’ and ‘Zero’ distributions are possible. Alias ‘xavier_uniform’, ‘he_uniform’, ‘ones’and ‘zeros’ are acceptable. Uppercase and lowercase are both acceptable. Refer to the values ofInitializer for more details. Default: ‘normal’.

  1. -

bias_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. PossibleInitializer and string are the same as ‘weight_init’. Refer to the values ofInitializer for more details. Default: ‘zeros’.

  • Inputs:
    • input (Tensor) - Tensor of shape

mindspore.nn - 图69
.

  • Outputs:
  • Tensor of shape

mindspore.nn - 图70
.

Examples

  1. Copy>>> net = nn.Conv2d(120, 240, 4, has_bias=False, weight_init='normal')
  2. >>> input = Tensor(np.ones([1, 120, 1024, 640]), mindspore.float32)
  3. >>> net(input).shape()
  4. (1, 240, 1024, 637)
  • class mindspore.nn.Conv2dTranspose(in_channels, out_channels, kernel_size, stride=1, pad_mode='same', padding=0, dilation=1, group=1, has_bias=False, weight_init='normal', bias_init='zeros')[source]
  • 2D transposed convolution layer.

Compute a 2D transposed convolution, which is also know as a deconvolution(although it is not actual deconvolution).

Input is typically of shape

mindspore.nn - 图71
, where
mindspore.nn - 图72
is batch size and
mindspore.nn - 图73
is channel number.

  • Parameters
    • in_channels (int) – The number of channels in the input space.

    • out_channels (int) – The number of channels in the output space.

    • kernel_size (Union__[int, tuple]) – int or tuple with 2 integers, which specifies the heightand width of the 2D convolution window. Single int means the value is for both height and width ofthe kernel. A tuple of 2 ints means the first value is for the height and the other is for thewidth of the kernel.

    • stride (int) – Specifies the same value for all spatial dimensions. Default: 1.

    • pad_mode (str) –

Select the mode of the pad. The optional values are“pad”, “same”, “valid”. Default: “same”.

  1. -

pad: Implicit paddings on both sides of the input.

  1. -

same: Adopted the way of completion.

  1. -

valid: Adopted the way of discarding.

  1. -

padding (int) – Implicit paddings on both sides of the input. Default: 0.

  1. -

dilation (int) – Specifies the dilation rate to use for dilatedconvolution. Default: 1.

  1. -

group (int) – Split filter into groups, in_channels and out_channels should bedivisible by the number of groups. Default: 1.

  1. -

has_bias (bool) – Specifies whether the layer uses a bias vector. Default: False.

  1. -

weight_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the convolution kernel.It can be a Tensor, a string, an Initializer or a numbers.Number. When a string is specified,values from ‘TruncatedNormal’, ‘Normal’, ‘Uniform’, ‘HeUniform’ and ‘XavierUniform’ distributions as wellas constant ‘One’ and ‘Zero’ distributions are possible. Alias ‘xavier_uniform’, ‘he_uniform’, ‘ones’and ‘zeros’ are acceptable. Uppercase and lowercase are both acceptable. Refer to the values ofInitializer for more details. Default: ‘normal’.

  1. -

bias_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the bias vector. PossibleInitializer and string are the same as ‘weight_init’. Refer to the values ofInitializer for more details. Default: ‘zeros’.

  • Inputs:
    • input (Tensor) - Tensor of shape

mindspore.nn - 图74
.

  • Outputs:
  • Tensor of shape

mindspore.nn - 图75
.

Examples

  1. Copy>>> net = nn.Conv2dTranspose(3, 64, 4, has_bias=False, weight_init='normal')
  2. >>> input = Tensor(np.ones([1, 3, 16, 50]), mindspore.float32)
  3. >>> net(input)
  • class mindspore.nn.DataWrapper(network, dataset_types, dataset_shapes, queue_name)[source]
  • Network training package class for dataset.

DataWrapper wraps the input network with a dataset which automatically fetches data with ‘GetNext’function from the dataset channel ‘queue_name’ and does forward computation in the construct function.

  • Parameters
    • network (Cell) – The training network for dataset.

    • dataset_types (list) – The type of dataset. The list contains describes the types of the inputs.

    • dataset_shapes (list) – The shapes of dataset. The list contains multiple sublists that describesthe shape of the inputs.

    • queue_name (str) – The identification of dataset channel which specifies the dataset channel to supplydata for the network.

  • Outputs:

  • Tensor, network output whose shape depends on the network.

Examples

  1. Copy>>> # call create_dataset function to create a regular dataset, refer to mindspore.dataset
  2. >>> train_dataset = create_dataset()
  3. >>> dataset_helper = mindspore.DatasetHelper(train_dataset)
  4. >>> net = Net()
  5. >>> net = DataWrapper(net, *(dataset_helper.types_shapes()), train_dataset.queue_name)
  • class mindspore.nn.Dense(in_channels, out_channels, weight_init='normal', bias_init='zeros', has_bias=True, activation=None)[source]
  • The fully connected layer.

Applies dense-connected layer for the input. This layer implements the operation as:

mindspore.nn - 图76

where

mindspore.nn - 图77
is the activation function passed as the activationargument (if passed in),
mindspore.nn - 图78
is a weight matrix with the samedata type as the inputs created by the layer, and
mindspore.nn - 图79
is a bias vectorwith the same data type as the inputs created by the layer (only if has_bias is True).

  • Parameters
    • in_channels (int) – The number of channels in the input space.

    • out_channels (int) – The number of channels in the output space.

    • weight_init (Union__[Tensor, str, Initializer, numbers.Number]) – The trainable weightinit parameter. The dtypeis same as input x. The values of str refer to the function _initializer. Default: ‘normal’.

    • bias_init (Union__[Tensor, str, Initializer, numbers.Number]) – The trainable biasinit parameter. The dtype issame as input x. The values of str refer to the function _initializer. Default: ‘zeros’.

    • has_bias (bool) – Specifies whether the layer uses a bias vector. Default: True.

    • activation (str) – Regularizer function applied to the output of the layer, eg. ‘relu’. Default: None.

  • Raises

  • ValueError – If weight_init or bias_init shape is incorrect.

  • Inputs:

    • input (Tensor) - Tensor of shape

mindspore.nn - 图80
.

  • Outputs:
  • Tensor of shape

mindspore.nn - 图81
.

Examples

  1. Copy>>> net = nn.Dense(3, 4)
  2. >>> input = Tensor(np.random.randint(0, 255, [2, 3]), mindspore.float32)
  3. >>> net(input)
  4. [[ 2.5246444 2.2738023 0.5711005 -3.9399147 ]
  5. [ 1.0739875 4.0155234 0.94188046 -5.459526 ]]
  • class mindspore.nn.DistributedGradReducer(parameters, mean=True, degree=None)[source]
  • A distributed optimizer.

Constructs a gradient reducer Cell, which applies communication and average operations onsingle-process gradient values.

  • Parameters
    • parameters (list) – the parameters to be updated.

    • mean (bool) – When mean is true, the mean coefficient (degree) would apply on gradients. Default: False.

    • degree (int) – The mean coefficient. Usually it equals to device number. Default: None.

  • Raises

  • ValueError – If degree is not a int or less than 0.

Examples

  1. Copy>>> from mindspore.communication import init, get_group_size
  2. >>> from mindspore.ops import composite as C
  3. >>> from mindspore.ops import operations as P
  4. >>> from mindspore.ops import functional as F
  5. >>> from mindspore import context
  6. >>> from mindspore import nn
  7. >>> from mindspore import ParallelMode, ParameterTuple
  8. >>>
  9. >>> device_id = int(os.environ["DEVICE_ID"])
  10. >>> context.set_context(mode=context.GRAPH_MODE, device_target="Davinci", save_graphs=True,
  11. >>> device_id=int(devie_id), enable_hccl=True)
  12. >>> init()
  13. >>> context.reset_auto_parallel_context()
  14. >>> context.set_auto_parallel_context(parallel_mode=ParallelMode.DATA_PARALLEL)
  15. >>>
  16. >>>
  17. >>>
  18. >>>
  19. >>> class TrainingWrapper(nn.Cell):
  20. >>> def __init__(self, network, optimizer, sens=1.0):
  21. >>> super(TrainingWrapper, self).__init__(auto_prefix=False)
  22. >>> self.network = network
  23. >>> self.network.add_flags(defer_inline=True)
  24. >>> self.weights = ParameterTuple(network.trainable_params())
  25. >>> self.optimizer = optimizer
  26. >>> self.grad = C.GradOperation('grad', get_by_list=True, sens_param=True)
  27. >>> self.sens = sens
  28. >>> self.reducer_flag = False
  29. >>> self.grad_reducer = None
  30. >>> self.parallel_mode = context.get_auto_parallel_context("parallel_mode")
  31. >>> if self.parallel_mode in [ParallelMode.DATA_PARALLEL,
  32. >>> ParallelMode.HYBRID_PARALLEL]:
  33. >>> self.reducer_flag = True
  34. >>> if self.reducer_flag:
  35. >>> mean = context.get_auto_parallel_context("mirror_mean")
  36. >>> if mean.get_device_num_is_set():
  37. >>> degree = context.get_auto_parallel_context("device_num")
  38. >>> else:
  39. >>> degree = get_group_size()
  40. >>> self.grad_reducer = nn.DistributedGradReducer(optimizer.parameters, mean, degree)
  41. >>>
  42. >>> def construct(self, *args):
  43. >>> weights = self.weights
  44. >>> loss = self.network(*args)
  45. >>> sens = P.Fill()(P.DType()(loss), P.Shape()(loss), self.sens)
  46. >>> grads = self.grad(self.network, weights)(*args, sens)
  47. >>> if self.reducer_flag:
  48. >>> # apply grad reducer on grads
  49. >>> grads = self.grad_reducer(grads)
  50. >>> return F.depend(loss, self.optimizer(grads))
  51. >>>
  52. >>> network = Net()
  53. >>> optimizer = nn.Momentum(network.trainable_params(), learning_rate=0.1, momentum=0.9)
  54. >>> train_cell = TrainingWrapper(network, optimizer)
  55. >>> inputs = Tensor(np.ones([16, 16]).astype(np.float32))
  56. >>> label = Tensor(np.zeros([16, 16]).astype(np.float32))
  57. >>> grads = train_cell(inputs, label)
  • class mindspore.nn.Dropout(keep_prob=0.5, seed0=0, seed1=0, dtype=mindspore.float32)[source]
  • Dropout layer for the input.

Randomly set some elements of the input tensor to zero with probability

mindspore.nn - 图82
during trainingusing samples from a Bernoulli distribution.

Note

Each channel will be zeroed out independently on every construct call.

The outputs are scaled by a factor of

mindspore.nn - 图83
during training sothat the output layer remains at a similar scale. During inference, thislayer returns the same tensor as the input.

This technique is proposed in paper Dropout: A Simple Way to Prevent Neural Networks from Overfitting and proved to be effective to reduceover-fitting and prevents neurons from co-adaptation. See more details in Improving neural networks bypreventing co-adaptation of feature detectors.

  • Parameters
    • keep_prob (float) – The keep rate, greater than 0 and less equal than 1. E.g. rate=0.9,dropping out 10% of input units. Default: 0.5.

    • seed0 (int) – The first random seed. Default: 0.

    • seed1 (int) – The second random seed. Default: 0.

    • dtype (mindspore.dtype) – Data type of input. Default: mindspore.float32.

  • Raises

  • ValueError – If keep_prob is not in range (0, 1).

  • Inputs:

    • input (Tensor) - An N-D Tensor.
  • Outputs:

  • Tensor, output tensor with the same shape as the input.

Examples

  1. Copy>>> x = Tensor(np.ones([20, 16, 50]), mindspore.float32)
  2. >>> net = nn.Dropout(keep_prob=0.8)
  3. >>> net(x)
  • class mindspore.nn.DynamicLossScaleUpdateCell(loss_scale_value, scale_factor, scale_window)[source]
  • Dynamic Loss scale update cell.

For loss scaling training, the initial loss scaling value will be set to be loss_scale_value.In every training step, the loss scaling value will be updated by loss scaling value/scale_factor_when there is overflow. And it will be increased by loss scaling value * _scale_factor if there is nooverflow for a continuous scale_window steps. This cell is used for Graph mode training in which alllogic will be executed on device side(Another training mode is feed mode in which some logic will beexecuted on host).

  • Parameters
    • loss_scale_value (float) – Init loss scale.

    • scale_factor (int) – Coefficient of increase and decrease.

    • scale_window (int) – Maximum continuous training steps that do not have overflow.

  • Inputs:

    • inputs (Tensor) - Tensor of shape

mindspore.nn - 图84
.

  1. -

label (Tensor) - Tensor of shape

mindspore.nn - 图85
.

  • Outputs:
  • Tensor, a scalar Tensor with shape

mindspore.nn - 图86
.

Examples

  1. Copy>>> net_with_loss = Net()
  2. >>> optimizer = nn.Momentum(net_with_loss.trainable_params(), learning_rate=0.1, momentum=0.9)
  3. >>> manager = nn.DynamicLossScaleUpdateCell(loss_scale_value=2**12, scale_factor=2, scale_window=1000)
  4. >>> train_network = nn.TrainOneStepWithLossScaleCell(net_with_loss, optimizer, scale_update_cell=manager)
  5. >>> train_network.set_train()
  6. >>>
  7. >>> inputs = Tensor(np.ones([16, 16]).astype(np.float32))
  8. >>> label = Tensor(np.zeros([16, 16]).astype(np.float32))
  9. >>> output = train_network(inputs, label)
  • class mindspore.nn.Embedding(vocab_size, embedding_size, use_one_hot=False, embedding_table='normal', dtype=mindspore.float32)[source]
  • A simple lookup table that stores embeddings of a fixed dictionary and size.

This module is often used to store word embeddings and retrieve them usingindices. The input to the module is a list of indices, and the output isthe corresponding word embeddings.

Note

When ‘use_one_hot’ is set to True, the input should be of type mindspore.int32.

  • Parameters
    • vocab_size (int) – Size of the dictionary of embeddings.

    • embedding_size (int) – The size of each embedding vector.

    • use_one_hot (bool) – Specifies whether to apply one_hot encoding form. Default: False.

    • embedding_table (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the embeddingtable.Refer to class _initializer for the values of string when a stringis specified. Default: ‘normal’.

    • dtype (mindspore.dtype) – Data type of input. Default: mindspore.float32.

  • Inputs:

    • input (Tensor) - Tensor of shape

mindspore.nn - 图87
.

  • Outputs:
  • Tensor of shape

mindspore.nn - 图88
.

Examples

  1. Copy>>> net = nn.Embedding(20000, 768, True)
  2. >>> input_data = Tensor(np.ones([8, 128]), mindspore.int32)
  3. >>>
  4. >>> # Maps the input word IDs to word embedding.
  5. >>> output = net(input_data)
  6. >>> output.shape()
  7. (8, 128, 768)
  • class mindspore.nn.EvaluationBase(eval_type)[source]
  • Base class of evaluation.

Note

Please refer to the definition of class Accuracy.

  • Parameters
  • eval_type (str) – Type of evaluation must be in {‘classification’, ‘multilabel’}.

  • Raises

  • TypeError – If the input type is not classification or multilabel.

  • clear()[source]

  • A interface describes the behavior of clearing the internal evaluation result.

Note

All subclasses should override this interface.

  • eval()[source]
  • A interface describes the behavior of computing the evaluation result.

Note

All subclasses should override this interface.

  • update(*inputs)[source]
  • A interface describes the behavior of updating the internal evaluation result.

Note

All subclasses should override this interface.

  1. - Parameters
  2. -

inputs – The first item is predicted array and the second item is target array.

  • class mindspore.nn.F1[source]
  • Calculates the F1 score. F1 is a special case of Fbeta when beta is 1.Refer to class Fbeta for more details.

mindspore.nn - 图89

Examples

  1. Copy>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  2. >>> y = Tensor(np.array([1, 0, 1]))
  3. >>> metric = nn.F1()
  4. >>> metric.update(x, y)
  5. >>> fbeta = metric.eval()
  • class mindspore.nn.Fbeta(beta)[source]
  • Calculates the fbeta score.

Fbeta score is a weighted mean of precison and recall.

mindspore.nn - 图90

  • Parameters
  • beta (float) – The weight of precision.

Examples

  1. Copy>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  2. >>> y = Tensor(np.array([1, 0, 1]))
  3. >>> metric = nn.Fbeta(1)
  4. >>> metric.update(x, y)
  5. >>> fbeta = metric.eval()
  6. [0.66666667 0.66666667]
  • clear()[source]
  • Clears the internal evaluation result.

  • eval(average=False)[source]

  • Computes the fbeta.

    • Parameters
    • average (bool) – Whether to calculate the average fbeta. Default value is False.

    • Returns

    • Float, computed result.
  • update(*inputs)[source]

  • Updates the internal evaluation result y_pred and y.

    • Parameters
    • inputs – Input y_pred and y. y_pred and y are Tensor, list or numpy.ndarray.y_pred is in most cases (not strictly) a list of floating numbers in range

mindspore.nn - 图91
and the shape is
mindspore.nn - 图92
, where
mindspore.nn - 图93
is the number of cases and
mindspore.nn - 图94
is the number of categories. y contains values of integers. The shape is
mindspore.nn - 图95
if one-hot encoding is used. Shape can also be
mindspore.nn - 图96
if category index is used.

  • class mindspore.nn.FixedLossScaleUpdateCell(loss_scale_value)[source]
  • Static scale update cell, the loss scaling value will not be updated.

For usage please refer to DynamicLossScaleUpdateCell.

  • Parameters
  • loss_scale_value (float) – Init loss scale.

Examples

  1. Copy>>> net_with_loss = Net()
  2. >>> optimizer = nn.Momentum(net_with_loss.trainable_params(), learning_rate=0.1, momentum=0.9)
  3. >>> manager = nn.FixedLossScaleUpdateCell(loss_scale_value=2**12)
  4. >>> train_network = nn.TrainOneStepWithLossScaleCell(net_with_loss, optimizer, scale_update_cell=manager)
  5. >>> train_network.set_train()
  6. >>>
  7. >>> inputs = Tensor(np.ones([16, 16]).astype(np.float32))
  8. >>> label = Tensor(np.zeros([16, 16]).astype(np.float32))
  9. >>> output = train_network(inputs, label)
  • class mindspore.nn.Flatten[source]
  • Flatten layer for the input.

Flattens a tensor without changing dimension of batch size on the 0-th axis.

  • Inputs:
    • input (Tensor) - Tensor of shape

mindspore.nn - 图97
to be flattened.

  • Outputs:
  • Tensor, the shape of the output tensor is

mindspore.nn - 图98
, where
mindspore.nn - 图99
isthe product of the remaining dimensions.

Examples

  1. Copy>>> net = nn.Flatten()
  2. >>> input = Tensor(np.array([[[1.2, 1.2], [2.1, 2.1]], [[2.2, 2.2], [3.2, 3.2]]]), mindspore.float32)
  3. >>> input.shape()
  4. (2, 2, 2)
  5. >>> net(input)
  6. [[1.2 1.2 2.1 2.1]
  7. [2.2 2.2 3.2 3.2]]
  • class mindspore.nn.GELU[source]
  • Gaussian error linear unit activation function.

Applies GELU function to each element of the input. The input is a Tensor with any valid shape.

GELU is defined as:

mindspore.nn - 图100
, where
mindspore.nn - 图101
is the cumulative distribution functionof standard Gaussian distribution and
mindspore.nn - 图102
is the element of the input.

  • Inputs:
    • input_data (Tensor) - The input of Tanh.
  • Outputs:

  • Tensor, with the same type and shape as the input_data.
  • class mindspore.nn.GetNextSingleOp(dataset_types, dataset_shapes, queue_name)[source]
  • Cell to run get next operation.

    • Parameters
      • dataset_types (list[mindspore.dtype]) – The types of dataset.

      • dataset_shapes (list[tuple[int]__]) – The shapes of dataset.

      • queue_name (str) – Queue name to fetch the data.

Detailed information, please refer to ops.operations.GetNext.

  • class mindspore.nn.L1Loss(reduction='mean')[source]
  • L1Loss creates a criterion to measure the mean absolute error (MAE) between

mindspore.nn - 图103
and
mindspore.nn - 图104
by element,where
mindspore.nn - 图105
is the input Tensor and
mindspore.nn - 图106
is the target Tensor.

For simplicity, let

mindspore.nn - 图107
and
mindspore.nn - 图108
be 1-dimensional Tensor with length
mindspore.nn - 图109
,the unreduced loss (i.e. with argument reduction set to ‘none’) of
mindspore.nn - 图110
and
mindspore.nn - 图111
is given as:

mindspore.nn - 图112

When argument reduction is ‘mean’, the mean value of

mindspore.nn - 图113
will be returned.When argument reduction is ‘sum’, the sum of
mindspore.nn - 图114
will be returned.
mindspore.nn - 图115
is the batch size.

  • Parameters
  • reduction (str) – Type of reduction to apply to loss. The optional values are “mean”, “sum”, “none”.Default: “mean”.

  • Inputs:

    • input_data (Tensor) - Tensor of shape

mindspore.nn - 图116
.

  1. -

target_data (Tensor) - Tensor of shape

mindspore.nn - 图117
.

  • Outputs:
  • Tensor, loss float tensor.

Examples

  1. Copy>>> loss = nn.L1Loss()
  2. >>> input_data = Tensor(np.array([1, 2, 3]), mindspore.float32)
  3. >>> target_data = Tensor(np.array([1, 2, 2]), mindspore.float32)
  4. >>> loss(input_data, target_data)
  • class mindspore.nn.LARS(optimizer, epsilon=1e-05, hyperpara=0.001, weight_decay=0.0, use_clip=False, decay_filter=>, lars_filter=>, loss_scale=1.0)[source]
  • Implements the LARS algorithm with LARSUpdate Operator.

LARS is an optimization algorithm employing a large batch optimization technique. Refer to paper LARGE BATCHTRAINING OF CONVOLUTIONAL NETWORKS.

  • Parameters
    • optimizer (Optimizer) – MindSpore optimizer for which to wrap and modify gradients.

    • epsilon (float) – Term added to the denominator to improve numerical stability. Default: 1e-05.

    • hyperpara (float) – Trust coefficient for calculating the local learning rate. Default: 0.001.

    • weight_decay (float) – Weight decay (L2 penalty). Default: 0.0.

    • use_clip (bool) – Whether to use clip operation for calculating the local learning rate. Default: False.

    • decay_filter (Function) – A function to determine whether apply weight decay on parameters. Default:lambda x: ‘LayerNorm’ not in x.name and ‘bias’ not in x.name.

    • lars_filter (Function) – A function to determine whether apply lars algorithm. Default:lambda x: ‘LayerNorm’ not in x.name and ‘bias’ not in x.name.

    • loss_scale (float) – A floating point value for the loss scale. Default: 1.0.

  • Inputs:

    • gradients (tuple[Tensor]) - The gradients of params in optimizer, the shape isas same as the params in optimizer.
  • Outputs:

  • Union[Tensor[bool], tuple[Parameter]], it depends on the output of optimizer.

Examples

  1. Copy>>> net = Net()
  2. >>> loss = nn.SoftmaxCrossEntropyWithLogits()
  3. >>> opt = nn.Momentum(net.trainable_params(), 0.1, 0.9)
  4. >>> opt_lars = nn.LARS(opt, epsilon=1e-08, hyperpara=0.02)
  5. >>> model = Model(net, loss_fn=loss, optimizer=opt_lars, metrics=None)

  • class mindspore.nn.LSTM(input_size, hidden_size, num_layers=1, has_bias=True, batch_first=False, dropout=0, bidirectional=False)[source]
  • LSTM (Long Short-Term Memory) layer.

Applies a LSTM to the input.

There are two pipelines connecting two consecutive cells in a LSTM model; one is cell state pipelineand another is hidden state pipeline. Denote two consecutive time nodes as

mindspore.nn - 图118
and
mindspore.nn - 图119
.Given an input
mindspore.nn - 图120
at time
mindspore.nn - 图121
, an hidden state
mindspore.nn - 图122
and an cellstate
mindspore.nn - 图123
of the layer at time
mindspore.nn - 图124
, the cell state and hidden state attime
mindspore.nn - 图125
is computed using an gating mechanism. Input gate
mindspore.nn - 图126
is designed to protect the cellfrom perturbation by irrelevant inputs. Forget gate
mindspore.nn - 图127
affords protection of the cell by forgettingsome information in the past, which is stored in
mindspore.nn - 图128
. Output gate
mindspore.nn - 图129
protects otherunits from perturbation by currently irrelevant memory contents. Candidate cell state
mindspore.nn - 图130
iscalculated with the current input, on which the input gate will be applied. Finally, current cell state
mindspore.nn - 图131
and hidden state
mindspore.nn - 图132
are computed with the calculated gates and cell states. The completeformulation is as follows.

mindspore.nn - 图133

Here

mindspore.nn - 图134
is the sigmoid function, and
mindspore.nn - 图135
is the Hadamard product.
mindspore.nn - 图136
are learnable weights between the output and the input in the formula. For instance,
mindspore.nn - 图137
are the weight and bias used to transform from input
mindspore.nn - 图138
to
mindspore.nn - 图139
.Details can be found in paper LONG SHORT-TERM MEMORY andLong Short-Term Memory Recurrent Neural Network Architectures for Large Scale Acoustic Modeling.

  • Parameters
    • input_size (int) – Number of features of input.

    • hidden_size (int) – Number of features of hidden layer.

    • num_layers (int) – Number of layers of stacked LSTM . Default: 1.

    • has_bias (bool) – Specifies whether has bias b_ih and b_hh. Default: True.

    • batch_first (bool) – Specifies whether the first dimension of input is batch_size. Default: False.

    • dropout (float) – If not 0, append Dropout layer on the outputs of eachLSTM layer except the last layer. Default 0. The range of dropout is [0.0, 1.0].

    • bidirectional (bool) – Specifies whether this is a bidirectional LSTM. If set True,number of directions will be 2 otherwise number of directions is 1. Default: False.

  • Inputs:

    • input (Tensor) - Tensor of shape (seqlen, batch_size, _input_size).

    • hx (tuple) - A tuple of two Tensors (h0, c_0) both of data type mindspore.float32 ormindspore.float16 and shape (num_directions * _num_layers, batchsize, _hidden_size).Data type of hx should be the same of input.

  • Outputs:

  • Tuple, a tuple constains (output, (h_n, c_n)).

    • output (Tensor) - Tensor of shape (seqlen, batch_size, num_directions * _hidden_size).

    • hx_n (tuple) - A tuple of two Tensor (hn, c_n) both of shape(num_directions * _num_layers, batchsize, _hidden_size).

Examples

  1. Copy>>> class LstmNet(nn.Cell):
  2. >>> def __init__(self, input_size, hidden_size, num_layers, has_bias, batch_first, bidirectional):
  3. >>> super(LstmNet, self).__init__()
  4. >>> self.lstm = nn.LSTM(input_size=input_size,
  5. >>> hidden_size=hidden_size,
  6. >>> num_layers=num_layers,
  7. >>> has_bias=has_bias,
  8. >>> batch_first=batch_first,
  9. >>> bidirectional=bidirectional,
  10. >>> dropout=0.0)
  11. >>>
  12. >>> def construct(self, inp, h0, c0):
  13. >>> return self.lstm(inp, (h0, c0))
  14. >>>
  15. >>> net = LstmNet(10, 12, 2, has_bias=True, batch_first=True, bidirectional=False)
  16. >>> input = Tensor(np.ones([3, 5, 10]).astype(np.float32))
  17. >>> h0 = Tensor(np.ones([1 * 2, 3, 12]).astype(np.float32))
  18. >>> c0 = Tensor(np.ones([1 * 2, 3, 12]).astype(np.float32))
  19. >>> output, (hn, cn) = net(input, h0, c0)
  • class mindspore.nn.Lamb(params, decay_steps, warmup_steps=0, start_learning_rate=0.1, end_learning_rate=0.0001, power=1.0, beta1=0.9, beta2=0.999, eps=1e-06, weight_decay=0.0, decay_filter=>)[source]
  • Lamb Dynamic LR.

LAMB is an optimization algorithm employing a layerwise adaptive large batchoptimization technique. Refer to the paper LARGE BATCH OPTIMIZATION FOR DEEP LEARNING: TRAINING BERT IN 76MINUTES.

  • Parameters
    • params (list[Parameter]) – A list of parameter, which will be updated. The element in _params_should be class mindspore.Parameter.

    • decay_steps (int) – The steps of the lr decay. Should be equal to or greater than 1.

    • warmup_steps (int) – The steps of lr warm up. Default: 0.

    • start_learning_rate (float) – A floating point value for the learning rate. Default: 0.1.

    • end_learning_rate (float) – A floating point value for the end learning rate. Default: 0.0001.

    • power (float) – The power of the polynomial. Default: 1.0.

    • beta1 (float) – The exponential decay rate for the 1st moment estimates. Default: 0.9.Should be in range (0.0, 1.0).

    • beta2 (float) – The exponential decay rate for the 2nd moment estimates. Default: 0.999.Should be in range (0.0, 1.0).

    • eps (float) – Term added to the denominator to improve numerical stability. Default: 1e-6.Should be greater than 0.

    • weight_decay (float) – Weight decay (L2 penalty). Default: 0.0. Should be equal to or greater than 0.

    • decay_filter (Function) – A function to determine whether to apply weight decay on parameters. Default:lambda x: ‘LayerNorm’ not in x.name and ‘bias’ not in x.name.

  • Inputs:

    • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.
  • Outputs:

  • tuple[Parameter], the updated velocity value, the shape is the same as params.

Examples

  1. Copy>>> net = Net()
  2. >>> loss = nn.SoftmaxCrossEntropyWithLogits()
  3. >>> optim = nn.Lamb(params=net.trainable_params(), decay_steps=10)
  4. >>> model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)

  • class mindspore.nn.LayerNorm(normalized_shape, begin_norm_axis=-1, begin_params_axis=-1, gamma_init='ones', beta_init='zeros')[source]
  • Applies Layer Normalization over a mini-batch of inputs.

Layer normalization is widely used in recurrent neural networks. It appliesnormalization over a mini-batch of inputs for each single training case as describedin the paper Layer Normalization. Unlike batchnormalization, layer normalization performs exactly the same computation at training andtesting times. It can be described using the following formula. It is applied across all channelsand pixel but only one batch size.

mindspore.nn - 图140

  • Parameters
    • normalized_shape (Union__(tuple[int]__, list[int]) – The normalization is performed over axesbegin_norm_axis … R - 1 and centering and scaling parameters are calculated overbegin_params_axis … R - 1.

    • begin_norm_axis (int) – It first normalization dimension: normalization will be performed along dimensionsbegin_norm_axis: rank(inputs), the value should be in [-1, rank(input)). Default: -1.

    • begin_params_axis (int) – The first parameter(beta, gamma)dimension: scale and centering parameterswill have dimensions begin_params_axis: rank(inputs) and will be broadcast withthe normalized inputs accordingly, the value should be in [-1, rank(input)). Default: -1.

    • gamma_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the gamma weight.The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’,‘he_uniform’, etc. Default: ‘ones’.

    • beta_init (Union__[Tensor, str, Initializer, numbers.Number]) – Initializer for the beta weight.The values of str refer to the function initializer including ‘zeros’, ‘ones’, ‘xavier_uniform’,‘he_uniform’, etc. Default: ‘zeros’.

  • Inputs:

    • input_x (Tensor) - The shape of ‘inputx’ is input_shape = (x1, x_2, …, x_R),and input_shape[begin_norm_axis:] is equal to normalized_shape.
  • Outputs:

  • Tensor, the normalized and scaled offset tensor, has the same shape and data type as the input_x.

Examples

  1. Copy>>> x = Tensor(np.ones([20, 5, 10, 10], np.float32))
  2. >>> shape1 = x.shape()[1:]
  3. >>> m = nn.LayerNorm(shape1, begin_norm_axis=1, begin_params_axis=1)
  4. >>> m(x)
  • extend_repr()[source]
  • Display instance object as string.
  • class mindspore.nn.LeakyReLU(alpha=0.2)[source]
  • Leaky ReLU activation function.

LeakyReLU is similar to ReLU, but LeakyReLU has a slope that makes it not equal to 0 at x < 0.The activation function is defined as:

mindspore.nn - 图141

See https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf

  • Parameters
  • alpha (float) – Slope of the activation function at x < 0. Default: 0.2.

  • Inputs:

    • input_x (Tensor) - The input of LeakyReLU.
  • Outputs:

  • Tensor, has the same type and shape with the input_x.
  • class mindspore.nn.LogSoftmax(axis=-1)[source]
  • LogSoftmax activation function.

Applies the LogSoftmax function to n-dimensional input tensor.

The input is transformed with Softmax function and then with log function to lie in range[-inf,0).

Logsoftmax is defined as:

mindspore.nn - 图142
,where
mindspore.nn - 图143
is the
mindspore.nn - 图144
-th slice along the given dim of the input Tensor.

  • Parameters
  • axis (int) – The axis to apply LogSoftmax operation, -1 means the last dimension. Default: -1.

  • Inputs:

    • x (Tensor) - The input of LogSoftmax.
  • Outputs:

  • Tensor, which has the same type and shape as the input as x with values in the range[-inf,0).
  • class mindspore.nn.Loss[source]
  • Calculates the average of the loss. If method ‘update’ is called every

mindspore.nn - 图145
iterations, the result ofevaluation will be:

mindspore.nn - 图146

Examples

  1. Copy>>> x = Tensor(np.array(0.2), mindspore.float32)
  2. >>> loss = nn.Loss()
  3. >>> loss.clear()
  4. >>> loss.update(x)
  5. >>> result = loss.eval()
  6. 0.20000000298023224
  • clear()[source]
  • Clears the internal evaluation result.

  • eval()[source]

  • Calculates the average of the loss.

    • Returns
    • Float, the average of the loss.

    • Raises

    • RuntimeError – If the total number is 0.
  • update(*inputs)[source]

  • Updates the internal evaluation result.

    • Parameters
    • inputs – Inputs contain only one element, the element is loss. The dimension ofloss should be 0 or 1.

    • Raises

      • ValueError – If the length of inputs is not 1.

      • ValueError – If the dimensions of loss is not 1.

  • class mindspore.nn.MAE[source]
  • Calculates the mean absolute error.

Creates a criterion that measures the mean absolute error (MAE)between each element in the input:

mindspore.nn - 图147
and the target:
mindspore.nn - 图148
.

mindspore.nn - 图149

Here

mindspore.nn - 图150
is the prediction and
mindspore.nn - 图151
is the true value.

Note

The method update must be called with the form update(y_pred, y).

Examples

  1. Copy>>> x = Tensor(np.array([0.1, 0.2, 0.6, 0.9]), mindspore.float32)
  2. >>> y = Tensor(np.array([0.1, 0.25, 0.7, 0.9]), mindspore.float32)
  3. >>> error = nn.MAE()
  4. >>> error.clear()
  5. >>> error.update(x, y)
  6. >>> result = error.eval()
  • clear()[source]
  • Clears the internal evaluation result.

  • eval()[source]

  • Computes the mean absolute error.

    • Returns
    • Float, the computed result.

    • Raises

    • RuntimeError – If the number of the total samples is 0.
  • update(*inputs)[source]

  • Updates the internal evaluation result

mindspore.nn - 图152
and
mindspore.nn - 图153
.

  1. - Parameters
  2. -

inputs – Input y_pred and y for calculating mean absolute error where the shape ofy_pred and y are both N-D and the shape are the same.

  1. - Raises
  2. -

ValueError – If the number of the input is not 2.

  • class mindspore.nn.MSE[source]
  • Measures the mean squared error.

Creates a criterion that measures the mean squared error (squared L2norm) between each element in the input:

mindspore.nn - 图154
and the target:
mindspore.nn - 图155
.

mindspore.nn - 图156

where

mindspore.nn - 图157
is batch size.

Examples

  1. Copy>>> x = Tensor(np.array([0.1, 0.2, 0.6, 0.9]), mindspore.float32)
  2. >>> y = Tensor(np.array([0.1, 0.25, 0.5, 0.9]), mindspore.float32)
  3. >>> error = nn.MSE()
  4. >>> error.clear()
  5. >>> error.update(x, y)
  6. >>> result = error.eval()
  • clear()[source]
  • Clear the internal evaluation result.

  • eval()[source]

  • Compute the mean squared error.

    • Returns
    • Float, the computed result.

    • Raises

    • RuntimeError – If the number of samples is 0.
  • update(*inputs)[source]

  • Updates the internal evaluation result

mindspore.nn - 图158
and
mindspore.nn - 图159
.

  1. - Parameters
  2. -

inputs – Input y_pred and y for calculating mean square error where the shape ofy_pred and y are both N-D and the shape are the same.

  1. - Raises
  2. -

ValueError – If the number of input is not 2.

  • class mindspore.nn.MSELoss(reduction='mean')[source]
  • MSELoss create a criterion to measures the mean squared error (squared L2-norm) between

mindspore.nn - 图160
and
mindspore.nn - 图161
by element, where
mindspore.nn - 图162
is the input and
mindspore.nn - 图163
is the target.

For simplicity, let

mindspore.nn - 图164
and
mindspore.nn - 图165
be 1-dimensional Tensor with length
mindspore.nn - 图166
,the unreduced loss (i.e. with argument reduction set to ‘none’) of
mindspore.nn - 图167
and
mindspore.nn - 图168
is given as:

mindspore.nn - 图169

When argument reduction is ‘mean’, the mean value of

mindspore.nn - 图170
will be returned.When argument reduction is ‘sum’, the sum of
mindspore.nn - 图171
will be returned.
mindspore.nn - 图172
is the batch size.

  • Parameters
  • reduction (str) – Type of reduction to apply to loss. The optional values are “mean”, “sum”, “none”.Default: “mean”.

  • Inputs:

    • input_data (Tensor) - Tensor of shape

mindspore.nn - 图173
.

  1. -

target_data (Tensor) - Tensor of shape

mindspore.nn - 图174
.

  • Outputs:
  • Tensor, weighted loss float tensor.

Examples

  1. Copy>>> loss = nn.MSELoss()
  2. >>> input_data = Tensor(np.array([1, 2, 3]), mindspore.float32)
  3. >>> target_data = Tensor(np.array([1, 2, 2]), mindspore.float32)
  4. >>> loss(input_data, target_data)
  • class mindspore.nn.MaxPool2d(kernel_size=1, stride=1, pad_mode='VALID', padding=0)[source]
  • Max pooling operation for temporal data.

Applies a 2D max pooling over an input Tensor which can be regarded as a composition of 2D planes.

Typically the input is of shape

mindspore.nn - 图175
, MaxPool2d outputsregional maximum in the
mindspore.nn - 图176
-dimension. Given kernel size
mindspore.nn - 图177
and stride
mindspore.nn - 图178
, the operation is as follows.

mindspore.nn - 图179

Note

pad_mode for training only supports “same” and “valid”.

  • Parameters
    • kernel_size (int) – Size of the window to take a max over.

    • stride (int) – Stride size of the window. Default: None.

    • pad_mode (str) –

Select the mode of the pad. The optional values are“same” and “valid”. Default: “valid”.

  1. -

same: Adopts the way of completion. Output height and width will be the same asthe input. Total number of padding will be calculated for horizontal and verticaldirection and evenly distributed to top and bottom, left and right if possible. Otherwise, thelast extra padding will be done from the bottom and the right side.

  1. -

valid: Adopts the way of discarding. The possibly largest height and width of output will be returnwithout padding. Extra pixels will be discarded.

  1. -

padding (int) – Now is not supported, mplicit zero padding to be added on both sides. Default: 0.

  • Inputs:
    • input (Tensor) - Tensor of shape

mindspore.nn - 图180
.

  • Outputs:
  • Tensor of shape

mindspore.nn - 图181
.

Examples

  1. Copy>>> pool = MaxPool2d(kernel_size=3, stride=1)
  2. >>> x = Tensor(np.random.randint(0, 10, [1, 2, 4, 4]), mindspore.float32)
  3. [[[[1. 5. 5. 1.]
  4. [0. 3. 4. 8.]
  5. [4. 2. 7. 6.]
  6. [4. 9. 0. 1.]]
  7. [[3. 6. 2. 6.]
  8. [4. 4. 7. 8.]
  9. [0. 0. 4. 0.]
  10. [1. 8. 7. 0.]]]]
  11. >>> output = pool(x)
  12. >>> output.shape()
  13. (1, 2, 2, 2)
  14. >>> output
  15. [[[[7. 8.]
  16. [9. 9.]]
  17. [[7. 8.]
  18. [8. 8.]]]]
  • class mindspore.nn.Metric[source]
  • Base class of metric.

Note

For examples of subclasses, please refer to the definition of class MAE, ‘Recall’ etc.

  • abstract clear()[source]
  • A interface describes the behavior of clearing the internal evaluation result.

Note

All subclasses should override this interface.

  • abstract eval()[source]
  • A interface describes the behavior of computing the evaluation result.

Note

All subclasses should override this interface.

  • abstract update(*inputs)[source]
  • A interface describes the behavior of updating the internal evaluation result.

Note

All subclasses should override this interface.

  1. - Parameters
  2. -

inputs – A variable-length input argument list.

  • class mindspore.nn.Momentum(params, learning_rate, momentum, weight_decay=0.0, loss_scale=1.0, decay_filter=>)[source]
  • Implements the Momentum algorithm.

Refer to the paper on the importance of initialization and momentum in deep learning for more details.

  • Parameters
    • params (list[Parameter]) – A list of parameter, which will be updated. The element in _parameters_should be class mindspore.Parameter.

    • learning_rate (Union__[float, Tensor, Iterable]) – A value for the learning rate. When the learning_rate isIterable or a Tensor and the dims of the Tensor is 1,use dynamic learning rate, then the i-th step willtake the i-th value as the learning rate.When the learning_rate is float or learning_rate is a Tensorbut the dims of the Tensor is 0, use fixed learning rate.Other cases are not supported.

    • momentum (float) – Hyperparameter of type float, means momentum for the moving average.

    • weight_decay (float) – Weight decay (L2 penalty). Default: 0.0.

    • loss_scale (float) – A floating point value for the loss scale. Default: 1.0.

    • decay_filter (Function) – A function to determine whether to apply weight decay on parameters. Default:lambda x: ‘beta’ not in x.name and ‘gamma’ not in x.name.

  • Inputs:

    • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.
  • Outputs:

  • Tensor[bool], the value is True.

  • Raises

  • ValueError – If the momentum is less than 0.0.

Examples

  1. Copy>>> net = Net()
  2. >>> loss = nn.SoftmaxCrossEntropyWithLogits()
  3. >>> optim = nn.Momentum(params=net.trainable_params(), learning_rate=0.1, momentum=0.9)
  4. >>> model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)

  • class mindspore.nn.Norm(axis=(), keep_dims=False)[source]
  • Computes the norm of vectors, currently including Euclidean norm, i.e.,

mindspore.nn - 图182
-norm.

  • Parameters
    • axis (tuple) – The axis over which to compute vector norms. Default: ().

    • keep_dims (bool) – If True, the axis indicated in axis are kept with size 1. Otherwise,the dimensions in axis are removed from the output shape. Default: False.

  • Inputs:

    • input (Tensor) - Tensor which is not empty.
  • Outputs:

  • Tensor, output tensor with dimensions in ‘axis’ reduced to 1 will be returned if ‘keep_dims’ is True;otherwise a Tensor with dimensions in ‘axis’ removed is returned.

Examples

  1. Copy>>> net = nn.Norm(axis=0)
  2. >>> input = Tensor(np.random.randint(0, 10, [4, 16]), mindspore.float32)
  3. >>> net(input)
  • class mindspore.nn.OneHot(axis=-1, depth=1, on_value=1.0, off_value=0.0, dtype=mindspore.float32)[source]
  • Returns a one-hot tensor.

The locations represented by indices in argument ‘indices’ take value on_value,while all other locations take value off_value.

Note

If the input indices is rank

mindspore.nn - 图183
, the output will have rank
mindspore.nn - 图184
. The newaxis is created at dimension axis.

  • Parameters
    • axis (int) – Features x depth if axis == -1, depth x featuresif axis == 0. Default: -1.

    • depth (int) – A scalar defining the depth of the one hot dimension. Default: 1.

    • on_value (float) – A scalar defining the value to fill in output[i][j]when indices[j] = i. Default: 1.0.

    • off_value (float) – A scalar defining the value to fill in output[i][j]when indices[j] != i. Default: 0.0.

    • dtype (mindspore.dtype) – Default: mindspore.float32.

  • Inputs:

    • indices (Tensor) - A tensor of indices of data type mindspore.int32 and arbitrary shape.
  • Outputs:

  • Tensor, the one-hot tensor of data type ‘dtype’ with dimension at ‘axis’ expanded to ‘depth’ and filled withon_value and off_value.

Examples

  1. Copy>>> net = nn.OneHot(depth=4, axis=1)
  2. >>> indices = Tensor([[1, 3], [0, 2]], dtype=mindspore.int32)
  3. >>> net(indices)
  4. [[[0. 0.]
  5. [1. 0.]
  6. [0. 0.]
  7. [0. 1.]]
  8. [[1. 0.]
  9. [0. 0.]
  10. [0. 1.]
  11. [0. 0.]]]
  • class mindspore.nn.Optimizer(learning_rate, parameters)[source]
  • Base class for all optimizers.

This class defines the API to add Ops to train a model.

Note

This class defines the API to add Ops to train a model. Never usethis class directly, but instead instantiate one of its subclasses.

  • Parameters
    • learning_rate (float) – A floating point value for the learning rate. Should be greater than 0.

    • parameters (list) – A list of parameter, which will be updated. The element in _parameters_should be class mindspore.Parameter.

  • Raises

    • ValueError – If the learning_rate is a Tensor, but the dims of tensor is greater than 1.

    • TypeError – If the learning_rate is not any of the three types: float, Tensor, Iterable.

  • class mindspore.nn.PReLU(channel=1, w=0.25)[source]
  • PReLU activation function.

Applies the PReLU function element-wise.

PReLU is defined as:

mindspore.nn - 图185
, where
mindspore.nn - 图186
is an element of an channel of the input.

Here

mindspore.nn - 图187
is an learnable parameter with default initial value 0.25.Parameter
mindspore.nn - 图188
has dimensionality of the argument channel. If called without argumentchannel, a single parameter
mindspore.nn - 图189
will be shared across all channels.

  • Parameters
    • channel (int) – The dimension of input. Default: 1.

    • w (float) – The initial value of w. Default: 0.25.

  • Inputs:

    • input_data (Tensor) - The input of Tanh.
  • Outputs:

  • Tensor, with the same type and shape as the input_data.
  • class mindspore.nn.ParameterUpdate(param)[source]
  • Cell that updates parameters.

With this Cell, one can manually update param with the input Tensor.

  • Parameters
  • param (Parameter) – The parameter to be updated manually.

  • Raises

  • KeyError – If parameter with the specified name do not exist.

Examples

  1. Copy>>> network = Net()
  2. >>> param = network.parameters_dict()['learning_rate']
  3. >>> update = nn.ParameterUpdate(param)
  4. >>> update.phase = "update_param"
  5. >>> lr = Tensor(0.001, mindspore.float32)
  6. >>> update(lr)
  • class mindspore.nn.Precision(eval_type='classification')[source]
  • Calculates precision for classification and multilabel data.

The precision function creates two local variables,

mindspore.nn - 图190
and
mindspore.nn - 图191
, that are used to compute the precision. This value isultimately returned as the precision, an idempotent operation that simply divides
mindspore.nn - 图192
by the sum of
mindspore.nn - 图193
and
mindspore.nn - 图194
.

mindspore.nn - 图195

Note

In the multi-label cases, the elements of

mindspore.nn - 图196
and
mindspore.nn - 图197
should be 0 or 1.

  • Parameters
  • eval_type (str) – Metric to calculate accuracy over a dataset, for classification ormultilabel. Default: ‘classification’.

Examples

  1. Copy>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  2. >>> y = Tensor(np.array([1, 0, 1]))
  3. >>> metric = nn.Precision('classification')
  4. >>> metric.clear()
  5. >>> metric.update(x, y)
  6. >>> precision = metric.eval()
  7. [0.5 1. ]
  • clear()[source]
  • Clears the internal evaluation result.

  • eval(average=False)[source]

  • Computes the precision.

    • Parameters
    • average (bool) – Specify whether calculate the average precision. Default value is False.

    • Returns

    • Float, the computed result.
  • update(*inputs)[source]

  • Updates the internal evaluation result with y_pred and y.

    • Parameters
    • inputs – Input y_pred and y. y_pred and y are Tensor, list or numpy.ndarray.y_pred is in most cases (not strictly) a list of floating numbers in range

mindspore.nn - 图198
and the shape is
mindspore.nn - 图199
, where
mindspore.nn - 图200
is the number of cases and
mindspore.nn - 图201
is the number of categories. For ‘multilabel’ evaluation type, y_pred can only be one-hotencoding with values 0 or 1. Indices with 1 indicate positive category. y contains valuesof integers. The shape is
mindspore.nn - 图202
if one-hot encoding is used. One-hot encodingshould be used when ‘eval_type’ is ‘multilabel’. Shape can also be
mindspore.nn - 图203
if categoryindex is used in ‘classification’ evaluation type.

  1. - Raises
  2. -

ValueError – If the number of input is not 2.

  • class mindspore.nn.ReLU[source]
  • Rectified Linear Unit activation function.

Applies the rectified linear unit function element-wise. It returnselement-wise

mindspore.nn - 图204
, specially, the neurons with the negative outputwill suppressed and the active neurons will stay the same.

  • Inputs:
    • input_data (Tensor) - The input of ReLU.
  • Outputs:

  • Tensor, with the same type and shape as the input_data.
  • class mindspore.nn.ReLU6[source]
  • Compute ReLU6 activation function.

ReLU6 is similar to ReLU with a upper limit of 6, which if the inputs are greater than 6, the outputswill be suppressed to 6.It computes element-wise as

mindspore.nn - 图205
. The input is a Tensor of any valid shape.

  • Inputs:
    • input_data (Tensor) - The input of ReLU6.
  • Outputs:

  • Tensor, which has the same type with input_data.
  • class mindspore.nn.Recall(eval_type='classification')[source]
  • Calculate recall for classification and multilabel data.

The recall class creates two local variables,

mindspore.nn - 图206
and
mindspore.nn - 图207
,that are used to compute the recall. This value is ultimately returned as the recall, an idempotent operationthat simply divides
mindspore.nn - 图208
by the sum of
mindspore.nn - 图209
and
mindspore.nn - 图210
.

mindspore.nn - 图211

Note

In the multi-label cases, the elements of

mindspore.nn - 图212
and
mindspore.nn - 图213
should be 0 or 1.

  • Parameters
  • eval_type (str) – Metric to calculate the recall over a dataset, for classification ormultilabel. Default: ‘classification’.

Examples

  1. Copy>>> x = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]))
  2. >>> y = Tensor(np.array([1, 0, 1]))
  3. >>> metric = nn.Recall('classification')
  4. >>> metric.clear()
  5. >>> metric.update(x, y)
  6. >>> recall = metric.eval()
  7. [1. 0.5]
  • clear()[source]
  • Clears the internal evaluation result.

  • eval(average=False)[source]

  • Computes the recall.

    • Parameters
    • average (bool) – Specify whether calculate the average recall. Default value is False.

    • Returns

    • Float, the computed result.
  • update(*inputs)[source]

  • Updates the internal evaluation result with y_pred and y.

    • Parameters
    • inputs – Input y_pred and y. y_pred and y are a Tensor, a list or an array.y_pred is in most cases (not strictly) a list of floating numbers in range

mindspore.nn - 图214
and the shape is
mindspore.nn - 图215
, where
mindspore.nn - 图216
is the number of cases and
mindspore.nn - 图217
is the number of categories. For ‘multilabel’ evaluation type, y_pred can only be one-hotencoding with values 0 or 1. Indices with 1 indicate positive category. y contains valuesof integers. The shape is
mindspore.nn - 图218
if one-hot encoding is used. One-hot encodingshould be used when ‘eval_type’ is ‘multilabel’. Shape can also be
mindspore.nn - 图219
if categoryindex is used in ‘classification’ evaluation type.

  1. - Raises
  2. -

ValueError – If the number of input is not 2.

  • class mindspore.nn.SGD(params, learning_rate=0.1, momentum=0.0, dampening=0.0, weight_decay=0.0, nesterov=False, loss_scale=1.0)[source]
  • Implements stochastic gradient descent (optionally with momentum).

Introduction to SGD can be found at https://en.wikipedia.org/wiki/Stochastic_gradient_descent.Nesterov momentum is based on the formula from paper On the importance of initialization andmomentum in deep learning.

  • Parameters
    • params (list[Parameter]) – A list of parameter, which will be updated. The element in _params_should be class mindspore.Parameter.

    • learning_rate (float) – A floating point value for the learning rate. Default: 0.1.

    • momentum (float) – A floating point value the momentum. Default: 0.

    • dampening (float) – A floating point value of dampening for momentum. Default: 0.

    • weight_decay (float) – Weight decay (L2 penalty). Default: 0.

    • nesterov (bool) – Enables the Nesterov momentum. Default: False.

    • loss_scale (float) – A floating point value for the loss scale. Default: 1.0.

  • Inputs:

    • gradients (tuple[Tensor]) - The gradients of params, the shape is the same as params.
  • Outputs:

  • Tensor[bool], the value is True.

  • Raises

  • ValueError – If the momentum, dampening or weight_decay value is less than 0.0.

Examples

  1. Copy>>> net = Net()
  2. >>> loss = nn.SoftmaxCrossEntropyWithLogits()
  3. >>> optim = nn.SGD(params=net.trainable_params())
  4. >>> model = Model(net, loss_fn=loss, optimizer=optim, metrics=None)
  • class mindspore.nn.SequentialCell(*args)[source]
  • Sequential cell container.

A list of Cells will be added to it in the order they are passed in the constructor.Alternatively, an ordered dict of cells can also be passed in.

  • Parameters
  • args (list, __optional) – List of subclass of Cell.

  • Raises

  • TypeError – If arg is not of type list or OrderedDict.

  • Inputs:

    • input (Tensor) - Tensor with shape according to the first Cell in the sequence.
  • Outputs:

  • Tensor, the output Tensor with shape depending on the input and defined sequence of Cells.

Examples

  1. Copy>>> conv = nn.Conv2d(3, 2, 3, pad_mode='valid')
  2. >>> bn = nn.BatchNorm2d(2)
  3. >>> relu = nn.ReLU()
  4. >>> seq = nn.SequentialCell([conv, bn, relu])
  5. >>>
  6. >>> x = Tensor(np.random.random((1, 3, 4, 4)), dtype=mindspore.float32)
  7. >>> seq(x)
  8. [[[[0.02531557 0. ]
  9. [0.04933941 0.04880078]]
  10. [[0. 0. ]
  11. [0. 0. ]]]]
  • class mindspore.nn.Sigmoid[source]
  • Sigmoid activation function.

Applies sigmoid-type activation element-wise.

Sigmoid function is defined as:

mindspore.nn - 图220
, where
mindspore.nn - 图221
is the element of the input.

  • Inputs:
    • input_data (Tensor) - The input of Tanh.
  • Outputs:

  • Tensor, with the same type and shape as the input_data.
  • class mindspore.nn.SmoothL1Loss(sigma=1.0)[source]
  • A loss class for learning region proposals.

SmoothL1Loss can be regarded as modified version of L1Loss or a combination of L1Loss and L2Loss.L1Loss computes the element-wise absolute difference between two input Tensor while L2Loss computes thesquared difference between two input Tensor. L2Loss often leads to faster convergence but it is lessrobust to outliers.

Given two input

mindspore.nn - 图222
of length
mindspore.nn - 图223
, the unreduced SmoothL1Loss can be describedas follows:

mindspore.nn - 图224

Here

mindspore.nn - 图225
controls the point where the loss function changes from quadratic to linear.Its default value is 1.0.
mindspore.nn - 图226
is the batch size. This function returns anunreduced loss Tensor.

  • Parameters
  • sigma (float) – A parameter used to control the point where the function will change fromquadratic to linear. Default: 1.0.

  • Inputs:

    • input_data (Tensor) - Tensor of shape

mindspore.nn - 图227
.

  1. -

target_data (Tensor) - Tensor of shape

mindspore.nn - 图228
.

  • Outputs:
  • Tensor, loss float tensor.

Examples

  1. Copy>>> loss = nn.SmoothL1Loss()
  2. >>> input_data = Tensor(np.array([1, 2, 3]), mindspore.float32)
  3. >>> target_data = Tensor(np.array([1, 2, 2]), mindspore.float32)
  4. >>> loss(input_data, target_data)
  • class mindspore.nn.Softmax(axis=-1)[source]
  • Softmax activation function.

Applies the Softmax function to an n-dimensional input Tensor.

The input is a Tensor of logits transformed with exponential function and thennormalized to lie in range [0, 1] and sum up to 1.

Softmax is defined as:

mindspore.nn - 图229

where

mindspore.nn - 图230
is the
mindspore.nn - 图231
-th slice along the given dim of the input Tensor.

  • Parameters
  • axis (Union__[int, tuple[int]__]) – The axis to apply Softmax operation, -1 means the last dimension. Default: -1.

  • Inputs:

    • x (Tensor) - The input of Softmax.
  • Outputs:

  • Tensor, which has the same type and shape as x with values in the range[0,1].
  • class mindspore.nn.SoftmaxCrossEntropyExpand(sparse=False)[source]
  • Computes softmax cross entropy between logits and labels. Implemented by expanded formula.

This is a wrapper of several functions.

mindspore.nn - 图232

where

mindspore.nn - 图233
is a 1D score Tensor,
mindspore.nn - 图234
is the target class.

Note

When argument sparse is set to True, the format of label is the indexrange from

mindspore.nn - 图235
to
mindspore.nn - 图236
instead of one-hot vectors.

  • Parameters
  • sparse (bool) – Specifies whether labels use sparse format or not. Default: False.

  • Inputs:

    • input_data (Tensor) - Tensor of shape

mindspore.nn - 图237
.

  1. -

label (Tensor) - Tensor of shape

mindspore.nn - 图238
.

  • Outputs:
  • Tensor, a scalar tensor including the mean loss.

Examples

  1. Copy>>> loss = nn.SoftmaxCrossEntropyExpand(sparse=True)
  2. >>> input_data = Tensor(np.ones([64, 512]), dtype=mindspore.float32)
  3. >>> label = Tensor(np.ones([64]), dtype=mindspore.int32)
  4. >>> loss(input_data, label)
  • class mindspore.nn.SoftmaxCrossEntropyWithLogits(is_grad=True, sparse=False, reduction=None)[source]
  • Computes softmax cross entropy between logits and labels.

Measures the distribution error between the probabilities of the input (computed with softmax function) and thetarget where the classes are mutually exclusive (only one class is positive) using cross entropy loss.

Typical input into this function is unnormalized scores and target of each class.Scores Tensor

mindspore.nn - 图239
is of shape
mindspore.nn - 图240
and target Tensor
mindspore.nn - 图241
is aTensor of shape
mindspore.nn - 图242
which contains one-hot labels of length
mindspore.nn - 图243
.

For each batch

mindspore.nn - 图244
, the loss is given as:

mindspore.nn - 图245

where

mindspore.nn - 图246
is a 1D score Tensor,
mindspore.nn - 图247
is the target class and
mindspore.nn - 图248
is a weight Tensor to generate weighted loss for each class. When not specified,weight Tensor is set to be None and weight is the same (
mindspore.nn - 图249
) for all class.

Note

While the target classes are mutually exclusive, i.e., only one class is positive in the target, the predictedprobabilities need not be exclusive. All that is required is that the predicted probability distributionof entry is a valid one.

  • Parameters
    • is_grad (bool) – Specifies whether calculate grad only. Default: True.

    • sparse (bool) – Specifies whether labels use sparse format or not. Default: False.

    • reduction (Union__[str, None]) – Type of reduction to apply to loss. Support ‘sum’ or ‘mean’ If None,do not reduction. Default: None.

  • Inputs:

    • logits (Tensor) - Tensor of shape

mindspore.nn - 图250
.

  1. -

labels (Tensor) - Tensor of shape

mindspore.nn - 图251
. If sparse is True, The type oflabels is mstype.int32. If sparse is False, the type of labels is same as the type of logits.

  • Outputs:
  • Tensor, a tensor of the same shape as logits with the component-wiselogistic losses.

Examples

  1. Copy>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
  2. >>> logits = Tensor(np.random.randint(0, 9, [1, 10]), mindspore.float32)
  3. >>> labels_np = np.zeros([1, 10]).astype(np.float32)
  4. >>> labels_np[0][0] = 1
  5. >>> labels = Tensor(labels_np)
  6. >>> loss(logits, labels)
  • class mindspore.nn.Tanh[source]
  • Tanh activation function.

Applies the Tanh function element-wise, returns a new tensor with the hyperbolic tangent of the elements of input,The input is a Tensor with any valid shape.

Tanh function is defined as:

mindspore.nn - 图252

where

mindspore.nn - 图253
is an element of the input Tensor.

  • Inputs:
    • input_data (Tensor) - The input of Tanh.
  • Outputs:

  • Tensor, with the same type and shape as the input_data.
  • class mindspore.nn.Top1CategoricalAccuracy[source]
  • Calculates the top-1 categorical accuracy. This class is a specialized class for TopKCategoricalAccuracy.Refer to class ‘TopKCategoricalAccuracy’ for more details.

Examples

  1. Copy>>> x = Tensor(np.array([[0.2, 0.5, 0.3, 0.6, 0.2], [0.1, 0.35, 0.5, 0.2, 0.],
  2. >>> [0.9, 0.6, 0.2, 0.01, 0.3]]), mindspore.float32)
  3. >>> y = Tensor(np.array([2, 0, 1]), mindspore.float32)
  4. >>> topk = nn.Top1CategoricalAccuracy()
  5. >>> topk.clear()
  6. >>> topk.update(x, y)
  7. >>> result = topk.eval()
  • class mindspore.nn.Top5CategoricalAccuracy[source]
  • Calculates the top-5 categorical accuracy. This class is a specialized class for TopKCategoricalAccuracy.Refer to class ‘TopKCategoricalAccuracy’ for more details.

Examples

  1. Copy>>> x = Tensor(np.array([[0.2, 0.5, 0.3, 0.6, 0.2], [0.1, 0.35, 0.5, 0.2, 0.],
  2. >>> [0.9, 0.6, 0.2, 0.01, 0.3]]), mindspore.float32)
  3. >>> y = Tensor(np.array([2, 0, 1]), mindspore.float32)
  4. >>> topk = nn.Top5CategoricalAccuracy()
  5. >>> topk.clear()
  6. >>> topk.update(x, y)
  7. >>> result = topk.eval()
  • class mindspore.nn.TopKCategoricalAccuracy(k)[source]
  • Calculates the top-k categorical accuracy.

Note

The method update must receive input of the form

mindspore.nn - 图254
. If some samples havethe same accuracy, the first sample will be chosen.

  • Parameters
  • k (int) – Specifies the top-k categorical accuracy to compute.

  • Raises

Examples

  1. Copy>>> x = Tensor(np.array([[0.2, 0.5, 0.3, 0.6, 0.2], [0.1, 0.35, 0.5, 0.2, 0.],
  2. >>> [0.9, 0.6, 0.2, 0.01, 0.3]]), mindspore.float32)
  3. >>> y = Tensor(np.array([2, 0, 1]), mindspore.float32)
  4. >>> topk = nn.TopKCategoricalAccuracy(3)
  5. >>> topk.clear()
  6. >>> topk.update(x, y)
  7. >>> result = topk.eval()
  8. 0.6666666666666666
  • clear()[source]
  • Clear the internal evaluation result.

  • eval()[source]

  • Computes the top-k categorical accuracy.

    • Returns
    • Float, computed result.
  • update(*inputs)[source]

  • Updates the internal evaluation result y_pred and y.

    • Parameters
    • inputs – Input y_pred and y. y_pred and y are Tensor, list or numpy.ndarray.y_pred is in most cases (not strictly) a list of floating numbers in range

mindspore.nn - 图255
and the shape is
mindspore.nn - 图256
, where
mindspore.nn - 图257
is the number of cases and
mindspore.nn - 图258
is the number of categories. y contains values of integers. The shape is
mindspore.nn - 图259
if one-hot encoding is used. Shape can also be
mindspore.nn - 图260
if category index is used.

  • class mindspore.nn.TrainOneStepCell(network, optimizer, sens=1.0)[source]
  • Network training package class.

Wraps the network with an optimizer. The resulting Cell be trained with input data and label.Backward graph will be created in the construct function to do parameter updating. Differentparallel modes are available to run the training.

  • Parameters
    • network (Cell) – The training network.

    • optimizer (Cell) – Optimizer for updating the weights.

    • sens (Number) – The scaling number to be filled as the input of backpropagation. Default value is 1.0.

  • Inputs:

    • data (Tensor) - Tensor of shape

mindspore.nn - 图261
.

  1. -

label (Tensor) - Tensor of shape

mindspore.nn - 图262
.

  • Outputs:
  • Tensor, a scalar Tensor with shape

mindspore.nn - 图263
.

Examples

  1. Copy>>> net = Net()
  2. >>> loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  3. >>> optim = nn.Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  4. >>> loss_net = nn.WithLossCell(net, loss_fn)
  5. >>> train_net = nn.TrainOneStepCell(loss_net, optim)
  • class mindspore.nn.TrainOneStepWithLossScaleCell(network, optimizer, scale_update_cell=None)[source]
  • Network training with loss scaling.

This is a training step with loss scaling. It takes a network, an optimizer and possibly a scale updateCell as args. The loss scale value can be updated in both host side or device side. TheTrainOneStepWithLossScaleCell will be compiled to be graph which takes data, label, sens as inputdata. The sens is acting as loss scaling value. If you want to update it on host side, the value shouldbe provided. If sens is not given, the loss scale update logic should be provied by scale_update_cell.If scale_update_cell is not None and sens is provided, the scale_update_cell will be ignored.

  • Parameters
    • network (Cell) – The training network.

    • optimizer (Cell) – Optimizer for updating the weights.

    • scale_update_cell (Cell) – The loss scaling update logic cell. Default: None.

  • Inputs:

    • inputs (Tensor) - Tensor of shape

mindspore.nn - 图264
.

  1. -

label (Tensor) - Tensor of shape

mindspore.nn - 图265
.

  1. -

scaling_sens (Tensor) - Tensor of shape

mindspore.nn - 图266
.

  • Outputs:
  • Tuple of 3 Tensor, the loss, overflow flag and current loss scaling value.

    • loss (Tensor) - Tensor with shape

mindspore.nn - 图267
.

  1. -

overflow (Tensor) - Tensor with shape

mindspore.nn - 图268
, type is bool.

  1. -

loss_scale (Tensor) - Tensor with shape

mindspore.nn - 图269
.

Examples

  1. Copy>>> net_with_loss = Net()
  2. >>> optimizer = nn.Momentum(net_with_loss.trainable_params(), learning_rate=0.1, momentum=0.9)
  3. >>> manager = nn.DynamicLossScaleUpdateCell(init_loss_scale=2**12, scale_factor=2, scale_window=1000)
  4. >>> train_network = nn.TrainOneStepWithLossScaleCell(net_with_loss, optimizer, scale_update_cell=manager)
  5. >>> train_network.set_train()
  6. >>>
  7. >>> inputs = Tensor(np.ones([16, 16]).astype(np.float32))
  8. >>> label = Tensor(np.zeros([16, 16]).astype(np.float32))
  9. >>> scaling_sens = Tensor(np.full((1), np.finfo(np.float32).max), dtype=mindspore.float32)
  10. >>> output = train_network(inputs, label, scaling_sens)
  • class mindspore.nn.WithEvalCell(network, loss_fn)[source]
  • Cell that returns loss, output and label for evaluation.

This Cell accepts a network and loss function as arguments and computes loss for model.It returns loss, output and label to calculate the metrics.

  • Parameters
    • network (Cell) – The network Cell.

    • loss_fn (Cell) – The loss Cell.

  • Inputs:

    • data (Tensor) - Tensor of shape

mindspore.nn - 图270
.

  1. -

label (Tensor) - Tensor of shape

mindspore.nn - 图271
.

  • Outputs:
  • Tuple, containing a scalar loss Tensor, a network output Tensor of shape

mindspore.nn - 图272
and a label Tensor of shape
mindspore.nn - 图273
.

Examples

  1. Copy>>> # For a defined network Net without loss function
  2. >>> net = Net()
  3. >>> loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  4. >>> eval_net = nn.WithEvalCell(net, loss_fn)
  • class mindspore.nn.WithGradCell(network, loss_fn=None, sens=None)[source]
  • Cell that returns the gradients.

Wraps the network with backward cell to compute gradients. A network with a loss function is necessaryas argument. If loss function in None, the network must be a wrapper of network and loss function. ThisCell accepts data and label as inputs and returns gradients for each trainable parameter.

Note

Run in PyNative mode.

  • Parameters
    • network (Cell) – The target network to wrap.

    • loss_fn (Cell) – Primitive loss function used to compute gradients. Default: None.

    • sens (Union__[None, Tensor, Scalar, Tuple …]) – The sensitive for backpropagation, the type and shapeshould be same as the network output. If None, we will fill one to a same type shape ofoutput value. Default: None.

  • Inputs:

    • data (Tensor) - Tensor of shape

mindspore.nn - 图274
.

  1. -

label (Tensor) - Tensor of shape

mindspore.nn - 图275
.

  • Outputs:
  • list, a list of Tensors with identical shapes as trainable weights.

Examples

  1. Copy>>> # For a defined network Net without loss function
  2. >>> net = Net()
  3. >>> loss_fn = nn.SoftmaxCrossEntropyWithLogits()
  4. >>> grad_net = nn.WithGradCell(net, loss_fn)
  5. >>>
  6. >>> # For a network wrapped with loss function
  7. >>> net = Net()
  8. >>> net_with_criterion = nn.WithLossCell(net, loss_fn)
  9. >>> grad_net = nn.WithGradCell(net_with_criterion)
  • class mindspore.nn.WithLossCell(backbone, loss_fn)[source]
  • Cell with loss function.

Wraps the network with loss function. This Cell accepts data and label as inputs andthe computed loss will be returned.

  • Parameters
    • backbone (Cell) – The target network to wrap.

    • loss_fn (Cell) – The loss function used to compute loss.

  • Inputs:

    • data (Tensor) - Tensor of shape

mindspore.nn - 图276
.

  1. -

label (Tensor) - Tensor of shape

mindspore.nn - 图277
.

  • Outputs:
  • Tensor, a scalar tensor with shape

mindspore.nn - 图278
.

Examples

  1. Copy>>> net = Net()
  2. >>> loss_fn = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
  3. >>> net_with_criterion = nn.WithLossCell(net, loss_fn)
  4. >>>
  5. >>> batch_size = 2
  6. >>> data = Tensor(np.ones([batch_size, 3, 64, 64]).astype(np.float32) * 0.01)
  7. >>> label = Tensor(np.ones([batch_size, 1, 1, 1]).astype(np.int32))
  8. >>>
  9. >>> net_with_criterion(data, label)
  • property backbone_network
  • Returns the backbone network.

    • Returns
    • Cell, the backbone network.
  • mindspore.nn.getactivation(_name)[source]
  • Gets the activation function.

    • Parameters
    • name (str) – The name of the activation function.

    • Returns

    • Function, the activation function.

Examples

  1. Copy>>> sigmoid = nn.get_activation('sigmoid')
  • mindspore.nn.getmetric_fn(_name, *args, **kwargs)[source]
  • Gets the metric method base on the input name.

    • Parameters
      • name (str) – The name of metric method. Refer to the ‘factory’object for the currently supported metrics.

      • args – Arguments for the metric function.

      • kwargs – Keyword arguments for the metric function.

    • Returns

    • Metric object, class instance of the metric method.

Examples

  1. Copy>>> metric = get_metric_fn('precision', eval_type='classification')
  • mindspore.nn.names()[source]
  • Get the names of the metric methods.

    • Returns
    • List, the name list of metric methods.