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.<p><a href="https://github.com/flywhl/cyantic">https://github.com/flywhl/cyantic</a><p>---<p>Example:<p>If you write a blueprint like<p><pre><code> @blueprint(Tensor)
class NormalTensor(Blueprint[Tensor]):
mean: float
std: float
size: tuple[int, ...]
def build(self) -> Tensor:
return torch.normal(self.mean, self.std, size=self.size)
</code></pre>
and a data class like<p><pre><code> class MyModel(CyanticModel):
some_tensor: Tensor
</code></pre>
you can construct MyModel using mean and std instead of a Tensor:<p><pre><code> some_yaml = """common:
size: [3, 5]
some_tensor:
mean: 0.0
std: 0.1
size: @value:common.size
"""
my_model = MyModel.model_validate(yaml.safe_load(some_yaml))
</code></pre>
You can also pre-process the data using `@hook` references, and there's an API for defining custom hooks.<p>There's good test coverage and I think the library is ready for use, but would appreciate issues if you come across any bugs.<p>Thanks for reading