> For the complete documentation index, see [llms.txt](https://docs.tensorleap.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/decorators/tensorleap_custom_loss.md).

# @tensorleap\_custom\_loss

The purpose of the `tensorleap_custom_loss` decorator 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
@tensorleap_custom_loss(name='weighted_ce')
def loss(tensor_1: np.ndarray, tensor_2: np.ndarray, ...) -> np.ndarray:
    pass
```

<table><thead><tr><th width="158.46928201888204">Args</th><th></th></tr></thead><tbody><tr><td><code>name</code></td><td><em>(str)</em> with the given name of the <strong>custom loss.</strong></td></tr></tbody></table>

#### Loss Function inputs:

np.ndarray tensors with a batch dimension

#### Loss Function outputs:

np.ndarray batched loss values

### Examples

#### Basic Usage

```python
from code_loader.contract.datasetclasses import PreprocessResponse
from code_loader.inner_leap_binder.leapbinder_decorators import tensorleap_custom_loss
import numpy as np
...

@tensorleap_custom_loss(name='weighted_ce')
def weighted_categorical_crossentropy(y_true :np.ndarray, y_pred: np.ndarray) -> np.ndarray:
    # Normalize predictions so each sample's probabilities sum to 1
    y_pred = y_pred / np.sum(y_pred, axis=-1, keepdims=True)
    
    # Clip predictions to avoid log(0) and ensure numerical stability
    epsilon = 1e-7  # Similar to K.epsilon()
    y_pred = np.clip(y_pred, epsilon, 1 - epsilon)
    
    # Define class weights
    weights = np.array([0.5, 2.1, 3, 4, 4, 4, 4, 4])
    
    # Compute weighted log loss
    loss = y_true * np.log(y_pred) * weights
    loss = -np.sum(loss, axis=-1)
    return loss
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/decorators/tensorleap_custom_loss.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
