Penyetelan Hyperparameter dengan DVC

CI/CD untuk Machine Learning

Ravi Bhadauria

Machine Learning Engineer

Alur kerja penyetelan hyperparameter

  • Penyetelan hyperparameter

    • Input: Rentang penelusuran parameter
    • Output: Parameter terbaik
  • Pelatihan

    • Input: Parameter terbaik
    • Output: Metrik dan plot (sudah dibahas)
  • Loose coupling untuk pelatihan mandiri

    • Penyetelan hyperparameter memadai namun tidak wajib
  • Kedua job bergantung pada dataset

# 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 untuk Machine Learning

Perubahan kode pelatihan

Perubahan kode pelatihan
# 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 untuk Machine Learning

Penyetelan Hyperparameter dengan 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 untuk Machine Learning

Perubahan DVC YAML

Penyetelan Hyperparameter
stages:
  preprocess: ...
  train: ...
  hp_tune:
    cmd: python hp_tuning.py
    deps:
    - processed_dataset/weather.csv
    - hp_config.json
    - hp_tuning.py
    outs: # Tidak melacak parameter terbaik
      - hp_tuning_results.md:
          cache: false
Pelatihan
stages:
  preprocess: ...
  hp_tune: ...
  train:
    cmd: python train.py
    deps:
    - processed_dataset/weather.csv
    - rfc_best_params.json # Parameter terbaik
    - train.py
    metrics:
      - metrics.json:
          cache: false
CI/CD untuk Machine Learning

Menjalankan tahap individual

  • Tahap dapat dijalankan terpisah dvc repro <stage_name>

  • Paksa jalankan tahap penyetelan hyperparameter dvc repro -f hp_tune

    • Memastikan berkas parameter terbaik diperbarui
  • Pelatihan dapat dijalankan dengan dvc repro train

  • Kedua tahap memicu praproses sebagai dependensi

CI/CD untuk Machine Learning

Keluaran Run Hyperparameter

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

Perubahan pada skrip penyetelan hyperparameter Python

# 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 untuk Machine Learning

Ringkasan

  • Rute penyetelan hyperparameter

    • Nama branch hp_tune/<some-string>
    • Ubah konfigurasi pencarian
    • Buka PR secara manual
      • Memaksa menjalankan pipeline DVC dvc repro -f hp_tune
      • Menggunakan cml pr create untuk membuat PR pelatihan baru dengan parameter terbaik
    • Force push commit ke PR pelatihan untuk memulai job pelatihan model
  • Rute manual

    • Nama branch train/<some-string>
    • Edit berkas parameter terbaik dan commit
    • Buka PR manual untuk memulai job pelatihan model
CI/CD untuk Machine Learning

Ayo berlatih!

CI/CD untuk Machine Learning

Preparing Video For Download...