Skip to main content
In this lesson we’ll train a model in Amazon SageMaker Studio using the SageMaker Python SDK. This guide walks through a compact, runnable notebook flow that demonstrates:
  • Defining and running a training job with the SageMaker SDK’s Estimator class.
  • Launching a Hyperparameter Tuning job to explore multiple hyperparameter combinations in parallel.
  • Inspecting and retrieving model artifacts produced by training.
What you’ll do (high-level):
  1. Open a Jupyter notebook in SageMaker Studio.
  2. Prepare data: split into train/validation/test (≈ 70% / 20% / 10%).
  3. Create and run a SageMaker training job using Estimator — provide the container image, compute resources, and IAM role.
A presentation slide titled "Demo Steps" listing three numbered steps: 01 Open Notebook, 02 Data Preparation (split dataset 70% train / 20% validation / 10% test), and 03 Create Training Job (use Estimator, specify container image, compute size, and IAM role). The slide has a dark teal background with horizontal highlighted bars and a small "© Copyright KodeKloud" note.
Overview and approach
  • We’ll use the generic Estimator from the SageMaker SDK, so we must provide the container image URI for the training algorithm. For this demo we use SageMaker’s built-in Linear Learner (regression).
  • Workflow: set hyperparameters (mini-batch size, epochs, etc.), upload CSV data to Amazon S3, call estimator.fit(…), then inspect the model artifact (model.tar.gz) in S3.
  • To accelerate experimentation, we create a Hyperparameter Tuning job to run multiple training jobs in parallel and pick the best model by an objective metric (e.g., validation RMSE).
Open SageMaker Studio, select a notebook server from the JupyterLab launcher, and open the notebook that will contain the demo code.
Screenshot of a JupyterLab interface running in AWS SageMaker, showing the Launcher with notebook and console kernels (Python, Glue, Spark) and various file-type tiles. The left sidebar shows a file browser with a highlighted notebook file (training_demo2.ipynb).
Compact, runnable notebook flow
  • The sequence below contains the main notebook steps: imports, session/role setup, load & split data, save CSVs, upload to S3, define estimator, and run training.
  • This example assumes a preprocessed CSV file (preprocessed.csv) is available in the notebook filesystem.
Define the Estimator, set hyperparameters, and start training
  • Below we configure the Estimator to use a single ml.m5.large instance for demonstration. Adjust instance type and count for larger jobs.
Screenshot of a JupyterLab/SageMaker workspace showing a CSV file (preprocessed.csv) preview with a left file browser and a large table of property data columns like latitude, longitude, bathrooms, bedrooms, floorAreaSqM and price. A mouse cursor highlights one of the cells in the table.
Monitoring and model artifacts
  • SageMaker creates a managed training job and provisions the compute instance(s). Monitor progress from SageMaker Studio (Jobs > Training) or the SageMaker Console training jobs page. Logs stream to the notebook cell output and include metrics such as validation RMSE and MSE.
  • After training completes, the model artifact (model.tar.gz) is saved to the configured S3 output prefix.
A screenshot of the Amazon SageMaker Studio web interface showing a running training job named "linear-learner-2025-05-06-13-48-11-566" with the Hyperparameters tab displayed. The table lists hyperparameter names like epochs, mini_batch_size and predictor_type (set to "regressor"), and a large cursor is visible.
Verify model artifact in the S3 Console (look under the training job’s output prefix for model.tar.gz).
A screenshot of the Amazon S3 console showing the "output/" folder containing a single object named "model.tar.gz" (1.2 KB, last modified May 6, 2025). The UI shows S3 navigation on the left and action buttons (Copy S3 URI, Download, Open, Delete) across the top.
Hyperparameter tuning to run many training jobs in parallel
  • Define ranges for the hyperparameters you want SageMaker to explore (ContinuousParameter or IntegerParameter).
  • Create a HyperparameterTuner and call fit(); the tuner launches multiple training jobs (up to max_parallel_jobs concurrently) and returns the best training job according to the specified objective metric.
By default tuner.fit(…) waits until the tuning job finishes and blocks the notebook cell. To launch asynchronously, call tuner.fit(…, wait=False) so you can continue other work while the tuner runs.
Retrieve the best tuning job programmatically
  • After tuning completes, use Boto3 or the SageMaker SDK to describe the tuning job and obtain the BestTrainingJob and its objective metric.
Practical notes and troubleshooting
  • If your model shows large RMSE values, investigate feature scaling, outliers, target distribution, and whether a linear model is appropriate. Hyperparameter tuning speeds up exploration but cannot replace good feature engineering and the right model choice.
  • Many built-in algorithms expect the label as the first column and CSV inputs without headers or index — ensure you follow the input formatting expected by the chosen algorithm.
  • For faster iteration, use smaller subsets of data or fewer epochs during development, then scale up for final training runs.
Summary — what you accomplished
  • Created a SageMaker training job with the Python SDK Estimator class.
  • Retrieved the built-in Linear Learner container image and provided it to a generic Estimator.
  • Uploaded train/validation/test CSVs to S3 and started training with estimator.fit(…).
  • Launched a HyperparameterTuner to try multiple hyperparameter combinations in parallel and retrieved the best model.
  • Verified the model artifact saved to S3 (model.tar.gz).
A presentation slide titled "Summary" showing five numbered points about an ML training workflow. The points highlight a simplified training process, an Estimator class, hyperparameter control, familiar .fit() syntax, and scalable training for parallel hyperparameter tuning.
Quick reference and links Recommended next steps
  • Try different instance types (e.g., ml.m5.2xlarge) and compare training time vs cost.
  • Replace Linear Learner with other built-in algorithms or your own training container to evaluate model performance.
  • Integrate model evaluation and model deployment (SageMaker endpoints or batch transform) as the next phase after training.

Watch Video

Practice Lab