Package org.mlflow.tracking
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 aMlflowContextwith a MlflowClient based on the MLFLOW_TRACKING_URI environment variable.MlflowContext(java.lang.String trackingUri)Constructs aMlflowContextwhich points to the specified trackingUri.MlflowContext(MlflowClient client)Constructs aMlflowContextwhich points to the specified trackingUri. -
Method Summary
Modifier and Type Method Description MlflowClientgetClient()Returns the client used to log runs.java.lang.StringgetExperimentId()Returns the experiment ID we are logging to.MlflowContextsetExperimentId(java.lang.String experimentId)Sets the experiment to log runs to by ID.MlflowContextsetExperimentName(java.lang.String experimentName)Sets the experiment to log runs to by name.ActiveRunstartRun()Starts a MLflow run without a name.ActiveRunstartRun(java.lang.String runName)Starts a MLflow run.ActiveRunstartRun(java.lang.String runName, java.lang.String parentRunId)LikestartRun(String)but sets themlflow.parentRunIdtag in order to create nested runs.voidwithActiveRun(java.lang.String runName, java.util.function.Consumer<ActiveRun> activeRunFunction)LikewithActiveRun(Consumer)with an explicity run name.voidwithActiveRun(java.util.function.Consumer<ActiveRun> activeRunFunction)LikestartRun(String)but will terminate the run after the activeRunFunction is executed.
-
Constructor Details
-
MlflowContext
public MlflowContext()Constructs aMlflowContextwith a MlflowClient based on the MLFLOW_TRACKING_URI environment variable. -
MlflowContext
public MlflowContext(java.lang.String trackingUri)Constructs aMlflowContextwhich points to the specified trackingUri.- Parameters:
trackingUri- The URI to log to.
-
MlflowContext
Constructs aMlflowContextwhich points to the specified trackingUri.- Parameters:
client- The client used to log runs.
-
-
Method Details
-
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.IllegalArgumentExceptionSets 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
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
Starts a MLflow run without a name. To log data to newly created MLflow run see the methods onActiveRun. MLflow runs should be ended usingActiveRun.endRun()- Returns:
- An
ActiveRunobject to log data to.
-
startRun
Starts a MLflow run. To log data to newly created MLflow run see the methods onActiveRun. MLflow runs should be ended usingActiveRun.endRun()- Parameters:
runName- The name of this run. For display purposes only and is stored in the mlflow.runName tag.- Returns:
- An
ActiveRunobject to log data to.
-
startRun
LikestartRun(String)but sets themlflow.parentRunIdtag 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
ActiveRunobject to log data to.
-
withActiveRun
LikestartRun(String)but will terminate the run after the activeRunFunction is executed. For examplemlflowContext.withActiveRun((activeRun -> { activeRun.logParam("layers", "4"); }));- Parameters:
activeRunFunction- A function which takes anActiveRunand logs data to it.
-
withActiveRun
public void withActiveRun(java.lang.String runName, java.util.function.Consumer<ActiveRun> activeRunFunction)LikewithActiveRun(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 anActiveRunand logs data to it.
-