Amazon SageMaker - Quick Guide



Amazon SageMaker - Introduction

Amazon SageMaker is a fully managed machine learning (ML) service that helps data scientists and developers build, train, and deploy ML models quickly into production-ready hosted environment. It simplifies each step of the machine learning lifecycle, from data preparation to model training and deployment.

  • Amazon SageMaker provides an intuitive user interface (UI) for running ML workflows, making its tools available across various integrated development environments (IDEs).
  • Amazon SageMaker also provides support for popular machine learning frameworks like TensorFlow, PyTorch, and Scikit-learn. This feature of Amazon SageMaker gives a flexibility to the developers to use the tools they need.

To start working with Amazon SageMaker, you need to set up either a Amazon SageMaker notebook instance or use Amazon SageMaker Studio. You can then upload your data, choose an ML algorithm, train your model, and deploy it.

Benefits of Using Amazon SageMaker for Machine Learning

In this section, let's understand the benefits of using Amazon SageMaker −

Fully Managed Service

Amazon SageMaker is fully managed which means AWS set up servers, manage the infrastructure, and scale resources as needed. Users can focus on their machine learning tasks without worrying about system maintenance or performance issues.

Scale Your ML Models

Amazon SageMaker allows you to scale your machine learning models as your data and application expand. It also supports distributed training which enables faster processing times. It also ensures that even complex models can be trained efficiently.

Cost Efficiency

Amazon SageMaker uses the pay-as-you-go model which means you only pay for what you use. You do not need to spend money on expensive hardware.

Amazon SageMaker also provides automatic model tuning and optimization which helps you to reduce computing time and expense.

Easy Model Deployment

With the help of Amazon SageMaker, you can easily deploy your machine learning models in a production environment. It provides various deployment options such as batch predictions, real-time inference, and A/B testing.

Built-In Algorithms and Preprocessing

Users do not need to write their own algorithms because Amazon SageMaker provides a wide variety of built-in algorithms that are optimized for performance. It saves lots of time saving time and effort.

Amazon SageMaker Provides a Secured Environment

Amazon SageMaker provides robust security features to protect your data and models. It is integrated with AWS Identity and Access Management (IAM) which allows you to control user permissions and access levels.

Support for Multiple Frameworks

Amazon SageMaker supports a wide variety of machine learning frameworks like TensorFlow, PyTorch, and MXNet. It allows developers to choose the best tools for their projects.

How to Setup Amazon SageMaker?

Follow the steps given below to set up Amazon SageMaker −

Setting Up Amazon SageMaker

Step 1: Sign into AWS

The first step to setting up Amazon SageMaker is signing in to your AWS Management Console. If you do not have an AWS account, you must create it. You can create it for free on AWS website.

Step 2: Navigate to Amazon SageMaker

After signing in, search for "Amazon SageMaker" in the AWS Console and select it. It will open Amazon SageMaker dashboard where you can manage your machine learning projects.

Step 3: Create a Amazon SageMaker Notebook Instance

To start building machine learning models, you will need to create a notebook instance. Click on "Notebook Instances" and select "Create Notebook Instance.".

Step 4: Configure the Notebook Instance

You need to choose an instance type based on your computing needs. If you are a beginner, you can start with a smaller instance type like ml.t2.medium.

You also need to give your notebook instance a name. After that select a role with the necessary permissions.

Step 5: Launch the Notebook Instance

Once configured, click "Create Notebook Instance". It will take a few minutes for the instance to be ready.

Once it is ready and running, click on "Open Jupyter" to access the Jupyter Notebook interface. Now you can start writing code, training models, and running experiments.

Step 6: Prepare Your Data

Before you start training your models, you need to upload your data to an Amazon S3 bucket.

S3 is the recommended storage service by AWS for use with Amazon SageMaker. You can access and preprocess the data directly from the notebook environment.

Configuring Your First Amazon SageMaker Project

Once your Amazon SageMaker notebook instance is ready, you can start configuring your first project. Follow the steps given below:

Step 1: Choose a Pre-built Algorithm or Write Your Own

Amazon SageMaker provides many built-in algorithms optimized for performance, such as Linear Learner, XGBoost, and more. You can choose one of these pre-built algorithms or you can write your own using Python.

Step 2: Data Preparation

Data preparation is the key to a successful project. Amazon SageMaker provides tools for cleaning and transforming your data. You can access your datasets stored in Amazon S3 directly from the notebook.

Use the data preparation libraries to explore, clean, and preprocess your data before feeding it into your machine learning model.

Step 3: Training Your Model

After data preparation, the next step is to train the model. In the Jupyter Notebook, you can specify the algorithm, define hyperparameters, and configure training jobs.

Step 4: Tuning and Optimization

Amazon SageMaker provides automatic model tuning, which adjusts hyperparameters to find the best model performance. After training, you can review the results and further tune your model for better accuracy.

