# smooth_bce_with_logits

Python API: `luxonis_train.attached_modules.losses.smooth_bce_with_logits`

Binary cross entropy with label smoothing.

## Classes

### SmoothBCEWithLogitsLoss

Binary cross entropy on logits, with label smoothing.

The loss moves each target toward the opposite class. It then computes
[BCEWithLogitsLoss](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/bce_with_logits.md)
on the new targets.

 * `Inputs:`: * `predictions` (`Tensor`): `[B, C, ...]` logits
    * `target` (`Tensor`): same shape, float values in `[0, 1]`
 * `Outputs:`: * `Tensor`: scalar, or `[B, C, ...]` when `reduction` is `"none"`
 * `Formula:`: For the smoothing factor s, the target y becomes y’ = (1 − s) y + s (1 − y) A target of `1` thus becomes 1 − s, and
   a target of `0` becomes s. The loss is
   [BCEWithLogitsLoss](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/bce_with_logits.md)
   of the logits and y’, with `bce_pow` as its `pos_weight`.

> **References**
> * Source: This project.
 * License: Apache-2.0 (this project)

> **Notes**
> `bce_pow` is not an exponent. It is the factor of the positive term of the binary cross entropy.

> **Example**
> Attached to a `DDRNetSegmentationHead` in `model.nodes`:

```yaml
- name: DDRNetSegmentationHead
  inputs: [DDRNet]
  losses:
    - name: SmoothBCEWithLogitsLoss
```

 * `Compatible with:`: * Nodes: *
   [BiSeNetHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/bisenet_head.md)
       * [ClassificationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/classification_head.md)
       * [DDRNetSegmentationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/ddrnet_segmentation_head.md)
       * [SegmentationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/segmentation_head.md)
       * [TransformerClassificationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/transformer_classification_head.md)
       * [TransformerSegmentationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/transformer_segmentation_head.md)

#### Methods

##### init

```python
def __init__(label_smoothing: float = 0.0, bce_pow: float = 1.0, weight: list[float] | None = None, reduction: Literal['mean', 'sum', 'none'] = 'mean', **kwargs):
```

Initialize the loss and the wrapped
[BCEWithLogitsLoss](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/bce_with_logits.md).

Parameters

 * `label_smoothing` (`float`): The smoothing factor s. A target of `1` becomes `1 - label_smoothing`, and a target of `0` becomes
   `label_smoothing`. `0.0` keeps the targets unchanged.
 * `bce_pow` (`float`): The factor of the positive term of the loss, the same for all classes. It becomes the `pos_weight` of the
   wrapped loss.
 * `weight` (`list[float] | None`): Factors for the loss of the elements. The wrapped loss turns the list into a tensor that
   broadcasts against the loss, aligned at the last dimension. `None` gives every element the factor `1`.
 * `reduction` (`Literal['mean', 'sum', 'none']`): How to reduce the loss of the elements: * `"none"`: return the loss of each
   element.
    * `"mean"`: return the mean over all elements.
    * `"sum"`: return the sum over all elements.
 * `**kwargs`: Keyword arguments forwarded to
   [BaseLoss](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/base_loss.md),
   such as `final_loss_weight` and `node`.

##### forward

```python
def forward(predictions: Tensor, target: Tensor) -> Tensor:
```

Smooth the targets and compute the binary cross entropy.

> **Example**
> A smoothing factor of `0.1` turns the targets `1` and `0` into `0.9` and `0.1`:

```pycon
>>> import torch
>>> import torch.nn.functional as F
>>> logits = torch.tensor([[2.0, -1.0]])
>>> target = torch.tensor([[1.0, 0.0]])
>>> loss = SmoothBCEWithLogitsLoss(label_smoothing=0.1)
>>> smoothed = torch.tensor([[0.9, 0.1]])
>>> bce = F.binary_cross_entropy_with_logits(logits, smoothed)
>>> torch.allclose(loss(logits, target), bce)
True
```

Parameters

 * `predictions` (`Tensor`): Logits of shape `[B, C, ...]`, the main output of the node.
 * `target` (`Tensor`): Float targets in `[0, 1]`, of the same shape as `predictions`.

Returns

 * `Tensor`: A scalar for the `"mean"` and `"sum"` reductions. For `"none"`, the loss of each element, of shape `[B, C, ...]`.

Raises

 * `RuntimeError`: When `predictions` and `target` have different shapes.

#### Attributes

##### criterion

##### supported_tasks
