Package org.mlflow.tracking
Class MlflowContext
- java.lang.Object
- 
- org.mlflow.tracking.MlflowContext
 
- 
 public class MlflowContext extends java.lang.ObjectMain entrypoint used to start MLflow runs to log to. This is a higher level interface thanMlflowClientand provides convenience methods to keep track of active runs and to set default tags on runs which are created throughMlflowContextOn construction, MlflowContext will choose a default experiment ID to log to depending on your environment. To log to a different experiment, usesetExperimentId(String)orsetExperimentName(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 SummaryConstructors 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 SummaryAll Methods Instance Methods Concrete Methods 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 Detail- 
MlflowContextpublic MlflowContext() Constructs aMlflowContextwith a MlflowClient based on the MLFLOW_TRACKING_URI environment variable.
 - 
MlflowContextpublic MlflowContext(java.lang.String trackingUri) Constructs aMlflowContextwhich points to the specified trackingUri.- Parameters:
- trackingUri- The URI to log to.
 
 - 
MlflowContextpublic MlflowContext(MlflowClient client) Constructs aMlflowContextwhich points to the specified trackingUri.- Parameters:
- client- The client used to log runs.
 
 
- 
 - 
Method Detail- 
getClientpublic MlflowClient getClient() Returns the client used to log runs.- Returns:
- the client used to log runs.
 
 - 
setExperimentNamepublic 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
 
 - 
setExperimentIdpublic 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.
 
 - 
getExperimentIdpublic java.lang.String getExperimentId() Returns the experiment ID we are logging to.- Returns:
- the experiment ID we are logging to.
 
 - 
startRunpublic ActiveRun 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.
 
 - 
startRunpublic ActiveRun startRun(java.lang.String runName) 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.
 
 - 
startRunpublic ActiveRun startRun(java.lang.String runName, java.lang.String parentRunId) 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.
 
 - 
withActiveRunpublic void withActiveRun(java.util.function.Consumer<ActiveRun> activeRunFunction) 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 an- ActiveRunand logs data to it.
 
 - 
withActiveRunpublic 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 an- ActiveRunand logs data to it.
 
 
- 
 
-