• About Us
  • Contact Us

MLOps on AWS

Introduction

What is AWS MLOps?

Why should you do MLOps Engineering on AWS?

Guide to deploy an MLOps open-source platform on a EC2 instance

Prerequisites

AWS CLI:

What is mlflow?

Centralized Experiment Tracking with MLFlow

import numpy as np

import pandas as pd

from sklearn.model_selection import train_test_split

from sklearn.ensemble import RandomForestRegressor

from sklearn.metrics import mean_squared_error

import mlflow

import mlflow.sklearn

from your_data_loader import load_data

# Set MLflow tracking URI and experiment name

mlflow.set_tracking_uri('http://your.mlflow.url:5000')

mlflow.set_experiment('Sample Experiment')

# Load the data

X, y = load_data()

# Split the data into train and test sets

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Start the MLflow run

with mlflow.start_run(run_name="your-run-name") as run:

    # Log run parameters

    mlflow.log_param("compute", 'local')

    mlflow.log_param("dataset", 'your-dataset-name')

    mlflow.log_param("dataset_version", '2.0')

    mlflow.log_param("dataset_path", 's3://your.example.bucket/path/to/dataset')

    mlflow.log_param("algo", 'random forest')

    # Log additional hyperparameters for reproducibility

    n_estimators = 5

    mlflow.log_param("n_estimators", n_estimators)

    # Train the model

    rf = RandomForestRegressor(n_estimators=n_estimators)

    rf.fit(X_train, y_train)

    y_pred = rf.predict(X_test)

    # Save the model artifact to the MLflow server for later deployment

    mlflow.sklearn.log_model(rf, "rf-baseline-model")

    # Log model performance using a metric

    mse = mean_squared_error(y_test, y_pred)

    mlflow.log_metric("mse", mse)

    # End the MLflow run

    mlflow.end_run()
# Updating all packages

sudo yum update

# Amazon Linux 2 AMI does not come with Python 3.7 installed at the time of writing

sudo yum install python3.10

# Installing MLFlow and the AWS Python SDK

sudo pip3 install mlflow[extras] psycopg2-binary boto3

# Starting the MLFlow server, don't forget to change the fields in caps

# If you are unfamilier with nohop, read up on it here: https://man.openbsd.org/nohup.1

nohup mlflow server --backend-store-uri postgresql://postgres:YOURPASSWORD@YOUR-DATABASE-ENDPOINT:5432 --default-artifact-root s3://YOURORGANISATION.MLFLOW.BUCKET

References
https://ml-ops.org/

https://ml-ops.org/content/mlops-principles

https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html

https://mlflow.org/docs/latest/what-is-mlflow.html