mindspore.ops.BCEWithLogitsLoss
- class mindspore.ops.BCEWithLogitsLoss(*args, **kwargs)[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} * log(p_{ij}) + (1 - Y_{ij})log(1 - p_{ij})]\]\(i\) indicates the \(i^{th}\) sample, \(j\) indicates the category. 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}\]\(l\) indicates the method of calculating the loss. There are three methods: the first method is to provide the loss value directly, the second method is to calculate the average value of all losses, and the third method is to calculate the sum of all losses
- 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’.
- Inputs:
predict (Tensor) - Input logits. Data type must be float16 or float32.
target (Tensor) - Ground truth label, has the same shape as predict. Data type must be float16 or float32.
weight (Tensor) - A rescaling weight applied to the loss of each batch element. It can be broadcast to a tensor with shape of predict. Data type must be float16 or float32.
pos_weight (Tensor) - A weight of positive examples. Must be a vector with length equal to the number of classes. It can be broadcast to a tensor with shape of predict. Data type must be float16 or float32.
- 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 any input 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
GPU
Examples
>>> import numpy as np >>> import mindspore.ops as ops >>> from mindspore import Tensor >>> from mindspore.common import dtype as mstype >>> predict = Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]), mstype.float32) >>> target = Tensor(np.array([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]), mstype.float32) >>> weight = Tensor(np.array([1.0, 1.0, 1.0]), mstype.float32) >>> pos_weight = Tensor(np.array([1.0, 1.0, 1.0]), mstype.float32) >>> loss = ops.BCEWithLogitsLoss() >>> output = loss(predict, target, weight, pos_weight) >>> print(output) 0.3463612