0
0
MLOpsdevops~10 mins

Automated model validation before promotion in MLOps - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you train a machine learning model, you want to make sure it works well before using it in real life. Automated model validation helps check the model's quality automatically before you decide to use it in your app or share it with others.
When you want to check if a new model is better than the old one before using it.
When you want to avoid mistakes by automatically testing models after training.
When you want to save time by not checking models manually every time.
When you want to keep a history of model performance to track improvements.
When you want to automatically stop bad models from being used in production.
Commands
This command runs the MLflow project in the current folder with a parameter alpha set to 0.5. It starts the model training and validation process automatically.
Terminal
mlflow run . -P alpha=0.5
Expected OutputExpected
2024/06/01 12:00:00 INFO mlflow.projects: === Run (ID=123abc) started === 2024/06/01 12:00:05 INFO mlflow.projects: Training model with alpha=0.5 2024/06/01 12:00:10 INFO mlflow.projects: Validation accuracy: 0.87 2024/06/01 12:00:10 INFO mlflow.projects: Model passed validation and is ready for promotion 2024/06/01 12:00:10 INFO mlflow.projects: === Run (ID=123abc) succeeded ===
-P - Set a parameter value for the MLflow project run
This command serves the validated model from the run with ID 123abc on port 1234 so you can test it live or use it in your app.
Terminal
mlflow models serve -m runs:/123abc/model -p 1234
Expected OutputExpected
2024/06/01 12:01:00 INFO mlflow.models: Starting model server at http://127.0.0.1:1234 2024/06/01 12:01:00 INFO mlflow.models: Model loaded successfully
-m - Specify the model URI to serve
-p - Set the port number for the model server
This command sends a test data point to the running model server to get a prediction and verify the model works as expected.
Terminal
curl -d '{"data": [[5.1, 3.5, 1.4, 0.2]]}' -H 'Content-Type: application/json' -X POST http://127.0.0.1:1234/invocations
Expected OutputExpected
{"predictions": [0]}
-d - Send JSON data in the request body
-H - Set the content type header to JSON
-X - Use POST method to send data
Key Concept

If you remember nothing else from this pattern, remember: automate testing your model's quality before using it to avoid mistakes and save time.

Code Example
MLOps
import mlflow
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load data
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.2, random_state=42)

# Start MLflow run
with mlflow.start_run() as run:
    # Train model
    model = LogisticRegression(max_iter=200)
    model.fit(X_train, y_train)

    # Predict and validate
    preds = model.predict(X_test)
    acc = accuracy_score(y_test, preds)

    # Log model and metric
    mlflow.log_metric("accuracy", acc)
    mlflow.sklearn.log_model(model, "model")

    # Check if accuracy is good enough
    if acc >= 0.85:
        print(f"Model passed validation with accuracy {acc:.2f}")
    else:
        print(f"Model failed validation with accuracy {acc:.2f}")
OutputSuccess
Common Mistakes
Skipping the validation step and promoting the model directly after training
This can cause bad models to be used in production, leading to wrong predictions and user problems.
Always run automated validation to check model quality before promotion.
Not specifying parameters correctly when running the MLflow project
The model may train with default or wrong settings, causing poor performance.
Use the -P flag to set parameters explicitly during the run.
Testing the model server with wrong data format or missing headers
The server will reject the request or return errors, making testing fail.
Send JSON data with the correct content-type header and use POST method.
Summary
Run the MLflow project with parameters to train and validate the model automatically.
Serve the validated model to test it live or integrate with applications.
Send test data to the model server to confirm it predicts correctly before promotion.