mindspore.mint.nn.BCEWithLogitsLoss
- class mindspore.mint.nn.BCEWithLogitsLoss(weight=None, reduction='mean', pos_weight=None)[source]
Add sigmoid activation function to input as logits, and use this logits to compute binary cross entropy between the logits and the target.
Set input input 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} \cdot \log(p_{ij}) + (1 - Y_{ij}) \cdot \log(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
weight (Tensor, optional) – A rescaling weight applied to the loss of each batch element. If not None, it can be broadcast to a tensor with shape of target, data type must be mindspore.float16, mindspore.float32 or mindspore.bfloat16(only Atlas A2 series products are supported). Default
None.reduction (str, optional) –
Apply specific reduction method to the output:
'none','mean','sum'. Default'mean'.'none': no reduction will be applied.'mean': compute and return the weighted mean of elements in the output.'sum': the output elements will be summed.
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 be broadcast to a tensor with shape of input, data type must be mindspore.float16, mindspore.float32 or mindspore.bfloat16(only Atlas A2 series products are supported). Default
None.
- Inputs:
input (Tensor) - Input input with shape \((N, *)\) where \(*\) means, any number of additional dimensions. The data type must be mindspore.float16, mindspore.float32 or mindspore.bfloat16(only Atlas A2 series products are supported).
target (Tensor) - Ground truth label with shape \((N, *)\) where \(*\) means, any number of additional dimensions. The same shape and data type as input.
- Outputs:
Tensor or Scalar, if reduction is
'none', its shape is the same as input. Otherwise, a scalar value will be returned.
- Raises
ValueError – If weight or pos_weight can not be broadcast to a tensor with shape of input.
ValueError – If reduction is not one of
'none','mean','sum'.
- Supported Platforms:
Ascend
Examples
>>> import mindspore >>> input = mindspore.tensor([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]) >>> target = mindspore.tensor([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]) >>> loss = mindspore.mint.nn.BCEWithLogitsLoss() >>> output = loss(input, target) >>> print(output) 0.3463612