I do neuro/ai research, so I have to instantiate a lot of Tensors from YAML definitions (that's how I define models declaratively). I built a middleware to simplify this, using intermediary pydantic models as "blueprints" for building complex objects during pydantic's build process. It lets me pass parameters (e.g. mean and standard deviation) into `thing.model_validate(...)`, and get a fully-built Tensor in the appropriate field of the model.
I do neuro/ai research, so I have to instantiate a lot of Tensors from YAML definitions (that's how I define models declaratively). I built a middleware to simplify this, using intermediary pydantic models as "blueprints" for building complex objects during pydantic's build process. It lets me pass parameters (e.g. mean and standard deviation) into `thing.model_validate(...)`, and get a fully-built Tensor in the appropriate field of the model.
https://github.com/flywhl/cyantic
---
Example:
If you write a blueprint like
and a data class like you can construct MyModel using mean and std instead of a Tensor: You can also pre-process the data using `@hook` references, and there's an API for defining custom hooks.There's good test coverage and I think the library is ready for use, but would appreciate issues if you come across any bugs.
Thanks for reading