> 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_metric.md).

# @tensorleap\_custom\_metric

The purpose of the `tensorleap_custom_metric` decorator is to add a [custom metric ](/tensorleap-integration/writing-integration-code/custom-metrics.md)suited to your needs to the tensorleap platform.

```python
@tensorleap_custom_metric(name='metrics',
                          direction=MetricDirection.Downward,
                          compute_insights=None)
def metrics(
    tensor_1: npt.NDArray[np.float32],
    tensor_2: npt.NDArray[np.float32],
    ...
) -> Union[npt.NDArray[np.float32], Dict[str, npt.NDArray[np.float32]]]:
    pass
```

<table><thead><tr><th width="158.46928201888204">Args</th><th></th></tr></thead><tbody><tr><td><code>name</code></td><td>The name of the the metric</td></tr><tr><td><code>direction</code></td><td><p>(optional, defaults to<code>MetricDirection.Downward</code>)</p><ul><li>For a metric where lower values are better use <code>MetricDirection.Downward</code></li><li>For a metric where higher values are better use <code>MetricDirection.Upward</code></li></ul></td></tr><tr><td><code>compute_insights</code></td><td><p>(optional, defaults to None). Specify metrics that should not have insights computed on them:<br></p><ul><li>in case the metric function returns a dictionary, compute_insights should return a dictionary as well, mapping metric names to booleans.</li><li>otherwise, compute_insights=False if this metric should be ommited when computing insights</li></ul></td></tr></tbody></table>

#### Metric Function inputs:

np.ndarray tensors with a batch dimension

#### Metric Function outputs:

np.ndarray batched metric values

### Examples

#### Basic Usage

```python
import numpy as np
import numpy.typing as npt
from code_loader.inner_leap_binder.leapbinder_decorators import tensorleap_custom_metric


@tensorleap_custom_metric(name='metrics',
                          direction=MetricDirection.Downward,
                          compute_insights=None)
def custom_metric_distances(
    y_true: npt.NDArray[np.float32],
    y_pred: npt.NDArray[np.float32]
) -> dict[str, npt.NDArray[np.float32]]:
    diff = y_true - y_pred
    axis = tuple(range(1, diff.ndim))

    return {
        "mean_difference": np.mean(diff, axis=axis),
        "mean_absolute_difference": np.mean(np.abs(diff), axis=axis)
    }

```

Full script usage can be found at [**Integration Script**](/tensorleap-integration/writing-integration-code.md#dataset-script).


---

# 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_metric.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.
