# Loss Node

## Custom Loss

{% hint style="success" %}
Using a custom loss is the recommended way to work within Tensorleap. It allows you to test the validation of the loss in your [local environment](https://docs.tensorleap.ai/tensorleap-integration/integration-test).
{% endhint %}

{% hint style="info" %}
Adding a custom loss is only possible after registering it in the [integration script](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code) and parsing it successfully through the system. To make sure you can add your loss ensure that the correct [code integration](https://docs.tensorleap.ai/user-interface/project/network/code-integration) that defines your loss is registered in the system
{% endhint %}

To add a custom loss, select CustomLoss when [adding nodes](https://docs.tensorleap.ai/user-interface/project/network/network-mapping/create-a-mapping-deprecated/..#adding-nodes) to the mapping. This loss would have the same arguments defined in your [custom loss](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code/custom-loss-function) defined in your [integration script](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code).

For example, adding this loss in the integration script:

```python
@tensorleap_custom_loss("total_loss")
def loss(pred80,pred40,pred20,gt,demo_pred):
    gt=np.squeeze(gt,axis=0)
    d={}
    d["bboxes"] = torch.from_numpy(gt[...,:4])
    d["cls"] = torch.from_numpy(gt[...,4])
    d["batch_idx"] = torch.zeros_like(d['cls'])
    y_pred_torch = [torch.from_numpy(s) for s in [pred80,pred40,pred20]]
    all_loss,_= criterion(y_pred_torch, d)
    return all_loss.unsqueeze(0).numpy()
```

Would result in a possible node addition to the mapping:

<figure><img src="https://3509361326-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F9UXeOlFqlw8pl79U2HGU%2Fuploads%2FtIQqkXw9DvTZvgwiKtDH%2Fimage.png?alt=media&#x26;token=630258af-d5d9-45cb-8680-92cc4d470b2f" alt=""><figcaption><p>Custom Loss and its properties</p></figcaption></figure>

This node could then be connected to the different inputs it expects to get in order to operate as expected

## Setup

The **CustomLoss** node have several properties:

* **Selected Custom Loss**: a dropdown from which a custom loss can be selected. The list would include all registered [custom losses](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code/custom-loss-function) from your [integration script](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code).
* **Name**: The name of the selected ground truth encoder in the integration script
* **Arguments**: The expected arguments for your custom loss, as detailed in the integration script.

## Default losses

The Tensorleap integration compes equipped with several losses ready for usage. These losses can be added from the dropdown menu, and each would have its own configurable settings. Adding these losses does not require any addition of a custom loss within the [integration script](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code).
