pandera.api.tensordict.container.TensorDictSchema¶
- class pandera.api.tensordict.container.TensorDictSchema(keys=None, batch_size=None, dtype=None, checks=None, coerce=False, name=None, title=None, description=None, metadata=None)[source]¶
Validate TensorDict and tensorclass objects.
Analogous to
DataFrameSchemafor DataFrames andDatasetSchemafor xarray Datasets.The schema accepts a
keysparameter (instead ofcolumns) which is more semantically appropriate for TensorDict’s dictionary-like structure.Keys may be plain strings or nested tuples, e.g.
("next", "observation"), matching TorchRL’s nested TensorDict layout.Create TensorDict schema.
- Parameters:
keys (
UnionType[dict[Union[str,tuple[str, …]],Tensor],None]) – Dictionary mapping key names toTensorcomponents. Keys may be strings or nested tuples, e.g.("next", "observation"). If a list of strings is provided, creates default Tensor components for each key.batch_size (
UnionType[tuple[UnionType[int,None], …],None]) – Expected batch dimensions. UseNonefor flexible dimensions. For example,(32,)requires exactly 32 samples, while(None,)allows any batch size.dtype (
Any) – Override dtype for all tensors (optional).checks – Schema-wide checks applied to the entire TensorDict.
coerce (
bool) – Enable automatic dtype coercion during validation.title (
UnionType[str,None]) – Human-readable title for the schema.description (
UnionType[str,None]) – Detailed description of the schema.metadata (
UnionType[dict,None]) – Additional metadata associated with the schema.
Attributes
BACKEND_REGISTRYpropertiesGet the properties of the schema for serialization purposes.
Methods
- __init__(keys=None, batch_size=None, dtype=None, checks=None, coerce=False, name=None, title=None, description=None, metadata=None)[source]¶
Create TensorDict schema.
- Parameters:
keys (
UnionType[dict[Union[str,tuple[str, …]],Tensor],None]) – Dictionary mapping key names toTensorcomponents. Keys may be strings or nested tuples, e.g.("next", "observation"). If a list of strings is provided, creates default Tensor components for each key.batch_size (
UnionType[tuple[UnionType[int,None], …],None]) – Expected batch dimensions. UseNonefor flexible dimensions. For example,(32,)requires exactly 32 samples, while(None,)allows any batch size.dtype (
Any) – Override dtype for all tensors (optional).checks – Schema-wide checks applied to the entire TensorDict.
coerce (
bool) – Enable automatic dtype coercion during validation.title (
UnionType[str,None]) – Human-readable title for the schema.description (
UnionType[str,None]) – Detailed description of the schema.metadata (
UnionType[dict,None]) – Additional metadata associated with the schema.
- classmethod get_backend(check_obj=None, check_type=None)[source]¶
Get the backend for TensorDict validation.
Overrides base class to handle tensorclass objects which have unique types at runtime. We use TensorDict’s backend since tensorclasses share the same interface and both inherit from object in their MRO.
- Return type:
BaseSchemaBackend
- static register_default_backends(check_obj_cls)[source]¶
Register default backends at validation time.
- Return type:
- validate(check_obj, head=None, tail=None, sample=None, random_state=None, lazy=False, inplace=False)[source]¶
Validate a TensorDict against the schema.
- Parameters:
check_obj (
Any) – TensorDict or tensorclass to validate.head (
UnionType[int,None]) – Number of leading samples to validate (not used for TensorDict).tail (
UnionType[int,None]) – Number of trailing samples to validate (not used for TensorDict).sample (
UnionType[int,None]) – Number of random samples to validate (not used for TensorDict).random_state (
UnionType[int,None]) – Random seed for sampling (not used for TensorDict).lazy (
bool) – IfTrue, collect all errors instead of fail-fast.inplace (
bool) – Whether to modify object in place (not supported for TensorDict).
- Return type:
- Returns:
Validated TensorDict or tensorclass.
- Raises:
SchemaError – If validation fails.