You can also use Amazon SageMaker's built-in optimization tools to speed up this process.

Step 5: Deploying the Model

Once you have trained and optimized the model, it is time to deploy it. Amazon SageMaker allows you to deploy models directly to an endpoint for real-time inference.

Choose "Create Endpoint" from the Amazon SageMaker dashboard and configure your deployment settings. Once deployed, you can start making predictions using your trained model.

Step 6: Monitoring and Scaling

After deploying your model, you can monitor its performance. For monitoring you can use Amazon SageMakers built-in tools. It provides real-time monitoring, automatic scaling based on demand, and version control.

Amazon SageMaker - Building ML Models

Read this chapter to learn how you can build Machine Learning (ML) models using the built-in algorithms in Amazon SageMaker.

Using Built-in Algorithms in Amazon SageMaker

Follow the steps given below −

Step 1: Choose a Built-in Algorithm

First, you need to choose a built-in algorithm. Amazon SageMaker provides a wide range of built-in algorithms like Linear Learner (for classification and regression), XGBoost (boosted trees for classification), and K-means (for clustering).

Step 2: Prepare the Data

Next, you need to upload your data to an Amazon S3 bucket. Amazon SageMaker reads the data from S3 and uses it for training the model. The data should be in a CSV or another format supported by Amazon SageMaker.

Step 3: Create a Amazon SageMaker Session

Open your Jupyter Notebook. Import the necessary libraries and create a Amazon SageMaker session −

import SageMaker
from SageMaker import get_execution_role

# Create a SageMaker session
session = SageMaker.Session()

# Define the S3 bucket and data path
bucket = 'your-s3-bucket-name'
prefix = 'your-data-prefix'
role = get_execution_role()

Step 4: Configure the Algorithm and Training Job

After creating a Amazon SageMaker session, we need to configure the built-in Linear Learner algorithm.

You can use Amazon SageMaker's LinearLearner estimator. Then, set the hyperparameters and start the training job as follows −

from SageMaker.amazon.linear_learner import LinearLearner

# Configure the LinearLearner estimator
linear = LinearLearner(role=role, instance_count=1,
                       instance_type='ml.m4.xlarge',
                       predictor_type='binary_classifier',
                       output_path=f's3://{bucket}/{prefix}/output')

# Define the data channels (train and validation)
train_data = f's3://{bucket}/{prefix}/train/train.csv'
validation_data = f's3://{bucket}/{prefix}/validation/validation.csv'
data_channels = {'train': train_data, 'validation': validation_data}

# Train the model
linear.fit(inputs=data_channels)

Step 5: Deploy the Model for Inference

After training, deploy the model to a Amazon SageMaker endpoint for real-time predictions as follows −

# Deploy the trained model
predictor = linear.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge')

# Make predictions
result = predictor.predict([[1.5, 2.5, 3.0]])
print(result)

Bringing Your Own Model to Amazon SageMaker

Amazon SageMaker also allows you to bring your own pre-trained model and deploy it. Follow the steps below to bring your own model to Amazon SageMaker −

Step 1: Save Your Model

If you have trained a model using a framework like TensorFlow or PyTorch, you can save the weights and architecture of the model to an S3 bucket.

Take a look at this following example

import torch
import boto3

# Save a PyTorch model locally
torch.save(model.state_dict(), 'model.pth')

# Upload the model to S3
s3 = boto3.client('s3')
s3.upload_file('model.pth', 'your-s3-bucket-name', 'model/model.pth')

Step 2: Create a Amazon SageMaker Model

Next, we need to create a model object and specify the container where the model is hosted. Use You can use Amazon SageMakers Model class for this. You can also use custom Docker images to serve your model as follows −

from SageMaker.model import Model

# Define the model
model = Model(model_data='s3://your-s3-bucket-name/model/model.pth',
              role=role,
              image_uri='your-custom-docker-image')

# Deploy the model
predictor = model.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge')

Step 3: Deploy the Model

After creating the model, deploy it to a Amazon SageMaker endpoint to make it available for inference as follows −

# Perform inference using the deployed model
result = predictor.predict([[1.5, 2.5, 3.0]])
print(result)

Amazon SageMaker - Training ML Models

You can easily train machine learning models by using Amazon SageMakers fully managed training service.

To train a ML model, you can either use Amazon SageMaker's built-in algorithms or use our own model. In both the cases, Amazon SageMaker allows you to run training jobs efficiently.

How to Train Models Using Amazon SageMaker?

Lets understand how you can train models using Amazon SageMaker with the help of below given Python program −

Step 1: Prepare Your Data

First, prepare your data and store it in Amazon S3 in CSV format or any other suitable format. Amazon SageMaker reads data from S3 for training jobs.

Step 2: Define the Estimator

