# 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](/tensorleap-integration/integration-test.md).
{% endhint %}

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

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

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="/files/P2hHoVfQALSkByDv0vjR" 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](/tensorleap-integration/writing-integration-code/custom-loss-function.md) from your [integration script](/tensorleap-integration/writing-integration-code.md).
* **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](/tensorleap-integration/writing-integration-code.md).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.tensorleap.ai/user-interface/project/network/network-mapping/create-a-mapping-deprecated/loss-node.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
