Machine Learning: Amazon SageMaker Batch Transform
Batch transform automatically manages the processing of large datasets within the limits of specified parameters. For example, having a dataset file, input1.csv, stored in an S3 bucket. The content of the input file might look like the following example.
Record1-Attribute1, Record1-Attribute2, Record1-Attribute3, ..., Record1-AttributeM
Record2-Attribute1, Record2-Attribute2, Record2-Attribute3, ..., Record2-AttributeM
Record3-Attribute1, Record3-Attribute2, Record3-Attribute3, ..., Record3-AttributeM
...
RecordN-Attribute1, RecordN-Attribute2, RecordN-Attribute3, ..., RecordN-AttributeM When a batch transform job starts, SageMaker AI starts compute instances and distributes the inference or preprocessing workload between them. Batch Transform partitions the Amazon S3 objects in the input by key and maps Amazon S3 objects to instances.
When you have multiple files, one instance might process input1.csv, and another instance might process the file named input2.csv. If you have one input file but initialize multiple compute instances, only one instance processes the input file. The rest of the instances are idle.
Thus, using it you can also split input files into mini batches. For example, you might create a mini batch from input1.csv by including only two of the records.
Record3-Attribute1, Record3-Attribute2, Record3-Attribute3, ..., Record3-AttributeM
Record4-Attribute1, Record4-Attribute2, Record4-Attribute3, ..., Record4-AttributeMTo split input files into mini batches when you create a batch transform job, set the SplitType parameter value to Line.
SageMaker AI uses the entire input file in a single request when:
√ SplitType is set to None.
√ An input file can’t be split into mini batches.
If the batch transform job successfully processes all of the records in an input file, it creates an output file. The output file has the same name and the
.outfile extension.
When can we use batch transform?
- To remove noise or bias that interferes with training or inference during data preprocessing.
- To split large datasets in order to get inferences.
- Run inference when you don’t need a persistent endpoint.
Thus, while deploying machine learning models for real-time predictions Batch Transform provides a robust and cost-effective solution for processing large volumes of data offline, making it ideal for scenarios where latency is not critical, such as generating predictions for marketing campaigns, analyzing historical data, or preprocessing data for downstream tasks.
Here, in this lab we will first launch a Jupyter Notebook and prepare our ML environment for data processing and training. We will download, explored, and preprocessed the Adult Census dataset, ensuring it is structured correctly for model training.
Then like other ML model training labs we have explored in previous blog posts, we will:
√ Split the dataset into training, validation, and test sets, aligning them for effective model training and evaluation.
√ Convert the dataset into CSV format and uploaded it to an Amazon S3 bucket for SageMaker processing.
√ Configure an XGBoost estimator with appropriate hyperparameters for training our model.
√ Train ML model on SageMaker, monitor its performance and generate debugging reports.
√ Deploy our trained XGBoost model as an endpoint on Amazon SageMaker for real-time inference.
√ Test and evaluate our model by making predictions on the test dataset.
√ Visualize prediction results and assess model performance using confusion matrices and classification reports.
Hence, we will perform complete end-to-end machine learning workflow on Amazon SageMaker, from data preprocessing to model deployment.
Task
a) Sign in to AWS Management Console.
b) Create and Configure Sagemaker notebook instance.
c) Download, Explore, and Transform Data.
d) Train the Model.
e) Deploy the Model.
f) Start the Batch Transform Job.
Solution
a) 👉 Sign in to AWS Management Console.
Sign in to the AWS Management Console, Make the default AWS Region as US East (N. Virginia) us-east-1.
b) 👉 Create and Configure Sagemaker notebook instance.
1. Navigate to SageMaker AI by clicking on the Services menu at the top, then click on Amazon SageMaker AI option. Make sure you are in the US East (N. Virginia) us-east-1 Region.
Expand the option of Applications and IDEs on the left panel and click on Notebooks.
Click on the Create Notebook instance button.
2. Notebook instance settings:
Notebook instance name: Enter notebook-sagemaker
Notebook instance type: Select ml.t2.medium
Platform identifier: Amazon Linux 2, Jupyter Lab4
Choose ‘AmazonSagemaker-LimitedAccess’ role under IAM role options and leave all settings as default. Then click on the Create Notebook instance button.
Now the notebook instance is now getting created with the status as Pending.
Wait till the status changes to InService. It will take up to 5 minutes.
Get Sumbul Naqvi’s stories in your inbox
Join Medium for free to get updates from this writer.
Once the instance is created, open it by clicking on ‘Open Jupyter.’
In Jupyter, click on ‘New’ and select conda_python3 to start a new notebook session.
c) 👉 Download, Explore, and Transform Data.
Run the below commands one by one and wait for 2–3 minutes after executing every command. Execute the next command when you see that the terminal is idle.
- Retrieves the default SageMaker execution role:
import boto3
import sagemaker
from sagemaker import get_execution_role
role = get_execution_role() # Gets the default SageMaker execution role
print(f"SageMaker Execution Role: {role}")2. Generate Synthetic Data:
import pandas as pd
import numpy as np
# Generate synthetic data
num_samples = 10
data = {
'feature1': np.random.rand(num_samples),
'feature2': np.random.rand(num_samples)
}
df = pd.DataFrame(data)
# Save to a CSV file
input_data_path = 'synthetic_data.csv'
df.to_csv(input_data_path, index=False, header=False) # No header needed for linear learner3. Upload Data to S3:
import sagemaker
sagemaker_session = sagemaker.Session()
bucket = sagemaker_session.default_bucket() # Use default bucket or specify your bucket
prefix = 'batch-transform-demo' # S3 prefix for the data
input_s3_path = sagemaker_session.upload_data(input_data_path, bucket=bucket, key_prefix=f"{prefix}/input")
print(f"Input data uploaded to: {input_s3_path}")d) 👉 Train the Model.
Train a Model (Using Linear Learner as an Example):
import sagemaker
from sagemaker.estimator import Estimator
from sagemaker.inputs import TrainingInput
import boto3
sagemaker_session = sagemaker.Session()
role = sagemaker.get_execution_role()
bucket = sagemaker_session.default_bucket()
prefix = 'batch-transform-demo'
# Create a simple CSV file
with open('simple_train.csv', 'w') as f:
f.write('0.1,0.2,0.3\n0.4,0.5,0.6\n')
# Upload the simple CSV to S3
train_s3_path = sagemaker_session.upload_data('simple_train.csv', bucket, f"{prefix}/simple_train")
linear_estimator = Estimator(
image_uri=sagemaker.image_uris.retrieve(
framework='linear-learner',
region=sagemaker_session.boto_region_name,
version='latest'
),
role=role,
instance_count=1,
instance_type='ml.m5.large',
output_path=f's3://{bucket}/{prefix}/output',
sagemaker_session=sagemaker_session,
hyperparameters={
'predictor_type': 'regressor',
'mini_batch_size': '1'
}
)
train_input = TrainingInput(s3_data=train_s3_path, content_type='text/csv')
linear_estimator.fit({'train': train_input})
model_data_s3_path = linear_estimator.model_data
print(f"Trained model saved at: {model_data_s3_path}")e) 👉 Deploy the Model.
- Create a Model Object:
from sagemaker.model import Model
# Assuming model_data_s3_path, sagemaker_session, and role are defined from the previous training step
linear_model = Model(
image_uri=sagemaker.image_uris.retrieve(
framework='linear-learner',
region=sagemaker_session.boto_region_name,
version='latest'
),
model_data=model_data_s3_path, # Use the S3 path to the trained model
role=role,
sagemaker_session=sagemaker_session # Important to add this line
)2. Create a Transformer Object:
transformer = linear_model.transformer(
instance_count=1,
instance_type='ml.m5.large',
output_path=f's3://{bucket}/{prefix}/output-transform',
)f) 👉 Start the Batch Transform Job.
- Start the Batch Transform Job using below code:
transformer.transform(
data=f's3://{bucket}/{prefix}/input', # S3 path to your input data
content_type='text/csv',
split_type='Line'
)
transformer.wait()
output_s3_path = transformer.output_path
print(f"Batch transform output saved at: {output_s3_path}")2. Retrieve and Inspect the Output:
output_prefix = f"{prefix}/output-transform"
output_key = f"{output_prefix}/synthetic_data.csv.out" # The output file name
s3_client = boto3.client('s3')
output_object = s3_client.get_object(Bucket=bucket, Key=output_key)
output_content = output_object['Body'].read().decode('utf-8')
print("Batch Transform Output:")
print(output_content)Conclusion
We used Amazon SageMaker to perform efficient, offline inference using Amazon SageMaker Batch Transform, including job creation without prior models or data by generating Synthetic Data.
Thanks,
Happy learning & sharing,
Sumbul