Now, you need to define the estimator. You can use the Estimator object to configure the training job. For this example, we'll train a model using the built-in XGBoost algorithm as follows −

import SageMaker
from SageMaker import get_execution_role
from SageMaker.inputs import TrainingInput

# Define your Amazon SageMaker session and role
session = SageMaker.Session()
role = get_execution_role()

# Define the XGBoost estimator
xgboost = SageMaker.estimator.Estimator(
    image_uri=SageMaker.image_uris.retrieve("xgboost", session.boto_region_name),
    role=role,
    instance_count=1,
    instance_type="ml.m4.xlarge",
    output_path=f"s3://your-bucket/output",
    SageMaker_session=session,
)

# Set hyperparameters
xgboost.set_hyperparameters(objective="binary:logistic", num_round=100)

Step 3: Specify Training Data

We need to specify the training data for further processing. You can use the TrainingInput class to specify the location of your data in S3 as follows −

# Specify training data in S3
train_input = TrainingInput
   (s3_data="s3://your-bucket/train", content_type="csv")
validation_input = TrainingInput
   (s3_data="s3://your-bucket/validation", content_type="csv")

Step 4: Train the Model

Finally, start the training job by calling the fit method as follows −

# Train the model
xgboost.fit({"train": train_input, "validation": validation_input})

Once trained, Amazon SageMaker will automatically provision resources, run the training job, and save the model output to the specified S3 location.

Distributed Training with Amazon SageMaker

Amazon SageMaker supports distributed training which enables you to scale training across multiple instances. This is useful in case when you are dealing with large datasets or deep learning models. Amazon SageMaker provides frameworks like TensorFlow and PyTorch that support distributed training.

To enable distributed training, you can increase the instance_count parameter in the Estimator object.

Example

Given below is an example using TensorFlow −

from SageMaker.tensorflow import TensorFlow

# Define the TensorFlow estimator with distributed training
tensorflow_estimator = TensorFlow(
    entry_point="train.py",
    role=role,
    instance_count=2,
    instance_type="ml.p3.2xlarge",
    framework_version="2.3",
    py_version="py37",
)

# Train the model on multiple instances
tensorflow_estimator.fit({"train": train_input, "validation": validation_input})

In this example, Amazon SageMaker uses two ml.p3.2xlarge instances for distributed training. It will reduce the training time for large models.

Amazon SageMaker - Deploying ML Models

Monitoring Model Performance in Amazon SageMaker

Monitoring machine learning models is important step to ensure the model will perform as expected when deployed in production. Amazon SageMaker provides various tools to monitor models, track metrics, and detect performance degradation over time.

Amazon SageMaker Model Monitor

The Amazon SageMaker Model Monitor tool continuously tracks the quality of models in real-time. It monitors incoming data for any kind of inconsistencies and alerts you when the prediction by model varies from the expected one. This tool ensures that your models stay accurate and reliable always.

CloudWatch Integration

Another monitoring tool is CloudWatch. Amazon SageMaker easily integrates with Amazon CloudWatch to collect, track, and visualize performance metrics in real-time. It allows you to configure custom metrics, such as accuracy or latency.

Automated Retraining

Amazon SageMaker also supports automated retraining which allows you to set triggers to retrain models when certain conditions are met. By automating retraining, you ensure that your models stay up to date with the latest data.

Hyperparameter Tuning and Optimization

Hyperparameter tuning plays an important role in achieving the best performance from a ML model. Amazon SageMakers hyperparameter optimization feature allows you to automatically search for the best combination of hyperparameters for your model.

Implementing Hyperparameter Tuning in Amazon SageMaker

Amazon SageMakers automatic hyperparameter tuning is also known as hyperparameter optimization (HPO). It helps you to identify the best hyperparameters by running multiple training jobs with different parameter combinations.

Example

Given below is a basic Python code example for hyperparameter tuning in Amazon SageMaker −

from SageMaker.tuner import HyperparameterTuner, ContinuousParameter

# Define the hyperparameters to tune
hyperparameter_ranges = {
    'eta': ContinuousParameter(0.01, 0.2),
    'max_depth': ContinuousParameter(3, 10)
}

# Set up the tuner
tuner = HyperparameterTuner(
    estimator=xgboost_estimator,
    objective_metric_name='validation:accuracy',
    hyperparameter_ranges=hyperparameter_ranges,
    max_jobs=10,
    max_parallel_jobs=2
)

# Start tuning job
tuner.fit({"train": train_input, "validation": validation_input})

Amazon SageMaker - Monitoring & Optimizing

Monitoring Model Performance in Amazon SageMaker

Monitoring machine learning models is important step to ensure the model will perform as expected when deployed in production. Amazon SageMaker provides various tools to monitor models, track metrics, and detect performance degradation over time.

Amazon SageMaker Model Monitor

