MLflow Tracking APIs
MLflow Tracking provides comprehensive APIs across multiple programming languages to capture your machine learning experiments. Whether you prefer automatic instrumentation or granular control, MLflow adapts to your workflow.
Choose Your Approach
MLflow offers two primary methods for experiment tracking, each optimized for different use cases:
🤖 Automatic Logging - Zero Setup, Maximum Coverage
Perfect for getting started quickly or when using supported ML libraries. Just add one line and MLflow captures everything automatically.
import mlflow
mlflow.autolog() # That's it!
# Your existing training code works unchanged
model.fit(X_train, y_train)
What gets logged automatically:
- Model parameters and hyperparameters
- Training and validation metrics
- Model artifacts and checkpoints
- Training plots and visualizations
- Framework-specific metadata
Supported libraries: Scikit-learn, XGBoost, LightGBM, PyTorch, Keras/TensorFlow, Spark, and more.
🛠️ Manual Logging - Complete Control, Custom Workflows
Ideal for custom training loops, advanced experimentation, or when you need precise control over what gets tracked.
- Python
- Java
- R
import mlflow
with mlflow.start_run():
# Log parameters
mlflow.log_param("learning_rate", 0.01)
mlflow.log_param("batch_size", 32)
# Your training logic here
for epoch in range(num_epochs):
train_loss = train_model()
val_loss = validate_model()
# Log metrics with step tracking
mlflow.log_metrics({"train_loss": train_loss, "val_loss": val_loss}, step=epoch)
# Log final model
mlflow.sklearn.log_model(model, name="model")
MlflowClient client = new MlflowClient();
RunInfo run = client.createRun();
// Log parameters
client.logParam(run.getRunId(), "learning_rate", "0.01");
client.logParam(run.getRunId(), "batch_size", "32");
// Log metrics with timesteps
for (int epoch = 0; epoch < numEpochs; epoch++) {
double trainLoss = trainModel();
client.logMetric(run.getRunId(), "train_loss", trainLoss,
System.currentTimeMillis(), epoch);
}
library(mlflow)
with(mlflow_start_run(), {
# Log parameters
mlflow_log_param("learning_rate", 0.01)
mlflow_log_param("batch_size", 32)
# Training loop
for (epoch in 1:num_epochs) {
train_loss <- train_model()
mlflow_log_metric("train_loss", train_loss, step = epoch)
}
})
Core Logging Functions
Setup & Configuration
Function | Purpose | Example |
---|---|---|
mlflow.set_tracking_uri() | Connect to tracking server or database | mlflow.set_tracking_uri("http://localhost:5000") |
mlflow.get_tracking_uri() | Get current tracking URI | uri = mlflow.get_tracking_uri() |
mlflow.create_experiment() | Create new experiment | exp_id = mlflow.create_experiment("my-experiment") |
mlflow.set_experiment() | Set active experiment | mlflow.set_experiment("fraud-detection") |
Run Management
Function | Purpose | Example |
---|---|---|
mlflow.start_run() | Start new run (with context manager) | with mlflow.start_run(): ... |
mlflow.end_run() | End current run | mlflow.end_run(status="FINISHED") |
mlflow.active_run() | Get currently active run | run = mlflow.active_run() |
mlflow.last_active_run() | Get last completed run | last_run = mlflow.last_active_run() |
Data Logging
Function | Purpose | Example |
---|---|---|
mlflow.log_param() / mlflow.log_params() | Log hyperparameters | mlflow.log_param("lr", 0.01) |
mlflow.log_metric() / mlflow.log_metrics() | Log performance metrics | mlflow.log_metric("accuracy", 0.95, step=10) |
mlflow.log_input() | Log dataset information | mlflow.log_input(dataset) |
mlflow.set_tag() / mlflow.set_tags() | Add metadata tags | mlflow.set_tag("model_type", "CNN") |
Artifact Management
Function | Purpose | Example |
---|---|---|
mlflow.log_artifact() | Log single file/directory | mlflow.log_artifact("model.pkl") |
mlflow.log_artifacts() | Log entire directory | mlflow.log_artifacts("./plots/") |
mlflow.get_artifact_uri() | Get artifact storage location | uri = mlflow.get_artifact_uri() |