Experiment tracking
5.10.1kata 1 de 5

MLflow Basics

Configuración y primeros pasos con MLflow

MLflow es una plataforma open-source para gestionar el ciclo de vida de los experimentos de machine learning. Su componente principal, MLflow Tracking, permite registrar automáticamente cada ejecución de entrenamiento con sus hiperparámetros, métricas y artefactos. Esto elimina el caos de carpetas con nombres como 'model_final_v2' y reemplaza ese sistema frágil por un registro estructurado y consultable.

La instalación es simple: `pip install mlflow`. Por defecto, MLflow almacena todos los datos en una carpeta local `./mlruns`. Para equipos, se configura un servidor centralizado con `mlflow server --backend-store-uri postgresql://... --default-artifact-root s3://...`. Cada integrante apunta su cliente al mismo servidor con `mlflow.set_tracking_uri('http://mlflow-server:5000')`.

El flujo básico gira en torno a tres objetos: experiments (agrupan runs relacionados), runs (una ejecución de entrenamiento), y artifacts (archivos producidos por el run). Se inicia un run con `mlflow.start_run()`, se loguean datos durante el entrenamiento, y al finalizar el contexto `with` el run se cierra automáticamente. La UI local se levanta con `mlflow ui` y permite explorar todo visualmente en el navegador.

structure.txt
# MLflow Basics — setup and first run

# Install
# pip install mlflow

import mlflow

# Point to local or remote server
mlflow.set_tracking_uri('file:./mlruns')

# Create or reuse an experiment
mlflow.set_experiment('my-first-experiment')

# Run a training session
with mlflow.start_run(run_name='baseline_run'):
    # Log a hyperparameter
    mlflow.log_param('learning_rate', 0.01)
    # Simulate training
    accuracy = 0.88
    # Log a metric
    mlflow.log_metric('val_accuracy', accuracy)

# Launch the UI
# mlflow ui --port 5000
# Open: http://localhost:5000

Debugging lab

Detecta y corrige el error en el código.

0/3 tests passing0%
  1. 01

    I ran `mlflow.log_metric('accuracy', 0.9)` outside a `with mlflow.start_run()` block and got an error. Why?

  2. 02

    I called `mlflow.set_tracking_uri()` inside the `with mlflow.start_run():` block and my runs are not being saved to the server.

  3. 03

    I have two experiments with the same name 'training' in MLflow. Now I can't tell them apart. How should I have structured this?