# add\_custom\_loss

The purpose of the `leap_binder.add_custom_loss` function is to register the [**Custom Loss Function(s)**](/tensorleap-integration/writing-integration-code/custom-loss-function.md) to be used within the platform. This function adds the custom loss function to the selection list within the **CustomLoss** node.

```python
code_loader.leap_binder.add_custom_loss(
    function=CustomCallableInterface,
    name=str
)
```

<table><thead><tr><th width="158.46928201888204">Args</th><th></th></tr></thead><tbody><tr><td><code>function</code></td><td><em>(</em>CustomCallableInterface<em>)</em> This parameter points to the custom <strong>Loss Function</strong>.</td></tr><tr><td><code>name</code></td><td><em>(str)</em> with the given name of the <strong>custom loss.</strong></td></tr></tbody></table>

### Examples

#### Basic Usage

```python
import numpy as np
from code_loader import leap_binder
...
def weighted_categorical_crossentropy(y_true, y_pred):
    # scale predictions so that the class probas of each sample sum to 1
    y_pred /= K.sum(y_pred, axis=-1, keepdims=True)
    # clip to prevent NaN's and Inf's
    y_pred = K.clip(y_pred, K.epsilon(), 1 - K.epsilon())
    # calc
    weights = np.array([0.5, 2.1, 3, 4, 4, 4, 4, 4])
    loss = y_true * K.log(y_pred) * weights
    loss = -K.sum(loss, -1)
    return loss

...
leap_binder.add_custom_loss(
    function=weighted_categorical_crossentropy,
    name='weighted_categorical_crossentropy'
)
```


---

# 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/tensorleap-integration/python-api/code_loader/leap_binder/add_custom_loss.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.
