Skip to main content

provides a convenient base configuration class for machine learning projects

Project description

easy-ml-config

Super lightweight library to provide a simple base configuration class to maintain consistency and reproducibility for machine learning projects. There is only one module ml_config.configuration that contains the BaseConfig class, which allows you to:

  • Define complex and nested configuration classes.
  • Export/Import your configuration to a dictionary or a yaml file with simply to_dict(), from_dict, to_yaml and from_yaml.
from dataclasses import dataclass
from easy_ml_config import BaseConfig

@dataclass
class ValidationConfig(BaseConfig):
    batch_size: int
    upload_to_server: bool = False

@dataclass
class TrainConfig(BaseConfig):
    num_epochs: int
    batch_size: int
    learning_rate: float
    validation_config: ValidationConfig | None = None

class ModelConfig(BaseConfig):
    num_layer: int

@dataclass
class MyExpConfig(BaseConfig):
    model: MyModelConfig
    train: TrainConfig
    exp_id: str

# Initialize from a dictionary
exp_config = MyExpConfig.from_dict({
    "model": {
        "num_layer": 3,
    },
    "train": {
        "num_epochs": 10,
        "batch_size": 32,
        "learning_rate": 0.001,
        "validation_config": {
            "batch_size": 16,
            "upload_to_server": False
        } # optional
    },
    "exp_id": "exp_1"
})

# Export to a yaml file
exp_config.to_yaml("exp_config_001.yaml")

# Initialize from a yaml file
exp_config = MyExpConfig.from_yaml("exp_config_001.yaml")

The yaml file will look like this:

model:
    num_layer: 3
train:
    num_epochs: 10
    batch_size: 32
    learning_rate: 0.001
    validation_config:
        batch_size: 16
        upload_to_server: false
exp_id: exp_1

Installation

pip install easy-ml-config

Usage

Start with defining your own configuration by inheriting from BaseConfig. Once you have that, you can utilize all the predefined functionalities of BaseConfig.

Define Your Configuration

from dataclasses import dataclass

from easy_ml_config import BaseConfig


@dataclass
class MyModelConfig(BaseConfig):
    num_layer: int

@dataclass
class MyExpConfig(BaseConfig):
    model: MyModelConfig
    batch_size: int
    learning_rate: float

Instantiate Your Configuration

When you are running an experiment with a specific settings. Initialize your configurations.

Initialize from A Dictionary

def main():
    nested_args = {
        "model": {
            "num_layer": 3,
        },
        "batch_size": 32,
        "learning_rate": 0.001,
    }
    config = MyExpConfig.from_dict(nested_args)
    # model = MyModel(num_layer=config.model.num_layer)

if __name__ == "__main__":
    main()

Initialize from A YAML File

# config.yaml
model:
  num_layer: 3
batch_size: 32
learning_rate: 0.001
config = MyExpConfig.from_yaml("config.yaml")

Output Your Configuration

Often you might want to save your configuration to a file. You can do that easily with the to_dict and to_yaml methods.

# Save to a dictionary
config_dict = config.to_dict()
# Save to a yaml file
config.to_yaml("config.yaml")

The exported dictionary and yaml file can be directly used again to recreate the Configurations.

# Load from a dictionary
config = MyExpConfig.from_dict(config_dict)
# Load from a yaml file
config = MyExpConfig.from_yaml("config.yaml")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easy_ml_config-1.1.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

easy_ml_config-1.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file easy_ml_config-1.1.0.tar.gz.

File metadata

  • Download URL: easy_ml_config-1.1.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for easy_ml_config-1.1.0.tar.gz
Algorithm Hash digest
SHA256 f0e2950b7acc8df84ed712d53e89808f4b32535ba8caa1e18ad1ba1d5ef70f30
MD5 f6ebae8b5732893b7da55b88601954d9
BLAKE2b-256 107ea9201abe45da583dbfa557a27f0a9ffd3b7f65dd7b1e280f415cb1c63d40

See more details on using hashes here.

File details

Details for the file easy_ml_config-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: easy_ml_config-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for easy_ml_config-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 72943274db4964dae4d2754a000e12773c1992ace1ee243808e55341b84765de
MD5 9cac313f08c3c3152b39a6dcad03633a
BLAKE2b-256 8277d499b3d377521a027b89239f859982360f135827f03d7ebecdb84ea49c86

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page