The Amazon SageMaker Model Monitor tool continuously tracks the quality of models in real-time. It monitors incoming data for any kind of inconsistencies and alerts you when the prediction by model varies from the expected one. This tool ensures that your models stay accurate and reliable always.

CloudWatch Integration

Another monitoring tool is CloudWatch. Amazon SageMaker easily integrates with Amazon CloudWatch to collect, track, and visualize performance metrics in real-time. It allows you to configure custom metrics, such as accuracy or latency.

Automated Retraining

Amazon SageMaker also supports automated retraining which allows you to set triggers to retrain models when certain conditions are met. By automating retraining, you ensure that your models stay up to date with the latest data.

Hyperparameter Tuning and Optimization

Hyperparameter tuning plays an important role in achieving the best performance from a ML model. Amazon SageMakers hyperparameter optimization feature allows you to automatically search for the best combination of hyperparameters for your model.

Implementing Hyperparameter Tuning in Amazon SageMaker

Amazon SageMakers automatic hyperparameter tuning is also known as hyperparameter optimization (HPO). It helps you to identify the best hyperparameters by running multiple training jobs with different parameter combinations.

Example

Given below is a basic Python code example for hyperparameter tuning in Amazon SageMaker −

from Amazon SageMaker.tuner import HyperparameterTuner, ContinuousParameter

# Define the hyperparameters to tune
hyperparameter_ranges = {
    'eta': ContinuousParameter(0.01, 0.2),
    'max_depth': ContinuousParameter(3, 10)
}

# Set up the tuner
tuner = HyperparameterTuner(
    estimator=xgboost_estimator,
    objective_metric_name='validation:accuracy',
    hyperparameter_ranges=hyperparameter_ranges,
    max_jobs=10,
    max_parallel_jobs=2
)

# Start tuning job
tuner.fit({"train": train_input, "validation": validation_input})

Amazon SageMaker - Pricing

Amazon SageMaker pricing is based on a pay-as-you-go model, which means you only need to pay for the resources you use. The pricing depends on the different components of the machine learning workflow.

Understanding Amazon SageMaker Pricing

The major pricing components of Amazon SageMaker are highlighted below −

Notebook Instances

When you use Amazon SageMaker's integrated Jupyter notebooks to develop machine learning models, you are charged based on the instance type and usage time.

Each instance type has a different hourly rate, depending on the CPU, memory, and GPU resources it provides. You can choose from a range of instance types as per your needs.

Training Jobs

For training ML models, Amazon SageMaker takes charges according to the computing resources and the duration of the training process. For example, if you use GPU-based instances for faster training, the cost will be higher.

On the other hand, if you use CPU-based instances, the cost will be lesser. The cost also varies based on the region you are using and the type of model (ML, DL, or Generative AI) you are training.

SagaMaker charges you for both the training instance and any other services that are consumed during the training job. These services include data transfer and S3 storage.

Hosting/Inference Endpoints

Amazon SageMaker also charge you for hosting. Hosting starts once your model is trained and deployed to a Amazon SageMaker endpoint. The fees of hosting depend on the instance type used for deployment and the number of active endpoints.

Like training jobs, higher-performing instances such as GPUs will be costly. The billing is calculated on an hourly basis for each endpoint.

S3 Storage and Data Transfer

Amazon SageMaker is dependent on Amazon S3 for storing datasets. You will be charged for data storage in S3, as well as any data transfers between S3 and Amazon SageMaker. These costs depend on the size of the data being used and the amount of data transferred in and out of the cloud.

Cost Optimization Tips for Amazon SageMaker

Here are some ways with the help of which you can manage and reduce costs when using Amazon SageMaker −

Use Spot Instances for Training Jobs

One of the most effective ways to reduce the Amazon SageMaker training costs is by using Spot Instances. Spot Instances allow you to use unused Amazon EC2 capacity at lower prices.

Select the Right Instance Type

Selecting the right instance type help you reduce the cost. So, choose only those instance types that match the workload for your development, training, and hosting needs.

For example, if you are working on a small experiment, a CPU-based instance is enough. You need not to use an expensive GPU instance for this.

Use Amazon SageMaker Managed Spot Training

You can enable Managed Spot Training, when setting up a training job in Amazon SageMaker. This feature automatically uses Spot Instances to reduce the cost of training jobs by up to 70%.

Monitor and Adjust Usage

You can also use Amazon CloudWatch and AWS Budgets to monitor your Amazon SageMaker usage and costs. You can also set alerts in them. You can also review your usage frequently to terminate unused endpoints.

Utilize Free Tier and AWS Credits

If you are a beginner with Amazon SageMaker, AWS provides a Free Tier that includes 250 hours of free t2.medium notebook instances and 50 hours of m4.xlarge instance usage for training jobs.

Advertisements