SageMaker Experiments SDK

0.1.45 · active · verified Thu Apr 16

sagemaker-experiments is an open-source Python library from AWS for experiment tracking within Amazon SageMaker jobs and notebooks. It allows users to create, manage, and query machine learning experiments, trials, and trial components to track model parameters, metrics, and artifacts. The library maintains an active release cadence, with frequent minor updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a SageMaker session, create an `Experiment` and `Trial`, and then use a `Tracker` to log parameters and metrics. It includes robust session handling for both SageMaker environments and local execution.

import os
import sagemaker
from sagemaker.experiments import Experiment, Trial
from sagemaker.experiments.tracker import Tracker

# Ensure a SageMaker session is available. In a SageMaker Studio or Job,
# a session is usually automatically configured.
# For local execution, ensure AWS credentials and region are set up (e.g., via environment vars).
try:
    sess = sagemaker.Session()
except Exception:
    # Fallback for local execution outside a SageMaker context if default fails
    import boto3
    print("Creating sagemaker.Session with boto3.Session for local execution.")
    sess = sagemaker.Session(boto3.Session(region_name=os.environ.get("AWS_REGION", "us-east-1")))

experiment_name = f"my-quickstart-experiment-{os.getpid()}"
trial_name = f"my-quickstart-trial-{os.getpid()}"

# 1. Create an Experiment
# Using .create() ensures a new experiment; .load() would retrieve an existing one.
my_experiment = Experiment.create(
    experiment_name=experiment_name,
    description="A simple quickstart experiment for sagemaker-experiments",
    sagemaker_session=sess
)
print(f"Created Experiment: {my_experiment.experiment_name}")

# 2. Create a Trial within the Experiment
my_trial = Trial.create(
    trial_name=trial_name,
    experiment_name=experiment_name,
    sagemaker_session=sess
)
print(f"Created Trial: {my_trial.trial_name}")

# 3. Use a Tracker to log parameters and metrics (e.g., simulating a training run)
with Tracker.create(display_name="TrainingJobComponent", sagemaker_session=sess) as tracker:
    tracker.log_parameters({"learning_rate": 0.01, "epochs": 10, "optimizer": "Adam"})
    tracker.log_metrics({"accuracy": 0.85, "loss": 0.15, "f1_score": 0.82})
    print(f"Logged data to TrialComponent: {tracker.trial_component.trial_component_name}")
    
    # Associate the tracker's automatically created trial component with our trial
    my_trial.add_trial_component(tracker.trial_component)

print("Experiment, Trial, and TrialComponent created and data logged.")
print("You can view these in SageMaker Studio under the Experiments tab.")

# Optional: Clean up created resources (uncomment to enable)
# print("Cleaning up resources...")
# my_trial.delete_all_trial_components()
# my_trial.delete()
# my_experiment.delete()
# print("Cleanup complete.")

view raw JSON →