2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Databricks にて Hyperopt を利用した際に特定の MLflow の experiment へトラッキングする方法

Last updated at Posted at 2023-07-13

概要

Databricks にて Hyperopt を利用した際に特定の MLflow の experiment へトラッキングする方法を共有します。Hyperopt 利用時には、MLflow へトラッキングする際にset_experimentにより experiment を指定することで想定通りにトラッキングできました。

mlflow.set_experiment(experiment_id="1420974677739169")

with mlflow.start_run():
  best_result = fmin(
    fn=objective, 
    space=search_space,
    algo=algo,
    max_evals=32,
    trials=spark_trials)

image.png

Hyperopt 利用時には親子関係でトラッキングが実施されるため、次のリンク先にて記載されている MLflow の仕様により、experiment_id の指定方法によっては想定通りに動作しない場合があります。

本記事では、次のリンク先にあるサンプルノートブックに基づいた検証結果を共有します。サンプルノートブックの一部を修正し、その実行結果を確認しました。

image.png

引用元: hyperopt-sklearn-model-selection - Databricks (microsoft.com)

Hyperopt の詳細を知りたい場合には、次のドキュメントが参考になります。

基本的な Hyperopt 利用時の MLflow へのトラッキング動作の確認

サンプルノートブックの実行結果から基本的な Hyperopt による MLflow へのトラッキングに関する動作を確認します。

サンプルノートブックでは、fminメソッドを実行する際に、mlflow.start_runによりトラッキングが実施されるようになっております。experiment_id を指定していないため、現在のノートブックにトラッキングされます。

image.png

Hyperopt で、チューニング対象の関数をobjectパラメータに指定するのですが、サンプルノートブックでは次のようなコードとなっております。MLflow へのトラッキングが明示されておりませんが、fminメソッドがトラッキング対象となっているため ML モデルの学習時に MLflow へトラッキングするようです。

image.png

サンプルノートブックの実行後、現在のノートブック上の experiment を確認すると、MLflow に対して親子階層でトラッキングされることが確認できました。dashing-sheep-743が親の experiment であり、spiffy-rat-555orderly-finch-409などが子の experiment です。

image.png

親の experiment では、fminメソッド実行時のパラメータや実行時のソースであるノートブックへのリンクが記載されています。

image.png

子の experiment では、モデル学習時のハイパーパラメータやメトリックなどの ML モデルの情報が記載されています。

image.png

指定した experiment にトラッキングする方法

C1. set_experimentメソッドで experiment_id を指定する方法

set_experimentメソッドで experiment_id を指定するため、トラッキング先の experiment を作成後、サンプルノートブックのコードを次のように修正しました。

修正前

with mlflow.start_run():
  best_result = fmin(
    fn=objective, 
    space=search_space,
    algo=algo,
    max_evals=32,
    trials=spark_trials)

修正後

mlflow.set_experiment(experiment_id="1420974677739169")

with mlflow.start_run():
  best_result = fmin(
    fn=objective, 
    space=search_space,
    algo=algo,
    max_evals=32,
    trials=spark_trials)

image.png

ノートブックを実行すると、指定した experiment に親子階層でトラッキングされました。

image.png

想定通りに experiment にトラッキングできない例

E1. fminメソッド実行時に experiment_id を指定する方法

fminメソッド実行時に experiment_id を指定するため、サンプルノートブックのコードを次のように修正しました。

修正前

with mlflow.start_run():
  best_result = fmin(
    fn=objective, 
    space=search_space,
    algo=algo,
    max_evals=32,
    trials=spark_trials)

修正後

with mlflow.start_run(experiment_id="1420974677739169"):
  best_result = fmin(
    fn=objective, 
    space=search_space,
    algo=algo,
    max_evals=32,
    trials=spark_trials)

修正したノートブックを実行すると、fminメソッド実行に関するトラッキングのみ指定した experiment に実施されていました。

image.png

ML モデルの学習に関するトラッキングは、実行したノートブックの experiment に記載されていました。そのため、親子階層でトラッキングされていないため、想定通りにトラッキングできていないと判断しました。

image.png

E2. fminメソッドとobject指定の関数実行時に experiment_id を指定する方法

fminメソッドとobject指定の関数実行時に experiment_id を指定するため、サンプルノートブックのコードを次のように修正しました。

修正前

with mlflow.start_run():
  best_result = fmin(
    fn=objective, 
    space=search_space,
    algo=algo,
    max_evals=32,
    trials=spark_trials)
def objective(params):
    classifier_type = params['type']
    del params['type']
    if classifier_type == 'svm':
        clf = SVC(**params)
    elif classifier_type == 'rf':
        clf = RandomForestClassifier(**params)
    elif classifier_type == 'logreg':
        clf = LogisticRegression(**params)
    else:
        return 0
    accuracy = cross_val_score(clf, X, y_discrete).mean()
    
    # Because fmin() tries to minimize the objective, this function must return the negative accuracy. 
    return {'loss': -accuracy, 'status': STATUS_OK}

修正後

with mlflow.start_run(experiment_id="1420974677739169"):
  best_result = fmin(
    fn=objective, 
    space=search_space,
    algo=algo,
    max_evals=32,
    trials=spark_trials)
def objective(params):
  with mlflow.start_run(experiment_id="1420974677739169"):
    classifier_type = params['type']
    del params['type']
    if classifier_type == 'svm':
        clf = SVC(**params)
    elif classifier_type == 'rf':
        clf = RandomForestClassifier(**params)
    elif classifier_type == 'logreg':
        clf = LogisticRegression(**params)
    else:
        return 0
    accuracy = cross_val_score(clf, X, y_discrete).mean()
    
    # Because fmin() tries to minimize the objective, this function must return the negative accuracy. 
    return {'loss': -accuracy, 'status': STATUS_OK}

修正したノートブックを実行すると、fminメソッド実行に関するトラッキングのみ指定した experiment に実施されていました。

image.png

ML モデルの学習に関するトラッキングは、実行したノートブックの experiment に記載されていました。そのため、親子階層でトラッキングされていないため、想定通りにトラッキングできていないと判断しました。

image.png

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?