ML Collections

1.1.0 · active · verified Sat Apr 11

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.

Warnings

Install

Imports

Quickstart

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.

from ml_collections import config_dict

# Create a ConfigDict
cfg = config_dict.ConfigDict()

# Assign values with dot notation
cfg.learning_rate = 0.001
cfg.optimizer = 'Adam'
cfg.model = config_dict.ConfigDict()
cfg.model.name = 'ResNet50'
cfg.model.num_layers = 50

# Access values
print(f"Learning rate: {cfg.learning_rate}")
print(f"Model name: {cfg.model.name}")

# ConfigDicts are type-safe (mostly)
try:
    cfg.learning_rate = 'high' # This will raise a TypeError
except TypeError as e:
    print(f"Caught expected error: {e}")

# Integer can be assigned to float fields
cfg.weight_decay = 1e-5
cfg.weight_decay = 0 # This works as int -> float conversion is allowed
print(f"Weight decay: {cfg.weight_decay}")

view raw JSON →