본문 바로가기

Tech Stack/MLflow

MLOps - 학습 코드 로깅 추가

set mlflow

 

import mlflow
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

#
# set mlflow
#
mlflow.set_tracking_uri("http://0.0.0.0:5001")
mlflow.set_experiment("tutorial")

 

mlflow가 중복되지 않게 with문을 써줌

그리고 params를 지정

with mlflow.start_run():
    #
    # log parameter
    #
    params = {"n_estimators": 100, "max_depth": 5}
    mlflow.log_params(params)

 

log_params에서 가져와야 하기 때문에

#
# train model
#
clf = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=2024)
clf.fit(X_train, y_train)

 

다음 코드에서

    #
    # train model
    #
    clf = RandomForestClassifier(n_estimators=params["n_estimators"], max_depth=params["max_depth"], random_state=2024)
    clf.fit(X_train, y_train)

다음과 같이 params값을 바꿈

 

마지막으로 메트릭스를 로깅 해줘야 함

    #
    # log metrics
    #
    print("Accuracy score is {:.4f}".format(acc_score))
    mlflow.log_metric("accuracy", acc_score)

 

실험 로깅 완료.

 

 

'Tech Stack > MLflow' 카테고리의 다른 글

MLOps - 교차검증  (0) 2024.02.23
MLOps - HPO 반영  (0) 2024.02.22
MLOps - Optuna  (0) 2024.02.22
MLOps - Hyperparameter Optimization  (0) 2024.02.22
MLOps - MLflow 실행 및 docker compose  (0) 2024.02.22