6

mlflow.active_run() returns nothing so I can't just use current_rui_id = mlflow.active_run().info.run_id

I have to get run_id inside of this construction for being able to continue logging parameters, metrics and artifacts inside of another block but for the same model:

with mlflow.start_run(run_name="test_ololo"):

    """ 
       fitting a model here ...
    """

    for name, val in metrics:
        mlflow.log_metric(name, np.float(val))

    # Log our parameters into mlflow
    for k, v in params.items():
        mlflow.log_param(key=k, value=v)

    pytorch.log_model(learn.model, f'model')
    mlflow.log_artifact('./outputs/fig.jpg')

I have to get current run_id to continue training inside the same run

with mlflow.start_run(run_id="215d3a71925a4709a9b694c45012988a"):

    """
       fit again
       log_metrics
    """

    pytorch.log_model(learn.model, f'model')
    mlflow.log_artifact('./outputs/fig2.jpg')
OnlyDryClean Codzy
  • 943
  • 1
  • 10
  • 19

3 Answers3

9
with mlflow.start_run(run_name="test_ololo") as run:

    run_id = run.info.run_id
DataJanitor
  • 1,276
  • 1
  • 8
  • 19
OnlyDryClean Codzy
  • 943
  • 1
  • 10
  • 19
  • 2
    Although this code might solve the problem, a good answer should also explain how it helps and what the code does. – BDL Jan 21 '20 at 10:55
6

You can try this code snippet:

import mlflow
mlflow.start_run()
run = mlflow.active_run()
print("Active run_id: {}".format(run.info.run_id))
mlflow.end_run()
DataJanitor
  • 1,276
  • 1
  • 8
  • 19
nikey_es
  • 61
  • 1
  • 2
  • Bonus: One should use `mlflow.start_run()` inside a `with`-block so that it is "cleaned up" if an error occurs and it does not reach `end_run()`. See: https://stackoverflow.com/questions/1369526/what-is-the-python-keyword-with-used-for – DataJanitor May 09 '23 at 12:34
5

The above should work and is in fact the best way to get a hold of active run inside of the with mlflow.start_run() block.

For completeness, mlflow.active_run().info.run_id will also work if executed inside of the with block. The with block will end mlflow run on exit, so there is no active run once the block exited.

DataJanitor
  • 1,276
  • 1
  • 8
  • 19
Tomas
  • 189
  • 1