Checks and Lazy Validation

Use Check to validate tensor values.

Apply checks to Tensor components

import torch
from tensordict import TensorDict
from pandera import Check
import pandera.tensordict as pa

schema = pa.TensorDictSchema(
    keys={
        "values": pa.Tensor(
            dtype=torch.float32,
            shape=(None,),
            checks=[Check.greater_than(0.0), Check.less_than(1.0)],
        ),
    },
    batch_size=(10,),
)

td = TensorDict(
    {"values": torch.tensor([0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 0.95])},
    batch_size=[10],
)
validated = schema.validate(td)

Lazy validation

Set lazy=True to collect all errors:

td_invalid = TensorDict(
    {"values": torch.tensor([-0.1, 0.2, 0.3, 10.0, 0.5])},
    batch_size=[5],
)

try:
    schema.validate(td_invalid, lazy=True)
except pa.errors.SchemaErrors as exc:
    print(f"Found {len(exc.schema_errors)} errors")
    print(exc)
Found 3 errors
{
    "SCHEMA": {
        "WRONG_DATATYPE": [
            {
                "schema": null,
                "column": null,
                "check": null,
                "error": "Expected batch_size[0]=10, got batch_size[0]=5"
            }
        ]
    },
    "DATA": {
        "CHECK_ERROR": [
            {
                "schema": null,
                "column": "values",
                "check": "greater_than(0.0)",
                "error": "column 'values' failed element-wise validator number 0: greater_than(0.0): failure cases found at tensor index [0]"
            },
            {
                "schema": null,
                "column": "values",
                "check": "less_than(1.0)",
                "error": "column 'values' failed element-wise validator number 1: less_than(1.0): failure cases found at tensor index [3]"
            }
        ]
    }
}

Failure cases point at the failing elements

When a value check fails, the reported error carries the exact tensor indices that violated the check, so you can locate the offending elements without searching the tensor by hand:

failure_schema = pa.TensorDictSchema(
    keys={
        "values": pa.Tensor(
            dtype=torch.float32,
            checks=[Check.greater_than(0.0)],
        ),
    },
)

td_fail = TensorDict(
    {"values": torch.tensor([1.0, -2.0, 3.0, -4.0, 5.0])},
    batch_size=[5],
)

try:
    failure_schema.validate(td_fail)
except pa.errors.SchemaError as exc:
    print(exc)
    # The failure cases include the violating indices and values
    print("indices:", exc.failure_cases["index"])
    print("values:", exc.failure_cases["failure_case"])
column 'values' failed element-wise validator number 0: greater_than(0.0): failure cases found at tensor index [1, 3]
indices: [1, 3]
values: [-2.0, -4.0]

For N-D tensors the indices are coordinate tuples; for batch-level checks (reduced over trailing dims, e.g. a one-hot row check) the reported index is the batch index of the failing row.

Available checks

Standard pandera checks work with tensor data:

See also