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

# @tensorleap\_input\_encoder

code\_loader.inner\_leap\_binder.leapbinder\_decorators.tensorleap\_input\_encoder

The `tensorleap_input_encoder` decorator registers an [**Input Encoder**](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code/input-encoder.md): a function that returns one model input for one sample.

```python
@tensorleap_input_encoder(name='image', channel_dim=-1)
def input_encoder(idx: int, preprocess: PreprocessResponse) -> np.ndarray:
    pass
```

| Args                |                                                                                                                                                                                                                                                                                                             |
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `name`              | *(str)* The name of the input, e.g. `image`. Must be unique among input encoders.                                                                                                                                                                                                                           |
| `channel_dim`       | *(int, optional)* The channel axis of the input **after** the platform adds the batch axis at position 0. Use `-1` for channel-last (`[B,H,W,C]`) and `1` for channel-first (`[B,C,H,W]`). `0` is invalid, since axis 0 is the batch. Defaults to `-1`; leaving it out produces a warning in the local run. |
| `model_input_index` | *(int, optional)* The position of this input among the model's inputs. Usually not needed: the integration test call `model([input_a, input_b])` already sets the order.                                                                                                                                    |

#### Function inputs

|              |                                                                                                                                                                                                                                                                                     |
| ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `idx`        | A sample id from `preprocess.sample_ids` (`str` or `int`, matching the response's `sample_id_type`). For a [grouped](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/datasetclasses/preprocessresponse.md#grouped-samples) response, a list of sample ids. |
| `preprocess` | The [PreprocessResponse](https://docs.tensorleap.ai/tensorleap-integration/python-api/code_loader/datasetclasses/preprocessresponse.md) the sample belongs to.                                                                                                                      |

#### Function output

A `np.ndarray` of dtype `float32`, without a batch axis. Any other dtype raises an error. For a grouped response, return either a list with one array per sample id, or one array whose first axis has the group's length.

{% hint style="info" %}
Inside the integration test the returned array already has the batch axis added (shape `[1, ...]`), so it can be passed to the model as is.
{% endhint %}

### Examples

#### Basic Usage

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


@tensorleap_input_encoder('image', channel_dim=-1)
def input_encoder(idx: str, preprocess: PreprocessResponse) -> np.ndarray:
    return preprocess.data['images'][idx].astype(np.float32)
```

Usage within the full script can be found at [**Dataset Script**](https://docs.tensorleap.ai/tensorleap-integration/writing-integration-code.md#dataset-script).

#### Guides

Full examples can be found at the **Dataset Integration** section of the following guides:

* [**MNIST Guide**](https://docs.tensorleap.ai/guides/full-guides/mnist-guide.md)
* [**IMDB Guide**](https://docs.tensorleap.ai/guides/full-guides/imdb-guide.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/python-api/code_loader/decorators/tensorleap_input_encoder.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.
