ML Collections is a library of Python collections designed for ML use cases. It provides dict-like data structures, primarily `ConfigDict` and `FrozenConfigDict`, which offer dot-based access, type safety, and other features useful for managing experiment configurations in a structured way. The library is actively maintained, with its current version being 1.1.0, and receives regular updates.
pip install ml-collectionsVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to create a `ConfigDict`, assign and access values using dot notation, and illustrates its type-safe behavior. It also shows the exception for assigning integers to float fields.
Replace `cfg.get_ref('field')` with `cfg.get_oneway_ref('field')` when a one-way dependency is desired.Ensure that assigned values match the initial type of the field, or explicitly cast them if a compatible conversion is desired (e.g., `str(integer_value)`).
Adhere to the `absl.flags`-like syntax for boolean overrides or use explicit `key=value` assignment.
Avoid nesting mutable dict-like structures directly within lists/tuples during initial `ConfigDict` construction or ensure a DAG structure. Consider creating nested `ConfigDict` instances explicitly for complex structures.
Ensure the field name is correct. If you intend to add a new field or modify a locked ConfigDict, use the `with cfg.unlocked():` context manager, or call `cfg.unlock()` and `cfg.lock()` explicitly.
Assign a value that is compatible with the field's original type. If you genuinely need to change the type, you can temporarily disable type safety using the `with cfg.ignore_type():` context manager, or delete the field and re-add it with the new type (if the ConfigDict is unlocked).
To make changes, you must create a new FrozenConfigDict with the desired modifications, or convert the FrozenConfigDict to a mutable ConfigDict using `.as_configdict()`, make your changes, and then convert it back to a FrozenConfigDict if immutability is still required.
Ensure that any nested dictionaries or ConfigDicts within lists or tuples are converted to FrozenConfigDicts *before* being used in the initialization of the parent FrozenConfigDict. Lists and tuples themselves should only contain other immutable types or already-frozen ConfigDicts.