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)
Advertisements