public class MlflowContext extends Object
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 and Description |
---|
MlflowContext()
Constructs a
MlflowContext with a MlflowClient based on the MLFLOW_TRACKING_URI
environment variable. |
MlflowContext(MlflowClient client)
Constructs a
MlflowContext which points to the specified trackingUri. |
MlflowContext(String trackingUri)
Constructs a
MlflowContext which points to the specified trackingUri. |
Modifier and Type | Method and Description |
---|---|
MlflowClient |
getClient()
Returns the client used to log runs.
|
String |
getExperimentId()
Returns the experiment ID we are logging to.
|
MlflowContext |
setExperimentId(String experimentId)
Sets the experiment to log runs to by ID.
|
MlflowContext |
setExperimentName(String experimentName)
Sets the experiment to log runs to by name.
|
ActiveRun |
startRun()
Starts a MLflow run without a name.
|
ActiveRun |
startRun(String runName)
Starts a MLflow run.
|
ActiveRun |
startRun(String runName,
String parentRunId)
Like
startRun(String) but sets the mlflow.parentRunId tag in order to create
nested runs. |
void |
withActiveRun(Consumer<ActiveRun> activeRunFunction)
Like
startRun(String) but will terminate the run after the activeRunFunction is
executed. |
void |
withActiveRun(String runName,
Consumer<ActiveRun> activeRunFunction)
Like
withActiveRun(Consumer) with an explicity run name. |
public MlflowContext()
MlflowContext
with a MlflowClient based on the MLFLOW_TRACKING_URI
environment variable.public MlflowContext(String trackingUri)
MlflowContext
which points to the specified trackingUri.trackingUri
- The URI to log to.public MlflowContext(MlflowClient client)
MlflowContext
which points to the specified trackingUri.client
- The client used to log runs.public MlflowClient getClient()
public MlflowContext setExperimentName(String experimentName) throws IllegalArgumentException
experimentName
- the name of the experiment to log runs to.IllegalArgumentException
- if the experiment name does not match an existing experimentpublic MlflowContext setExperimentId(String experimentId)
experimentId
- the id of the experiment to log runs to.public String getExperimentId()
public ActiveRun startRun()
ActiveRun
. MLflow runs should be ended using ActiveRun.endRun()
ActiveRun
object to log data to.public ActiveRun startRun(String runName)
ActiveRun
. MLflow runs should be ended using ActiveRun.endRun()
runName
- The name of this run. For display purposes only and is stored in the
mlflow.runName tag.ActiveRun
object to log data to.public ActiveRun startRun(String runName, String parentRunId)
startRun(String)
but sets the mlflow.parentRunId
tag in order to create
nested runs.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 parentActiveRun
object to log data to.public void withActiveRun(Consumer<ActiveRun> activeRunFunction)
startRun(String)
but will terminate the run after the activeRunFunction is
executed.
For example
mlflowContext.withActiveRun((activeRun -> { activeRun.logParam("layers", "4"); }));
activeRunFunction
- A function which takes an ActiveRun
and logs data to it.public void withActiveRun(String runName, Consumer<ActiveRun> activeRunFunction)
withActiveRun(Consumer)
with an explicity run name.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.Copyright © 2019. All rights reserved.