> 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/writing-integration-code/custom-metrics.md).

# Custom Metrics

You can write your own custom metrics to be calculated and stored for later use within the [**Metrics Dashboard**](/user-interface/dashboards/dashlets/metrics-dashboard.md).

Tensorleap stores metrics and metadata for every sample. This allows the user to investigate certain populations within the dataset. This also means that the **custom metric** is calculated per prediction on a **single** sample.

This function can get multiple `np.ndarray` arrays as inputs which will be exposed in the **Metric** UI connections. These are of shape *`(batch, dim-1,...,dim-n)`*. \
The function returns a `np.ndarray` that contains a batched metric result.&#x20;

Example of usage:

```python
@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)
    }
```

The [@tensorleap\_custom\_metric](/tensorleap-integration/python-api/code_loader/decorators/tensorleap_custom_metric.md) decorator registers each metric collection into the Tensorleap integration, and can be added to the model analysis using the [integration test](/tensorleap-integration/integration-test.md).


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code/custom-metrics.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
