Part 9- 7 Steps to Deploy Your ML Model on SageMaker (In Under a Day)
10-part series about building your first Data stack from 0 to 1, and be ready for AI implementation.
Hello Readers!
In our last article, we build our Predictive analytics model with RFM. Now, we're going to deploy this model to AWS Sagemaker.
While deploying, I also want to touch on how to setup SageMaker efficiently, if you have used SageMaker before you likely know amount of configurations it takes to deploy simple ML model. If you have not used it, this article serve as good best practices guide for you.
This one will be very hands on article, so buckle up, prepare your AWS environment and let’s get coding!
1. Set up your SageMaker environment in less than 15 minutes
The first step in any SageMaker project is setting up your environment. This process is often perceived as complex, but it doesn't have to be.
Start by logging into your AWS console and navigating to the SageMaker service.
Click on "Notebook instances" in the left sidebar, then "Create notebook instance".
Give your instance a name and choose an instance type. For most projects, an ml.t3.medium instance is sufficient to start with.
Under "Permissions and encryption", choose "Create a new role" if you don't already have one set up.
Click "Create notebook instance". Your instance will be ready in a few minutes.
That's it! You've just set up your SageMaker environment.
Once your notebook instance is ready, click "Open Jupyter" to launch the Jupyter interface.
Create a new notebook and run the following code to install the required libraries:
!pip install pandas numpy scikit-learn xgboost plotnine
2. Prepare your data for ML in three simple steps
Now following few steps are similar to what we already built in last week's article. This one will focus on how to deploy RFM model using SageMaker. Let's break it down into three simple steps:
Step 1: Load your data
First, we need to load our data. In this example, we'll use a CSV file stored in an S3 bucket.
import pandas as pd
import boto3
s3 = boto3.client('s3')
bucket_name = 'your-bucket-name'
object_key = 'path/to/your/data.csv'
obj = s3.get_object(Bucket=bucket_name, Key=object_key)
df = pd.read_csv(obj['Body'])
This code uses boto3 to access your S3 bucket and pandas to read the CSV file into a DataFrame.
Step 2: Clean and preprocess your data
Next, we need to clean our data and handle any missing values:
# Handle missing values
df = df.dropna()
# Convert date column to datetime
df['date'] = pd.to_datetime(df['InvoiceDate'])
# Create new features
df['total_amount'] = df['Quantity'] * df['UnitPrice']
This code removes any rows with missing values, converts the 'date' column to datetime format, and creates a new 'total_amount' feature.
Step 3: Split your data
I will save you some time here and suggest to refer feature engineering step as described in previous post, so that we can focus on sagemaker concepts here.
In summary, we will need to split our training and test data and create target features.
from sklearn.model_selection import train_test_split
X = df[['feature1', 'feature2', 'feature3']]
y = df['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
This code separates our features and target variable, then splits the data into training (80%) and testing (20%) sets.
3. Choose and customize your model without advanced coding
SageMaker offers a variety of built-in algorithms that you can use without writing complex code. For this example, we'll use XGBoost, a powerful and popular algorithm for both classification and regression tasks.
Here's how you can set up an XGBoost model in SageMaker:
from sagemaker.xgboost.estimator import XGBoost
xgb_estimator = XGBoost(
entry_point='script.py',
framework_version='1.0-1',
hyperparameters={
'max_depth': 5,
'eta': 0.2,
'gamma': 4,
'min_child_weight': 6,
'subsample': 0.8,
'objective': 'reg:squarederror',
'num_round': 100
},
role=role,
instance_count=1,
instance_type='ml.m5.xlarge',
output_path='s3://{}/{}/output'.format(bucket, prefix)
)
This code sets up an XGBoost estimator with some basic hyperparameters. You can easily customize these parameters without needing to understand the underlying code of the algorithm.
4. Train your model faster using SageMaker's built-in optimizations
Training your model is where SageMaker really shines. It handles all the heavy lifting of setting up the training infrastructure, allowing you to focus on your model.
To start training, simply call the fit
method on your estimator:
xgb_estimator.fit({'train': train_input, 'validation': val_input})
SageMaker will automatically provision the necessary compute resources, train your model, and then shut down the resources when training is complete.
But SageMaker doesn't just train your model - it optimizes the process. It uses techniques like:
Distributed training across multiple instances
Automatic model tuning to find the best hyperparameters
Incremental training to update existing models with new data
These optimizations can significantly speed up your training process, especially for large datasets or complex models.
5. Deploy your model to production with just a few clicks
Once your model is trained, deploying it is surprisingly simple. You can do it with just a few lines of code:
predictor = xgb_estimator.deploy(
initial_instance_count=1,
instance_type='ml.t2.medium'
)
This code creates an endpoint that you can use to get real-time predictions from your model.
But deployment isn't just about getting your model online. SageMaker offers several features to make your deployments more robust and efficient:
Auto-scaling to handle varying levels of traffic
A/B testing to compare different model versions
Multi-model endpoints to host multiple models on a single endpoint
These features allow you to deploy your model with confidence, knowing that it can handle real-world usage patterns.
6. Beyond the basics: Advanced SageMaker features
While the steps we've covered will get you up and running quickly, SageMaker offers many more advanced features for when you're ready to take your ML workflows to the next level:
SageMaker Experiments
SageMaker Experiments allows you to organize, track, compare, and evaluate your machine learning experiments:
from sagemaker.experiments.run import Run
with Run(experiment_name='my-experiment', run_name='my-run') as run:
run.log_parameter('learning_rate', 0.1)
run.log_metric('accuracy', 0.9)
This feature is invaluable for keeping track of your different model versions and hyperparameter configurations.
SageMaker Debugger
SageMaker Debugger helps you monitor your training jobs in real-time and detect issues like overfitting or vanishing gradients:
from sagemaker.debugger import Rule, rule_configs
rules = [
Rule.sagemaker(rule_configs.vanishing_gradient()),
Rule.sagemaker(rule_configs.overfit()),
Rule.sagemaker(rule_configs.poor_weight_initialization())
]
estimator = XGBoost(
...
rules = rules
)
By adding these rules to your estimator, SageMaker will automatically monitor your training job and alert you if any issues are detected.
SageMaker Model Monitor
Once your model is deployed, SageMaker Model Monitor helps you detect concept drift and data quality issues:
from sagemaker.model_monitor import DataCaptureConfig
data_capture_config = DataCaptureConfig(
enable_capture=True,
sampling_percentage=100,
destination_s3_uri='s3://{}/{}/captured-data'.format(bucket, prefix)
)
predictor = model.deploy(
...
data_capture_config=data_capture_config
)
This setup captures all incoming data to your endpoint, allowing you to monitor for changes in your data distribution over time.
By implementing these advanced configurations, we have achieved 80% of the tasks that most companies need to do in order to prepare, deploy, monitor and continuously improve their ML models lifecycle.
7. Common pitfalls and how to avoid them
But Even with SageMaker's user-friendly interface, there are still some common pitfalls that newcomers often encounter. Here's how to watch out for them :
1. Forgetting to shut down resources
And how I accidentally spent 1000s of $, but that’s story for other article :)
SageMaker resources can be expensive if left running. Always remember to shut down your notebook instances and endpoints when you're not using them:
# To stop a notebook instance
notebook_instance_name = 'your-notebook-name'
sagemaker_client = boto3.client('sagemaker')
sagemaker_client.stop_notebook_instance(NotebookInstanceName=notebook_instance_name)
# To delete an endpoint
endpoint_name = 'your-endpoint-name'
sagemaker_client.delete_endpoint(EndpointName=endpoint_name)
2. Not optimizing for cost
SageMaker offers several instance types. While it's tempting to always use the most powerful instances, this can quickly become expensive. Start with smaller instances and scale up only when necessary.
3. Ignoring data security
Always ensure your data is encrypted, both in transit and at rest. SageMaker makes this easy:
estimator = XGBoost(
...
volume_kms_key='your-kms-key-id',
output_kms_key='your-kms-key-id',
enable_network_isolation=True
)
This ensures that your data and model artifacts are encrypted, and your training job runs in an isolated network.
Conclusion: Your SageMaker journey is just beginning
Deploying your first ML model on SageMaker in under a day is just the start. As you become more comfortable with the platform, you'll discover more advanced features that can help you build more sophisticated ML workflows.
Remember, the key to success with SageMaker (and ML in general) is experimentation. Don't be afraid to try different algorithms, hyperparameters, and features. With SageMaker, you can quickly iterate on your models and find what works best for your specific use case.
So what are you waiting for? Fire up that SageMaker notebook and start deploying!
For the last post in this series, we will reflect how far we have come and also how to yield the results for months to come. So, stay tuned.