Class MlflowContext

java.lang.Object
org.mlflow.tracking.MlflowContext

public class MlflowContext
extends java.lang.Object
Main entrypoint used to start MLflow runs to log to. This is a higher level interface than MlflowClient and provides convenience methods to keep track of active runs and to set default tags on runs which are created through MlflowContext On construction, MlflowContext will choose a default experiment ID to log to depending on your environment. To log to a different experiment, use setExperimentId(String) or setExperimentName(String)

For example:

   // Uses the URI set in the MLFLOW_TRACKING_URI environment variable.
   // To use your own tracking uri set it in the call to "new MlflowContext("tracking-uri")"
   MlflowContext mlflow = new MlflowContext();
   ActiveRun run = mlflow.startRun("run-name");
   run.logParam("alpha", "0.5");
   run.logMetric("MSE", 0.0);
   run.endRun();
   
  • Constructor Summary

    Constructors 
    Constructor Description
    MlflowContext()
    Constructs a MlflowContext with a MlflowClient based on the MLFLOW_TRACKING_URI environment variable.
    MlflowContext​(java.lang.String trackingUri)
    Constructs a MlflowContext which points to the specified trackingUri.
    MlflowContext​(MlflowClient client)
    Constructs a MlflowContext which points to the specified trackingUri.
  • Method Summary

    Modifier and Type Method Description
    MlflowClient getClient()
    Returns the client used to log runs.
    java.lang.String getExperimentId()
    Returns the experiment ID we are logging to.
    MlflowContext setExperimentId​(java.lang.String experimentId)
    Sets the experiment to log runs to by ID.
    MlflowContext setExperimentName​(java.lang.String experimentName)
    Sets the experiment to log runs to by name.
    ActiveRun startRun()
    Starts a MLflow run without a name.
    ActiveRun startRun​(java.lang.String runName)
    Starts a MLflow run.
    ActiveRun startRun​(java.lang.String runName, java.lang.String parentRunId)
    Like startRun(String) but sets the mlflow.parentRunId tag in order to create nested runs.
    void withActiveRun​(java.lang.String runName, java.util.function.Consumer<ActiveRun> activeRunFunction)
    Like withActiveRun(Consumer) with an explicity run name.
    void withActiveRun​(java.util.function.Consumer<ActiveRun> activeRunFunction)
    Like startRun(String) but will terminate the run after the activeRunFunction is executed.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • MlflowContext

      public MlflowContext()
      Constructs a MlflowContext with a MlflowClient based on the MLFLOW_TRACKING_URI environment variable.
    • MlflowContext

      public MlflowContext​(java.lang.String trackingUri)
      Constructs a MlflowContext which points to the specified trackingUri.
      Parameters:
      trackingUri - The URI to log to.
    • MlflowContext

      public MlflowContext​(MlflowClient client)
      Constructs a MlflowContext which points to the specified trackingUri.
      Parameters:
      client - The client used to log runs.
  • Method Details

    • getClient

      public MlflowClient getClient()
      Returns the client used to log runs.
      Returns:
      the client used to log runs.
    • setExperimentName

      public MlflowContext setExperimentName​(java.lang.String experimentName) throws java.lang.IllegalArgumentException
      Sets the experiment to log runs to by name.
      Parameters:
      experimentName - the name of the experiment to log runs to.
      Throws:
      java.lang.IllegalArgumentException - if the experiment name does not match an existing experiment
    • setExperimentId

      public MlflowContext setExperimentId​(java.lang.String experimentId)
      Sets the experiment to log runs to by ID.
      Parameters:
      experimentId - the id of the experiment to log runs to.
    • getExperimentId

      public java.lang.String getExperimentId()
      Returns the experiment ID we are logging to.
      Returns:
      the experiment ID we are logging to.
    • startRun

      public ActiveRun startRun()
      Starts a MLflow run without a name. To log data to newly created MLflow run see the methods on ActiveRun. MLflow runs should be ended using ActiveRun.endRun()
      Returns:
      An ActiveRun object to log data to.
    • startRun

      public ActiveRun startRun​(java.lang.String runName)
      Starts a MLflow run. To log data to newly created MLflow run see the methods on ActiveRun. MLflow runs should be ended using ActiveRun.endRun()
      Parameters:
      runName - The name of this run. For display purposes only and is stored in the mlflow.runName tag.
      Returns:
      An ActiveRun object to log data to.
    • startRun

      public ActiveRun startRun​(java.lang.String runName, java.lang.String parentRunId)
      Like startRun(String) but sets the mlflow.parentRunId tag in order to create nested runs.
      Parameters:
      runName - The name of this run. For display purposes only and is stored in the mlflow.runName tag.
      parentRunId - The ID of this run's parent
      Returns:
      An ActiveRun object to log data to.
    • withActiveRun

      public void withActiveRun​(java.util.function.Consumer<ActiveRun> activeRunFunction)
      Like startRun(String) but will terminate the run after the activeRunFunction is executed. For example
         mlflowContext.withActiveRun((activeRun -> {
           activeRun.logParam("layers", "4");
         }));
         
      Parameters:
      activeRunFunction - A function which takes an ActiveRun and logs data to it.
    • withActiveRun

      public void withActiveRun​(java.lang.String runName, java.util.function.Consumer<ActiveRun> activeRunFunction)
      Like withActiveRun(Consumer) with an explicity run name.
      Parameters:
      runName - The name of this run. For display purposes only and is stored in the mlflow.runName tag.
      activeRunFunction - A function which takes an ActiveRun and logs data to it.