tinyms.losses¶
Losses module. Loss function in machine learning is the target of the model. It shows how well the model works on a dataset and the optimization target which the optimizer is searching.
-
tinyms.losses.
net_with_loss
(net)[source]¶ This function is provided for AI beginners who are not familiar with which loss should be chosen for the network to be trained. Instead of choosing different loss function, users could directly get the best suitable loss function by specifying network.
- Parameters
net (layers.Layer) – The instance of network to be trained.
- Raises
TypeError – When network type is not supported.
Note
Currently this function only supports few networks, if the network type is not supported, the system would raise TypeError exception.
Examples
>>> from tinyms.model import ssd300 >>> from tinyms.losses import net_with_loss >>> >>> net = ssd300() >>> net_loss = net_with_loss(net)
-
class
tinyms.losses.
SSD300WithLoss
(network)[source]¶ Provide SSD300 training loss through network.
- Parameters
network (layers.Layer) – The training network.
- Returns
Tensor, the loss of the network.
Examples
>>> from tinyms.model import ssd300 >>> from tinyms.losses import SSD300WithLoss >>> >>> net = SSD300WithLoss(ssd300())
-
class
tinyms.losses.
CrossEntropyWithLabelSmooth
(smooth_factor=0.0, num_classes=1000)[source]¶ CrossEntropyWith LabelSmooth.
- Parameters
- Returns
None.
Examples
>>> CrossEntropyWithLabelSmooth(smooth_factor=0., num_classes=1000)
-
class
tinyms.losses.
CycleGANGeneratorLoss
(generator, D_A, D_B)[source]¶ Cycle GAN generator loss.
- Parameters
generator (layers.Layer) – Generator of CycleGAN.
D_A (layers.Layer) – The discriminator network of domain A to domain B.
D_B (layers.Layer) – The discriminator network of domain B to domain A.
- Outputs:
Tuple Tensor, the losses of generator.
-
class
tinyms.losses.
CycleGANDiscriminatorLoss
(D_A, D_B, reduction='none')[source]¶ Cycle GAN discriminator loss.
- Parameters
D_A (layers.Layer) – The discriminator network of domain A to domain B.
D_B (layers.Layer) – The discriminator network of domain B to domain A.
reduction (str) – The discriminator network of reduction. Default: none.
- Outputs:
the loss of discriminator.
-
class
tinyms.losses.
L1Loss
(reduction='mean')[source]¶ L1Loss creates a criterion to measure the mean absolute error (MAE) between \(x\) and \(y\) element-wise, where \(x\) is the input Tensor and \(y\) is the target Tensor.
For simplicity, let \(x\) and \(y\) be 1-dimensional Tensor with length \(N\), the unreduced loss (i.e. with argument reduction set to ‘none’) of \(x\) and \(y\) is given as:
\[L(x, y) = \{l_1,\dots,l_N\}, \quad \text{with } l_n = \left| x_n - y_n \right|\]When argument reduction is ‘mean’, the mean value of \(L(x, y)\) will be returned. When argument reduction is ‘sum’, the sum of \(L(x, y)\) will be returned. \(N\) is the batch size.
- Parameters
reduction (str) – Type of reduction to be applied to loss. The optional values are “mean”, “sum”, and “none”. Default: “mean”.
- Inputs:
input_data (Tensor) - Tensor of shape \((x_1, x_2, ..., x_R)\).
target_data (Tensor) - Tensor of shape \((y_1, y_2, ..., y_S)\).
- Outputs:
Tensor, loss float tensor.
- Raises
ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> loss = nn.L1Loss() >>> input_data = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> target_data = Tensor(np.array([1, 2, 2]), mindspore.float32) >>> output = loss(input_data, target_data) >>> print(output) 0.33333334
-
class
tinyms.losses.
MSELoss
(reduction='mean')[source]¶ MSELoss creates a criterion to measure the mean squared error (squared L2-norm) between \(x\) and \(y\) element-wise, where \(x\) is the input and \(y\) is the target.
For simplicity, let \(x\) and \(y\) be 1-dimensional Tensor with length \(N\), the unreduced loss (i.e. with argument reduction set to ‘none’) of \(x\) and \(y\) is given as:
\[L(x, y) = \{l_1,\dots,l_N\}, \quad \text{with} \quad l_n = (x_n - y_n)^2.\]When argument reduction is ‘mean’, the mean value of \(L(x, y)\) will be returned. When argument reduction is ‘sum’, the sum of \(L(x, y)\) will be returned. \(N\) is the batch size.
- Parameters
reduction (str) – Type of reduction to be applied to loss. The optional values are “mean”, “sum”, and “none”. Default: “mean”.
- Inputs:
input_data (Tensor) - Tensor of shape \((x_1, x_2, ..., x_R)\).
target_data (Tensor) - Tensor of shape \((y_1, y_2, ..., y_S)\).
- Outputs:
Tensor, weighted loss float tensor.
- Raises
ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> loss = nn.MSELoss() >>> input_data = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> target_data = Tensor(np.array([1, 2, 2]), mindspore.float32) >>> output = loss(input_data, target_data) >>> print(output) 0.33333334
-
class
tinyms.losses.
SmoothL1Loss
(beta=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 the squared difference between two input Tensor. L2Loss often leads to faster convergence but it is less robust to outliers.
Given two input \(x,\ y\) of length \(N\), the unreduced SmoothL1Loss can be described as follows:
\[\begin{split}L_{i} = \begin{cases} \frac{0.5 (x_i - y_i)^{2}}{\text{beta}}, & \text{if } |x_i - y_i| < \text{beta} \\ |x_i - y_i| - 0.5 \text{beta}, & \text{otherwise. } \end{cases}\end{split}\]Here \(\text{beta}\) controls the point where the loss function changes from quadratic to linear. Its default value is 1.0. \(N\) is the batch size. This function returns an unreduced loss Tensor.
- Parameters
beta (float) – A parameter used to control the point where the function will change from quadratic to linear. Default: 1.0.
- Inputs:
input_data (Tensor) - Tensor of shape \((x_1, x_2, ..., x_R)\). Data type must be float16 or float32.
target_data (Tensor) - Ground truth data, with the same type and shape as input_data.
- Outputs:
Tensor, loss float tensor.
- Raises
TypeError – If beta is not a float.
TypeError – If dtype of input_data or target_data is neither float16 not float32.
ValueError – If beta is less than or equal to 0.
ValueError – If shape of input_data is not the same as target_data.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> loss = nn.SmoothL1Loss() >>> input_data = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> target_data = Tensor(np.array([1, 2, 2]), mindspore.float32) >>> output = loss(input_data, target_data) >>> print(output) [0. 0. 0.5]
-
class
tinyms.losses.
FocalLoss
(weight=None, gamma=2.0, reduction='mean')[source]¶ The loss function proposed by Kaiming team in their paper
Focal Loss for Dense Object Detection
improves the effect of image object detection. It is a loss function to solve the imbalance of categories and the difference of classification difficulty. If you want to learn more, please refer to the paper. https://arxiv.org/pdf/1708.02002.pdf. The function is shown as follows:\[FL(p_t) = -(1-p_t)^\gamma log(p_t)\]- Parameters
gamma (float) – Gamma is used to adjust the steepness of weight curve in focal loss. Default: 2.0.
weight (Union[Tensor, None]) – A rescaling weight applied to the loss of each batch element. The dimension of weight should be 1. If None, no weights are applied. Default: None.
reduction (str) – Type of reduction to be applied to loss. The optional values are “mean”, “sum”, and “none”. If “none”, do not perform reduction. Default: “mean”.
- Inputs:
predict (Tensor) - Tensor of shape should be (B, C) or (B, C, H) or (B, C, H, W). Where C is the number of classes. Its value is greater than 1. If the shape is (B, C, H, W) or (B, C, H), the H or product of H and W should be the same as target.
target (Tensor) - Tensor of shape should be (B, C) or (B, C, H) or (B, C, H, W). The value of C is 1 or it needs to be the same as predict’s C. If C is not 1, the shape of target should be the same as that of predict, where C is the number of classes. If the shape is (B, C, H, W) or (B, C, H), the H or product of H and W should be the same as predict.
- Outputs:
Tensor, it’s a tensor with the same shape and type as input predict.
- Raises
TypeError – If the data type of
gamma
is not float..TypeError – If
weight
is not a Tensor.ValueError – If
target
dim different frompredict
.ValueError – If
target
channel is not 1 andtarget
shape is different frompredict
.ValueError – If
reduction
is not one of ‘none’, ‘mean’, ‘sum’.
- Supported Platforms:
Ascend
GPU
Example
>>> predict = Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], mstype.float32) >>> target = Tensor([[1], [1], [0]], mstype.int32) >>> focalloss = nn.FocalLoss(weight=Tensor([1, 2]), gamma=2.0, reduction='mean') >>> output = focalloss(predict, target) >>> print(output) 0.12516622
-
class
tinyms.losses.
SoftmaxCrossEntropyWithLogits
(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 the target where the classes are mutually exclusive (only one class is positive) using cross entropy loss.
Typical input into this function is unnormalized scores denoted as x whose shape is (N, C), and the corresponding targets.
For each instance \(x_i\), i ranges from 0 to N-1, the loss is given as:
\[\ell(x_i, c) = - \log\left(\frac{\exp(x_i[c])}{\sum_j \exp(x_i[j])}\right) = -x_i[c] + \log\left(\sum_j \exp(x_i[j])\right)\]where \(x_i\) is a 1D score Tensor, \(c\) is the index of 1 in one-hot.
Note
While the target classes are mutually exclusive, i.e., only one class is positive in the target, the predicted probabilities need not to be exclusive. It is only required that the predicted probability distribution of entry is a valid one.
- Parameters
- Inputs:
logits (Tensor) - Tensor of shape (N, C). Data type must be float16 or float32.
labels (Tensor) - Tensor of shape (N, ). If sparse is True, The type of labels is int32 or int64. If sparse is False, the type of labels is the same as the type of logits.
- Outputs:
Tensor, a tensor of the same shape and type as logits with the component-wise logistic losses.
- Raises
TypeError – If sparse is not a bool.
TypeError – If sparse is True and dtype of labels is neither int32 not int64.
TypeError – If sparse is False and dtype of labels is neither float16 not float32.
ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True) >>> np.random.seed(0) >>> logits = Tensor(np.random.randint(0, 9, [1, 10]), mindspore.float32) >>> labels_np = np.ones([1,]).astype(np.int32) >>> labels = Tensor(labels_np) >>> output = loss(logits, labels) >>> print(output) [7.868383]
-
class
tinyms.losses.
BCELoss
(weight=None, reduction='none')[source]¶ BCELoss creates a criterion to measure the binary cross entropy between the true labels and predicted labels.
Set the predicted labels as \(x\), true labels as \(y\), the output loss as \(\ell(x, y)\). Let,
\[L = \{l_1,\dots,l_N\}^\top, \quad l_n = - w_n \left[ y_n \cdot \log x_n + (1 - y_n) \cdot \log (1 - x_n) \right]\]Then,
\[\begin{split}\ell(x, y) = \begin{cases} L, & \text{if reduction} = \text{'none';}\\ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]Note
Note that the predicted labels should always be the output of sigmoid and the true labels should be numbers between 0 and 1.
- Parameters
- Inputs:
inputs (Tensor) - The input Tensor. The data type must be float16 or float32.
labels (Tensor) - The label Tensor which has same shape and data type as inputs.
- Outputs:
Tensor or Scalar, if reduction is ‘none’, then output is a tensor and has the same shape as inputs. Otherwise, the output is a scalar.
- Raises
TypeError – If dtype of inputs, labels or weight (if given) is neither float16 not float32.
ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.
ValueError – If shape of inputs is not the same as labels or weight (if given).
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> weight = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 3.3, 2.2]]), mindspore.float32) >>> loss = nn.BCELoss(weight=weight, reduction='mean') >>> inputs = Tensor(np.array([[0.1, 0.2, 0.3], [0.5, 0.7, 0.9]]), mindspore.float32) >>> labels = Tensor(np.array([[0, 1, 0], [0, 0, 1]]), mindspore.float32) >>> output = loss(inputs, labels) >>> print(output) 1.8952923
-
class
tinyms.losses.
BCEWithLogitsLoss
(reduction='mean', weight=None, pos_weight=None)[source]¶ Adds sigmoid activation function to input predict, and uses the given logits to compute binary cross entropy between the target and the output.
Sets input predict as X, input target as Y, output as L. Then,
\[p_{ij} = sigmoid(X_{ij}) = \frac{1}{1 + e^{-X_{ij}}}\]\[L_{ij} = -[Y_{ij} * ln(p_{ij}) + (1 - Y_{ij})ln(1 - p_{ij})]\]Then,
\[\begin{split}\ell(x, y) = \begin{cases} L, & \text{if reduction} = \text{'none';}\\ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} \end{cases}\end{split}\]- Parameters
reduction (str) – Type of reduction to be applied to loss. The optional values are ‘mean’, ‘sum’, and ‘none’. If ‘none’, do not perform reduction. Default:’mean’.
weight (Tensor, optional) – A rescaling weight applied to the loss of each batch element. If not None, it must can be broadcast to a tensor with shape of predict, data type must be float16 or float32. Default: None.
pos_weight (Tensor, optional) – A weight of positive examples. Must be a vector with length equal to the number of classes. If not None, it must can be broadcast to a tensor with shape of predict, data type must be float16 or float32. Default: None.
- Inputs:
predict (Tensor) - Input logits. The data type must be float16 or float32.
target (Tensor) - Ground truth label. Has the same data type and shape with predict.
- Outputs:
Scalar. If reduction is ‘none’, it’s a tensor with the same shape and type as input predict.
- Raises
TypeError – If data type of predict or target is neither float16 nor float32.
TypeError – If weight or pos_weight is Parameter.
TypeError – If data type of weight or pos_weight is neither float16 nor float32.
ValueError – If weight or pos_weight can not be broadcast to a tensor with shape of predict.
ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.
- Supported Platforms:
Ascend
Examples
>>> predict = Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]).astype(np.float32)) >>> target = Tensor(np.array([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]).astype(np.float32)) >>> loss = nn.BCEWithLogitsLoss() >>> output = loss(predict, target) >>> print(output) 0.3463612
-
class
tinyms.losses.
CosineEmbeddingLoss
(margin=0.0, reduction='mean')[source]¶ Computes the similarity between two tensors using cosine distance.
Given two tensors x1, x2, and a Tensor label y with values 1 or -1:
\[\begin{split}loss(x_1, x_2, y) = \begin{cases} 1-cos(x_1, x_2), & \text{if } y = 1\\ max(0, cos(x_1, x_2)-margin), & \text{if } y = -1\\ \end{cases}\end{split}\]- Parameters
- Inputs:
input_x1 (Tensor) - Input tensor.
input_x2 (Tensor) - Its shape and data type must be the same as input_x1’s shape and data type.
y (Tensor) - Contains value 1 or -1. Suppose the shape of input_x1 is \((x_1, x_2, x_3,..., x_R)\), then the shape of target must be \((x_1, x_3, x_4, ..., x_R)\).
- Outputs:
loss (Tensor) - If reduction is “none”, its shape is the same as y’s shape, otherwise a scalar value will be returned.
- Raises
TypeError – If margin is not a float.
ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.
ValueError – If margin is not in range [-1, 1].
- Supported Platforms:
Ascend
GPU
Examples
>>> x1 = Tensor(np.array([[0.3, 0.8], [0.4, 0.3]]), mindspore.float32) >>> x2 = Tensor(np.array([[0.4, 1.2], [-0.4, -0.9]]), mindspore.float32) >>> y = Tensor(np.array([1, -1]), mindspore.int32) >>> cosine_embedding_loss = nn.CosineEmbeddingLoss() >>> output = cosine_embedding_loss(x1, x2, y) >>> print(output) 0.0003426075
-
class
tinyms.losses.
SampledSoftmaxLoss
(num_sampled, num_classes, num_true=1, sampled_values=None, remove_accidental_hits=True, seed=0, reduction='none')[source]¶ Computes the sampled softmax training loss.
- Parameters
num_sampled (int) – The number of classes to randomly sample per batch.
num_classes (int) – The number of possible classes.
num_true (int) – The number of target classes per training example.
sampled_values (Union[list, tuple]) – List or tuple of (sampled_candidates, true_expected_count, sampled_expected_count) returned by a *CandidateSampler function. Default to None, UniformCandidateSampler is applied.
remove_accidental_hits (bool) – Whether to remove “accidental hits” where a sampled class equals one of the target classes. Default is True.
seed (int) – Random seed for candidate sampling. Default: 0
reduction (str) – Type of reduction to be applied to loss. The optional values are “mean”, “sum”, and “none”. If “none”, do not perform reduction. Default: “none”.
- Inputs:
weights (Tensor) - Tensor of shape (C, dim).
bias (Tensor) - Tensor of shape (C). The class biases.
labels (Tensor) - Tensor of shape (N, num_true), type int64, int32. The target classes.
inputs (Tensor) - Tensor of shape (N, dim). The forward activations of the input network.
- Outputs:
Tensor, a tensor of shape (N) with the per-example sampled softmax losses.
- Raises
TypeError – If sampled_values is not a list or tuple.
TypeError – If dtype of labels is neither int32 not int64.
ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.
ValueError – If num_sampled or num_true is great than num_classes.
ValueError – If length of sampled_values is not equal to 3.
- Supported Platforms:
GPU
Examples
>>> mindspore.set_seed(1) >>> loss = nn.SampledSoftmaxLoss(num_sampled=4, num_classes=7, num_true=1) >>> weights = Tensor(np.random.randint(0, 9, [7, 10]), mindspore.float32) >>> biases = Tensor(np.random.randint(0, 9, [7]), mindspore.float32) >>> labels = Tensor([0, 1, 2]) >>> inputs = Tensor(np.random.randint(0, 9, [3, 10]), mindspore.float32) >>> output = loss(weights, biases, labels, inputs) >>> print(output) [4.6051701e+01 1.4000047e+01 6.1989022e-06]
-
class
tinyms.losses.
DiceLoss
(smooth=1e-05)[source]¶ The Dice coefficient is a set similarity loss. It is used to calculate the similarity between two samples. The value of the Dice coefficient is 1 when the segmentation result is the best and 0 when the segmentation result is the worst. The Dice coefficient indicates the ratio of the area between two objects to the total area. The function is shown as follows:
\[dice = 1 - \frac{2 * (pred \bigcap true)}{pred \bigcup true}\]- Parameters
smooth (float) – A term added to the denominator to improve numerical stability. Should be greater than 0. Default: 1e-5.
- Inputs:
y_pred (Tensor) - Tensor of shape (N, …). The data type must be float16 or float32.
y (Tensor) - Tensor of shape (N, …). The data type must be float16 or float32.
- Outputs:
Tensor, a tensor of shape with the per-example sampled Dice losses.
- Raises
ValueError – If the dimensions are different.
TypeError – If the type of inputs are not Tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> loss = nn.DiceLoss(smooth=1e-5) >>> y_pred = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32) >>> y = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mstype.float32) >>> output = loss(y_pred, y) >>> print(output) 0.38596618
-
class
tinyms.losses.
MultiClassDiceLoss
(weights=None, ignore_indiex=None, activation='softmax')[source]¶ When there are multiple classifications, label is transformed into multiple binary classifications by one hot. For each channel section in the channel, it can be regarded as a binary classification problem, so it can be obtained through the binary loss of each category, and then the average value.
- Parameters
weights (Union[Tensor, None]) – Tensor of shape [num_classes, dim]. The weight shape[0] should be equal to y shape[1].
activation (Union[str, Cell]) – Activate function applied to the output of the fully connected layer, eg. ‘ReLU’. Default: ‘softmax’. Choose from: [‘softmax’, ‘logsoftmax’, ‘relu’, ‘relu6’, ‘tanh’,’Sigmoid’]
- Inputs:
y_pred (Tensor) - Tensor of shape (N, C, …). The y_pred dimension should be greater than 1. The data type must be float16 or float32.
y (Tensor) - Tensor of shape (N, C, …). The y dimension should be greater than 1. The data type must be loat16 or float32.
- Outputs:
Tensor, a tensor of shape with the per-example sampled MultiClass Dice Losses.
- Raises
ValueError – If the shapes are different.
TypeError – If the type of inputs are not Tensor.
ValueError – If the dimension of y or y_pred is less than 2.
ValueError – If the weight shape[0] is not equal to y.shape[1].
ValueError – If weight is a tensor, but the dimension is not 2.
- Supported Platforms:
Ascend
GPU
Examples
>>> loss = nn.MultiClassDiceLoss(weights=None, ignore_indiex=None, activation="softmax") >>> y_pred = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32) >>> y = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mstype.float32) >>> output = loss(y_pred, y) >>> print(output) 0.3283009
-
class
tinyms.losses.
RMSELoss
[source]¶ RMSELoss creates a standard to measure the root mean square error between \(x\) and \(y\) element-wise, where \(x\) is the input and \(y\) is the target.
For simplicity, let \(x\) and \(y\) be 1-dimensional Tensor with length \(M\) and \(N\), the unreduced loss (i.e. with argument reduction set to ‘none’) of \(x\) and \(y\) is given as:
\[\begin{split}loss = \begin{cases} \sqrt{\frac{1}{M}\sum_{m=1,n=1}^{M,N}{(x_m-y_n)^2}}, & \text {if M > N } \\\\ \sqrt{\frac{1}{N}\sum_{m=1,n=1}^{M,N}{(x_m-y_n)^2}}, &\text{if M < N } \end{cases}\end{split}\]- Inputs:
logits (Tensor) - Tensor of shape \((x_1, x_2, ..., x_M)\).
label (Tensor) - Tensor of shape \((y_1, y_2, ..., y_N)\).
- Outputs:
Tensor, weighted loss float tensor.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> loss = nn.RMSELoss() >>> input_data = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> target_data = Tensor(np.array([1, 2, 2]), mindspore.float32) >>> output = loss(input_data, target_data) >>> print(output) 0.57735026
-
class
tinyms.losses.
MAELoss
(reduction='mean')[source]¶ MAELoss creates a standard to measure the average absolute error between \(x\) and \(y\) element-wise, where \(x\) is the input and \(y\) is the target.
For simplicity, let \(x\) and \(y\) be 1-dimensional Tensor with length \(M\) and \(N\), the unreduced loss (i.e. with argument reduction set to ‘none’) of \(x\) and \(y\) is given as:
\[\begin{split}MAE = \begin{cases} \sqrt{\frac{1}{M}\sum_{m=1,n=1}^{M,N}{|x_m-y_n|}}, & \text {if M > N } \\\\ \sqrt{\frac{1}{N}\sum_{m=1,n=1}^{M,N}{|x_m-y_n|}}, &\text{if M < N } \end{cases}\end{split}\]- Parameters
reduction (str) – Type of reduction to be applied to loss. The optional values are “mean”, “sum”, and “none”. Default: “mean”.
- Inputs:
logits (Tensor) - Tensor of shape \((x_1, x_2, ..., x_M)\).
label (Tensor) - Tensor of shape \((y_1, y_2, ..., y_N)\).
- Outputs:
Tensor, weighted loss float tensor.
- Raises
ValueError – If reduction is not one of ‘none’, ‘mean’, ‘sum’.
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> loss = nn.MAELoss() >>> input_data = Tensor(np.array([1, 2, 3]), mindspore.float32) >>> target_data = Tensor(np.array([1, 2, 2]), mindspore.float32) >>> output = loss(input_data, target_data) >>> print(output) 0.33333334