mixle.models._neural_serial module¶
Serialization + input-validation helpers shared by the neural-leaf families (mixle.models).
The neural leaves wrap a live torch.nn.Module, which the generic JSON encoder in
mixle.utils.serialization cannot walk (a module is not a registered mixle class). These helpers give
every neural leaf a working to_dict/from_dict and the recursive __pysp_getstate__/__pysp_setstate__
hooks (so a leaf inside a MixtureDistribution serializes too) by persisting the module as portable bytes.
The module round-trips through torch.save/torch.load of a pickle byte buffer – which requires the
wrapped nn.Module class to be reachable at module level (that is why the build_* helpers were hoisted). The
bytes are base64-encoded so the whole payload is plain JSON.
- module_to_bytes(module)[source]
Serialize a torch nn.Module (architecture + weights) to portable bytes via
torch.save.
- module_from_bytes(data)[source]
Reconstruct a torch nn.Module previously encoded by
module_to_bytes().
- encode_module(module)[source]
A JSON-safe tagged dict for a torch module (base64 of
module_to_bytes()).
- decode_module(payload)[source]
Inverse of
encode_module().
- check_finite(x, where)[source]
Raise a clear error if
xhas any non-finite entry, so a NaN cannot silently poison a mixture E-step.A neural leaf that returned NaN log-density would corrupt every responsibility in the E-step without a diagnosable failure; validating at the density boundary turns that into an immediate, named error instead.