Amazon SageMaker is a powerful cloud platform for building, training, and deploying machine learning models. Its built-in algorithms simplify the process of training deep learning models, even for beginners. In this tutorial, you’ll learn how to use SageMaker’s built-in algorithms to train a deep learning model from scratch, with clear code examples and explanations at every step.
By the end of this guide, you’ll understand how to preprocess data, configure training jobs, deploy models, and make predictions—all without managing complex infrastructure.
First, launch a SageMaker Jupyter Notebook instance:
DL-Training-Demo
) and leave
default settings.
# Install required libraries
!pip install sagemaker boto3 pandas numpy
For this example, we’ll use the MNIST dataset (handwritten digits). SageMaker requires data to be stored in an S3 bucket.
import sagemaker
from sagemaker import get_execution_role
import boto3
# Initialize SageMaker session
sagemaker_session = sagemaker.Session()
role = get_execution_role()
# Download MNIST dataset
!wget https://sagemaker-sample-data-us-west-2.s3-us-west-2.amazonaws.com/tensorflow/mnist/mnist.npz
# Upload to S3
bucket_name = 'your-s3-bucket-name' # Replace with your bucket
prefix = 'mnist-data'
sagemaker_session.upload_data(path='mnist.npz', bucket=bucket_name, key_prefix=prefix)
We’ll use SageMaker’s built-in TensorFlow algorithm. Configure the training job:
from sagemaker.tensorflow import TensorFlow
# Define hyperparameters
hyperparameters = {
'epochs': 10,
'batch-size': 128,
'learning-rate': 0.001
}
# Initialize the TensorFlow estimator
estimator = TensorFlow(
entry_point='train.py', # Your training script
role=role,
instance_count=1,
instance_type='ml.m5.large',
framework_version='2.12',
py_version='py39',
hyperparameters=hyperparameters
)
# Start training
estimator.fit({'training': f's3://{bucket_name}/{prefix}/mnist.npz'})
Note: Create a train.py
script to define
your model architecture and training logic.
After training, deploy the model to a hosted endpoint for inference:
# Deploy the model
predictor = estimator.deploy(
initial_instance_count=1,
instance_type='ml.t2.medium'
)
# Example prediction
import numpy as np
sample_data = np.random.rand(1, 28, 28) # Replace with real data
result = predictor.predict(sample_data)
print(result)
Avoid unnecessary costs by deleting resources:
# Delete endpoint
predictor.delete_endpoint()
# Optionally, delete S3 data
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)
bucket.objects.filter(Prefix=prefix).delete()
You’ve now trained a deep learning model using SageMaker’s built-in algorithms! With this foundation, you can explore other algorithms like XGBoost or K-Means, or dive into custom model training. SageMaker abstracts away infrastructure complexity, letting you focus on solving real-world problems.
Category: AWS
Similar Articles