Hyperparametertuning met DVC

CI/CD voor Machine Learning

Ravi Bhadauria

Machine Learning Engineer

Workflow voor hyperparametertuning

  • Hyperparametertuning

    • Input: Bereiken voor sweep
    • Output: Beste parameters
  • Training

    • Input: Beste parameters
    • Output: Metrics en plots (al behandeld)
  • Losse koppeling voor onafhankelijk trainen

    • Tuning is voldoende maar niet vereist
  • Beide jobs zijn datasetafhankelijk

# Contents of hp configuration
{
    "n_estimators": [2, 4, 5],
    "max_depth": [10, 20, 50],
    "random_state": [1993]
}
# Contents of best parameters
{
    "n_estimators": 5,
    "max_depth": 20,
    "random_state": 1993
}
CI/CD voor Machine Learning

Wijzigingen in trainingscode

Wijzigingen in trainingscode
# Load hyperparameters from the JSON file
with open("rfc_best_params.json", "r") as params_file:
  rfc_params = json.load(params_file)

# Define and train model model = RandomForestClassifier(**rfc_params) model.fit(X_train, y_train)
CI/CD voor Machine Learning

Hyperparametertuning met GridSearch

# Define the model and hyperparameter search space
model = RandomForestClassifier()
param_grid = json.load(open("hp_config.json", "r"))

# Perform GridSearch with five fold CV grid_search = GridSearchCV(model, param_grid, cv=5) grid_search.fit(X_train, y_train)
# Get the best hyperparameters best_params = grid_search.best_params_ with open("rfc_best_params.json", "w") as outfile: json.dump(best_params, outfile)
CI/CD voor Machine Learning

Wijzigingen in DVC YAML

Hyperparametertuning
stages:
  preprocess: ...
  train: ...
  hp_tune:
    cmd: python hp_tuning.py
    deps:
    - processed_dataset/weather.csv
    - hp_config.json
    - hp_tuning.py
    outs: # Best parameters niet tracken
      - hp_tuning_results.md:
          cache: false
Training
stages:
  preprocess: ...
  hp_tune: ...
  train:
    cmd: python train.py
    deps:
    - processed_dataset/weather.csv
    - rfc_best_params.json # Beste parameters
    - train.py
    metrics:
      - metrics.json:
          cache: false
CI/CD voor Machine Learning

Losse stages starten

  • Stages kun je los starten met dvc repro <stage_name>

  • Forceer de tuning-stage dvc repro -f hp_tune

    • Zorgt dat het bestand met beste parameters wordt geüpdatet
  • Training start je met dvc repro train

  • Beide stages triggeren preprocessing als dependency

CI/CD voor Machine Learning

Output van tuning-run

mean_test_score std_test_score max_depth n_estimators random_state
0.999733 0.000413118 20 5 1993
0.999307 0.000574418 50 5 1993
0.99888 0.000617378 10 5 1993
0.997813 0.00117333 10 4 1993

Wijzigingen in Python-script voor hyperparametertuning

# Save the results of hyperparameter tuning
cv_results = pd.DataFrame(grid_search.cv_results_)
markdown_table = cv_results.to_markdown(index=False)
with open("hp_tuning_results.md", "w") as markdown_file:
  markdown_file.write(markdown_table)
CI/CD voor Machine Learning

Samenvatting

  • Route hyperparametertuning

    • Branched naam hp_tune/<some-string>
    • Pas de zoekconfig aan
    • Open handmatig een PR
      • Forceert DVC-pipeline dvc repro -f hp_tune
      • Gebruikt cml pr create om een nieuwe training-PR te maken met beste parameters
    • Force-push een commit naar de training-PR om de trainingjob te starten
  • Handmatige route

    • Branchnaam train/<some-string>
    • Bewerk het bestand met beste parameters en commit
    • Open handmatig een PR om de trainingjob te starten
CI/CD voor Machine Learning

Laten we oefenen!

CI/CD voor Machine Learning

Preparing Video For Download...