# smooth_bce_with_logits

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

Binary cross entropy with label smoothing.

## Classes

| Name | Description |
| --- | --- |
| [`SmoothBCEWithLogitsLoss`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/smooth_bce_with_logits.md#smooth_bce_with_logits-Classes-SmoothBCEWithLogitsLoss) | Binary cross entropy on logits, with label smoothing. |

### class `SmoothBCEWithLogitsLoss`

```python
class SmoothBCEWithLogitsLoss(BaseLoss)
```

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#bce_with_logits-Classes-BCEWithLogitsLoss)
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#bce_with_logits-Classes-BCEWithLogitsLoss)
   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#bisenet_head-Classes-BiSeNetHead)
       * [ClassificationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/classification_head.md#classification_head-Classes-ClassificationHead)
       * [DDRNetSegmentationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/ddrnet_segmentation_head.md#ddrnet_segmentation_head-Classes-DDRNetSegmentationHead)
       * [SegmentationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/segmentation_head.md#segmentation_head-Classes-SegmentationHead)
       * [TransformerClassificationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/transformer_classification_head.md#transformer_classification_head-Classes-TransformerClassificationHead)
       * [TransformerSegmentationHead](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/nodes/heads/transformer_segmentation_head.md#transformer_segmentation_head-Classes-TransformerSegmentationHead)

Attributes

| Name | Description |
| --- | --- |
| `criterion` |  |
| `supported_tasks` |  |

Methods

| Name | Description |
| --- | --- |
| [`__init__`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/smooth_bce_with_logits.md#smooth_bce_with_logits-Classes-SmoothBCEWithLogitsLoss-__init__) | 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#bce_with_logits-Classes-BCEWithLogitsLoss). |
| [`forward`](https://docs.luxonis.com/software-v3/ai-inference/model-source/training/luxonis-train/luxonis-train-api-reference/attached_modules/losses/smooth_bce_with_logits.md#smooth_bce_with_logits-Classes-SmoothBCEWithLogitsLoss-forward) | Smooth the targets and compute the binary cross entropy. |

#### method `__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#bce_with_logits-Classes-BCEWithLogitsLoss).

Args

 * `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#base_loss-Classes-BaseLoss),
   such as `final_loss_weight` and `node`.

#### method `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
```

Args

| Name | Type | Description | Default |
| --- | --- | --- | --- |
| `predictions` | `Tensor` | Logits of shape `[B, C, ...]`, the main output of the node. | required |
| `target` | `Tensor` | Float targets in `[0, 1]`, of the same shape as `predictions`. | required |

Returns

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

Raises

| Type | Description |
| --- | --- |
| `RuntimeError` | When `predictions` and `target` have different shapes. |
