本文整理汇总了Python中mlflow.tracking.MlflowClient方法的典型用法代码示例。如果您正苦于以下问题:Python tracking.MlflowClient方法的具体用法?Python tracking.MlflowClient怎么用?Python tracking.MlflowClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mlflow.tracking
的用法示例。
在下文中一共展示了tracking.MlflowClient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _retrieve_mlflow_experiment_id
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def _retrieve_mlflow_experiment_id(name, create=False):
experiment_id = None
if name:
existing_experiment = MlflowClient().get_experiment_by_name(name)
if existing_experiment:
experiment_id = existing_experiment.experiment_id
else:
if create:
experiment_id = mlflow.create_experiment(name)
else:
raise Exception(
'Experiment "{}" not found in {}'.format(
name, mlflow.get_tracking_uri()
)
)
return experiment_id
示例2: __init__
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def __init__(self,
experiment_name: str = 'default',
tracking_uri: Optional[str] = None,
tags: Optional[Dict[str, Any]] = None,
save_dir: Optional[str] = None):
if not _MLFLOW_AVAILABLE:
raise ImportError('You want to use `mlflow` logger which is not installed yet,'
' install it with `pip install mlflow`.')
super().__init__()
if not tracking_uri and save_dir:
tracking_uri = f'file:{os.sep * 2}{save_dir}'
self._mlflow_client = MlflowClient(tracking_uri)
self.experiment_name = experiment_name
self._run_id = None
self.tags = tags
示例3: _wait_for
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def _wait_for(submitted_run_obj):
"""Wait on the passed-in submitted run, reporting its status to the tracking server."""
run_id = submitted_run_obj.run_id
active_run = None
# Note: there's a small chance we fail to report the run's status to the tracking server if
# we're interrupted before we reach the try block below
try:
active_run = tracking.MlflowClient().get_run(run_id) if run_id is not None else None
if submitted_run_obj.wait():
_logger.info("=== Run (ID '%s') succeeded ===", run_id)
_maybe_set_run_terminated(active_run, "FINISHED")
else:
_maybe_set_run_terminated(active_run, "FAILED")
raise ExecutionException("Run (ID '%s') failed" % run_id)
except KeyboardInterrupt:
_logger.error("=== Run (ID '%s') interrupted, cancelling run ===", run_id)
submitted_run_obj.cancel()
_maybe_set_run_terminated(active_run, "FAILED")
raise
示例4: _print_description_and_log_tags
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def _print_description_and_log_tags(self):
_logger.info(
"=== Launched MLflow run as Databricks job run with ID %s."
" Getting run status page URL... ===",
self._databricks_run_id)
run_info = self._job_runner.jobs_runs_get(self._databricks_run_id)
jobs_page_url = run_info["run_page_url"]
_logger.info("=== Check the run's status at %s ===", jobs_page_url)
host_creds = databricks_utils.get_databricks_host_creds(self._job_runner.databricks_profile)
tracking.MlflowClient().set_tag(self._mlflow_run_id,
MLFLOW_DATABRICKS_RUN_URL, jobs_page_url)
tracking.MlflowClient().set_tag(self._mlflow_run_id,
MLFLOW_DATABRICKS_SHELL_JOB_RUN_ID, self._databricks_run_id)
tracking.MlflowClient().set_tag(self._mlflow_run_id,
MLFLOW_DATABRICKS_WEBAPP_URL, host_creds.host)
job_id = run_info.get('job_id')
# In some releases of Databricks we do not return the job ID. We start including it in DB
# releases 2.80 and above.
if job_id is not None:
tracking.MlflowClient().set_tag(self._mlflow_run_id,
MLFLOW_DATABRICKS_SHELL_JOB_ID, job_id)
示例5: test_set_experiment
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def test_set_experiment():
with pytest.raises(TypeError):
mlflow.set_experiment() # pylint: disable=no-value-for-parameter
with pytest.raises(Exception):
mlflow.set_experiment(None)
with pytest.raises(Exception):
mlflow.set_experiment("")
name = "random_exp"
exp_id = mlflow.create_experiment(name)
mlflow.set_experiment(name)
with start_run() as run:
assert run.info.experiment_id == exp_id
another_name = "another_experiment"
mlflow.set_experiment(another_name)
exp_id2 = mlflow.tracking.MlflowClient().get_experiment_by_name(another_name)
with start_run() as another_run:
assert another_run.info.experiment_id == exp_id2.experiment_id
示例6: test_parent_create_run
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def test_parent_create_run():
with mlflow.start_run() as parent_run:
parent_run_id = parent_run.info.run_id
os.environ[_RUN_ID_ENV_VAR] = parent_run_id
with mlflow.start_run() as parent_run:
assert parent_run.info.run_id == parent_run_id
with pytest.raises(Exception, match='To start a nested run'):
mlflow.start_run()
with mlflow.start_run(nested=True) as child_run:
assert child_run.info.run_id != parent_run_id
with mlflow.start_run(nested=True) as grand_child_run:
pass
def verify_has_parent_id_tag(child_id, expected_parent_id):
tags = tracking.MlflowClient().get_run(child_id).data.tags
assert tags[MLFLOW_PARENT_RUN_ID] == expected_parent_id
verify_has_parent_id_tag(child_run.info.run_id, parent_run.info.run_id)
verify_has_parent_id_tag(grand_child_run.info.run_id, child_run.info.run_id)
assert mlflow.active_run() is None
示例7: test_client_registry_operations_raise_exception_with_unsupported_registry_store
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def test_client_registry_operations_raise_exception_with_unsupported_registry_store():
"""
This test case ensures that Model Registry operations invoked on the `MlflowClient`
fail with an informative error message when the registry store URI refers to a
store that does not support Model Registry features (e.g., FileStore).
"""
with TempDir() as tmp:
client = MlflowClient(registry_uri=tmp.path())
expected_failure_functions = [
client._get_registry_client,
lambda: client.create_registered_model("test"),
lambda: client.get_registered_model("test"),
lambda: client.create_model_version("test", "source", "run_id"),
lambda: client.get_model_version("test", 1),
]
for func in expected_failure_functions:
with pytest.raises(MlflowException) as exc:
func()
assert exc.value.error_code == ErrorCode.Name(FEATURE_DISABLED)
示例8: test_models_artifact_repo_init_with_stage_uri
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def test_models_artifact_repo_init_with_stage_uri(
host_creds_mock): # pylint: disable=unused-argument
model_uri = "models:/MyModel/Production"
artifact_location = "dbfs://databricks/mlflow-registry/12345/models/keras-model"
model_version_detailed = ModelVersion("MyModel", "10", "2345671890", "234567890",
"some description", "UserID",
"Production", "source", "run12345")
get_latest_versions_patch = mock.patch.object(MlflowClient, "get_latest_versions",
return_value=[model_version_detailed])
get_model_version_download_uri_patch = mock.patch.object(MlflowClient,
"get_model_version_download_uri",
return_value=artifact_location)
with get_latest_versions_patch, get_model_version_download_uri_patch:
models_repo = ModelsArtifactRepository(model_uri)
assert models_repo.artifact_uri == model_uri
assert isinstance(models_repo.repo, DbfsRestArtifactRepository)
assert models_repo.repo.artifact_uri == artifact_location
示例9: test_metric_name
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def test_metric_name(tmpdir: py.path.local) -> None:
tracking_file_name = "file:{}".format(tmpdir)
metric_name = "my_metric_name"
mlflc = MLflowCallback(tracking_uri=tracking_file_name, metric_name=metric_name)
study = optuna.create_study(study_name="my_study")
study.optimize(_objective_func, n_trials=3, callbacks=[mlflc])
mlfl_client = MlflowClient(tracking_file_name)
experiments = mlfl_client.list_experiments()
experiment = experiments[0]
experiment_id = experiment.experiment_id
run_infos = mlfl_client.list_run_infos(experiment_id)
first_run_id = run_infos[0].run_id
first_run = mlfl_client.get_run(first_run_id)
first_run_dict = first_run.to_dictionary()
assert metric_name in first_run_dict["data"]["metrics"]
示例10: configure
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def configure(
self,
run_uuid,
experiment_name,
tracking_uri,
run_name=None,
always_log_artifacts=False,
create_run=True,
create_experiment=True,
nest_run=True,
):
if mlflow.active_run() and not nest_run:
logger.info('Ending previous MLFlow run: {}.'.format(self.run_uuid))
mlflow.end_run()
self.always_log_artifacts = always_log_artifacts
self._experiment_name = experiment_name
self._run_name = run_name
# MLflow specific
if tracking_uri:
mlflow.set_tracking_uri(tracking_uri)
if run_uuid:
existing_run = MlflowClient().get_run(run_uuid)
if not existing_run and not create_run:
raise FileNotFoundError(
'Run ID {} not found under {}'.format(
run_uuid, mlflow.get_tracking_uri()
)
)
experiment_id = self._retrieve_mlflow_experiment_id(
experiment_name, create=create_experiment
)
return mlflow.start_run(
run_uuid,
experiment_id=experiment_id,
run_name=run_name,
nested=nest_run,
)
示例11: experiment_name
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def experiment_name(self):
# return MlflowClient().get_experiment(self.experiment_id).name
return self._experiment_name
示例12: get_run_id
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def get_run_id(client: MlflowClient, experiment_name: str, model_key: str) -> str:
"""
Get an existing or create a new run for the given model_key and experiment_name.
The model key corresponds to a unique configuration of the model. The corresponding
run must be manually stopped using the `mlflow.tracking.MlflowClient.set_terminated`
method.
Parameters
----------
client: mlflow.tracking.MlflowClient
Client with tracking uri set to AzureML if configured.
experiment_name: str
Name of experiment to log to.
model_key: str
Unique ID of model configuration.
Returns
-------
run_id: str
Unique ID of MLflow run to log to.
"""
experiment = client.get_experiment_by_name(experiment_name)
experiment_id = (
getattr(experiment, "experiment_id")
if experiment
else client.create_experiment(experiment_name)
)
return client.create_run(experiment_id, tags={"model_key": model_key}).info.run_id
示例13: log_machine
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def log_machine(mlflow_client: MlflowClient, run_id: str, machine: Machine):
"""
Send logs to configured MLflow backend
Parameters
----------
mlflow_client: MlflowClient
Client instance to call logging methods from.
run_id: str
Unique ID off MLflow Run to log to.
machine: Machine
Machine to log with MlflowClient.
"""
# Log machine metrics and params
for batch_kwargs in batch_log_items(*get_machine_log_items(machine)):
mlflow_client.log_batch(run_id, **batch_kwargs)
# Send configs as JSON artifacts
try:
with tempfile.TemporaryDirectory(dir="./") as tmp_dir:
fp = os.path.join(tmp_dir, f"metadata.json")
with open(fp, "w") as fh:
json.dump(machine.to_dict(), fh, cls=MachineEncoder)
mlflow_client.log_artifacts(run_id=run_id, local_dir=tmp_dir)
# Map to MlflowLoggingError for coding errors in the model builder
except Exception as e:
raise MlflowLoggingError(e)
示例14: _init
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def _init(self):
from mlflow.tracking import MlflowClient
client = MlflowClient()
run = client.create_run(self.config.get("mlflow_experiment_id"))
self._run_id = run.info.run_id
for key, value in self.config.items():
client.log_param(self._run_id, key, value)
self.client = client
示例15: experiment
# 需要导入模块: from mlflow import tracking [as 别名]
# 或者: from mlflow.tracking import MlflowClient [as 别名]
def experiment(self) -> MlflowClient:
r"""
Actual MLflow object. To use mlflow features in your
:class:`~pytorch_lightning.core.lightning.LightningModule` do the following.
Example::
self.logger.experiment.some_mlflow_function()
"""
return self._mlflow_client