Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 41 ยท ML Lifecycle and Pipelines

Configuration and Parameters

Keep hyperparameters and paths out of code: use config files, typed dataclasses, defaults and overrides, validate them early, and store the exact config with every run.

Look at any research notebook and you will find magic numbers: a learning rate typed into a cell, a file path to someone's laptop, a threshold changed "just to try". Each hard-coded value is a decision that is invisible to everyone else. Configuration management moves those decisions into one reviewed, versioned place that the code reads.

Good configuration gives you three things: you can change an experiment without editing code, you can reproduce a run by loading its config, and you can see in a pull request exactly what changed.

Code versus configuration

What goes where

Code Logic that rarely changes.
Parameters Hyperparameters, thresholds.
Paths and names Datasets, output folders.
Secrets Keys live in the environment.

Never commit secrets to Git.

1 / 2

Rule of thumb: values that differ between experiments or environments belong in configuration. Passwords and tokens belong in environment variables or a secret manager, never in the config file you commit.

A config as a typed object

A plain dictionary has no safety net: a typo like learnng_rate silently becomes a new key. A dataclass gives names, types and defaults.

Python A typed config with validation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Bad values and misspelled keys fail immediately, at the start of the run, instead of an hour into training.

Defaults, files and overrides

A common layering, from weakest to strongest:

  1. defaults in code,
  2. a config file (YAML or JSON) checked into Git,
  3. environment-specific overrides (dev, staging, prod),
  4. command-line overrides for one-off experiments.
Python Layered configuration

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Later layers win. Checking for unknown keys catches typos in any layer.

Config files in practice

YAML is popular because it reads well and allows comments. The Python standard library reads JSON and TOML; YAML needs the PyYAML package. A typical config.yaml (read-only example):

YAML
seed: 42
data:
  path: data/churn.parquet
  test_size: 0.2
model:
  type: random_forest
  n_estimators: 300
  max_depth: 8

Nested sections keep large configs readable. Tools such as Hydra, OmegaConf and Pydantic Settings add composition, interpolation and validation. For small projects, a dataclass plus a JSON or YAML file is enough.

Save the config with the run

The config that actually ran is part of the model's provenance. Store the final merged config next to the model and in your experiment tracker, and compute a short hash to compare configs quickly.

Python Hash a config to compare runs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Sorting the keys makes the hash independent of key order, so equal configs always share an id.

Common mistakes

  • Secrets in config files. Use environment variables (os.environ["API_KEY"]) and keep .env files out of Git.
  • Paths from your laptop. Use relative paths or a data root setting.
  • Too many options. Every knob is a thing to test. Expose only what really varies.
  • Silent defaults. Log the final config at the start of every run.
  • Mixing config and state. A config describes intent; do not write results back into it.

If a value affects model behavior and is not in the config or run record, you cannot reproduce the model.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Where should API keys and passwords be stored?

    Secrets must never be committed to version control.
  2. 2. What advantage does a frozen dataclass config have over a plain dict?

    A misspelled argument raises an error instead of silently creating a new key.
  3. 3. In layered configuration, which source usually has the highest priority?

    One-off overrides should beat files and defaults.
  4. 4. Why sort keys before hashing a config?

    Canonical ordering gives a stable fingerprint.

Summary

  • Keep hyperparameters, paths and thresholds in versioned configuration, not scattered in code.
  • Use typed objects (dataclasses, Pydantic) to validate early and catch typos.
  • Layer defaults, files, environment overrides and command-line options.
  • Store the final config with every run and hash it to compare runs.
  • Never commit secrets.

Next: Data Versioning and Fingerprints