DVC によるハイパーパラメータチューニング

Machine Learning のための CI/CD

Ravi Bhadauria

Machine Learning Engineer

ハイパーパラメータチューニングのワークフロー

  • ハイパーパラメータチューニング

    • 入力: パラメータ探索範囲
    • 出力: 最適なパラメータ
  • トレーニング

    • 入力: 最適なパラメータ
    • 出力: メトリクスとプロット(説明済み)
  • 独立したトレーニングのための疎結合

    • ハイパーパラメータチューニングは十分条件だが必須ではありません
  • どちらのジョブもデータセットに依存します

# 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
}
Machine Learning のための CI/CD

トレーニングコードの変更

Python ハイパーパラメータチューニングスクリプトの変更
# 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)
Machine Learning のための CI/CD

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)
Machine Learning のための CI/CD

DVC YAML の変更

ハイパーパラメータチューニング
stages:
  preprocess: ...
  train: ...
  hp_tune:
    cmd: python hp_tuning.py
    deps:
    - processed_dataset/weather.csv
    - hp_config.json
    - hp_tuning.py
    outs: # Not tracking best parameters
      - hp_tuning_results.md:
          cache: false
トレーニング
stages:
  preprocess: ...
  hp_tune: ...
  train:
    cmd: python train.py
    deps:
    - processed_dataset/weather.csv
    - rfc_best_params.json # Best parameters
    - train.py
    metrics:
      - metrics.json:
          cache: false
Machine Learning のための CI/CD

個別のステージを実行する

  • 各ステージは dvc repro <stage_name> で個別に実行できます

  • ハイパーパラメータチューニングステージを強制実行するには dvc repro -f hp_tune を使用します

    • 最適なパラメータファイルが必ず更新されます
  • トレーニングは dvc repro train で実行できます

  • 両方のステージは依存関係として前処理ステップをトリガーします

Machine Learning のための CI/CD

ハイパーパラメータ実行の出力

mean_test_score std_test_score maxdepth nestimators 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

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)
Machine Learning のための CI/CD

まとめ

  • ハイパーパラメータチューニングのルート

    • ブランチ名 hp_tune/<some-string>
    • 探索設定を変更する
    • 手動で PR を開く
      • DVC パイプラインを強制実行 dvc repro -f hp_tune
      • cml pr create を使用して最適なパラメータを含む新しいトレーニング PR を作成する
    • トレーニング PR にコミットを強制プッシュしてモデルトレーニングジョブを開始する
  • 手動ルート

    • ブランチ名 train/<some-string>
    • 最適なパラメータファイルを編集してコミットする
    • 手動で PR を開いてモデルトレーニングジョブを開始する
Machine Learning のための CI/CD

練習しましょう!

Machine Learning のための CI/CD

Preparing Video For Download...