keras 如何使用自定义参数进行mlflow自动记录

wqnecbli  于 2023-05-23  发布在  其他
关注(0)|答案(1)|浏览(146)

我尝试同时使用mlflow.keras.autologmlflow.log_parammlflow v 1.22.0)记录我的ML试验。但是,唯一记录的东西是autolog的产品,而不是log_param的产品。

experiment = mlf_client.get_experiment_by_name(experiment_name)
with mlflow.start_run(experiment_id=experiment.experiment_id):
    mlflow.keras.autolog(log_input_examples=True)
    mlflow.log_param('batch_size', self.batch_size)
    mlflow.log_param('training_set_size', len(kwargs['training_ID_list']))
    mlflow.log_param('testing_set_size', len(kwargs['testing_ID_list']))
    
    history = self.train_NN_model(**kwargs)

我知道我可以使用log_paramlog_model来保存模型本身,但是这样我就丢失了一些autolog可以自动为我记录的有用内容(例如,模型摘要)。
是否可以使用**autolog自定义**参数进行日志记录?

qvtsj1bj

qvtsj1bj1#

是的,你可以。让我通过改编mlflow文档中的示例来演示这一点

from pprint import pprint
import numpy as np
from sklearn.linear_model import LinearRegression
import mlflow
from mlflow import MlflowClient

def fetch_logged_data(run_id):
    client = MlflowClient()
    data = client.get_run(run_id).data
    tags = {k: v for k, v in data.tags.items() if not k.startswith("mlflow.")}
    artifacts = [f.path for f in client.list_artifacts(run_id, "model")]
    return data.params, data.metrics, tags, artifacts

# enable autologging
mlflow.sklearn.autolog()

# prepare training data
X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
y = np.dot(X, np.array([1, 2])) + 3

# train a model
model = LinearRegression()
with mlflow.start_run() as run:
    model.fit(X, y)
    mlflow.log_param('my-param',1)
    mlflow.log_metric('my-metric',2)

# fetch logged data
params, metrics, tags, artifacts = fetch_logged_data(run.info.run_id)

pprint(params)
# {'copy_X': 'True',
#  'fit_intercept': 'True',
#   my-param': '1',
#  'n_jobs': 'None',
#  'normalize': 'False'}

pprint(metrics)
# {'my-metric': 2.0,
#  'training_score': 1.0,
#  'training_mean_absolute_error': 2.220446049250313e-16,
#  'training_mean_squared_error': 1.9721522630525295e-31,
#  'training_r2_score': 1.0,
#  'training_root_mean_squared_error': 4.440892098500626e-16}

pprint(tags)
# {'estimator_class': 'sklearn.linear_model._base.LinearRegression',
#  'estimator_name': 'LinearRegression'}

pprint(artifacts)
# ['model/MLmodel', 'model/conda.yaml', 'model/model.pkl']

请参阅我作为要点分享的this fully working example

相关问题