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 aMlflowContext
with a MlflowClient based on the MLFLOW_TRACKING_URI environment variable.MlflowContext(java.lang.String trackingUri)
Constructs aMlflowContext
which points to the specified trackingUri.MlflowContext(MlflowClient client)
Constructs aMlflowContext
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)
LikestartRun(String)
but sets themlflow.parentRunId
tag in order to create nested runs.void
withActiveRun(java.lang.String runName, java.util.function.Consumer<ActiveRun> activeRunFunction)
LikewithActiveRun(Consumer)
with an explicity run name.void
withActiveRun(java.util.function.Consumer<ActiveRun> activeRunFunction)
LikestartRun(String)
but will terminate the run after the activeRunFunction is executed.
-
Constructor Details
-
MlflowContext
public MlflowContext()Constructs aMlflowContext
with a MlflowClient based on the MLFLOW_TRACKING_URI environment variable. -
MlflowContext
public MlflowContext(java.lang.String trackingUri)Constructs aMlflowContext
which points to the specified trackingUri.- Parameters:
trackingUri
- The URI to log to.
-
MlflowContext
Constructs aMlflowContext
which 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
ActiveRun
object 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
ActiveRun
object to log data to.
-
startRun
LikestartRun(String)
but sets themlflow.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
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 anActiveRun
and 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 anActiveRun
and logs data to it.
-