{"library":"sagemaker-experiments","title":"SageMaker Experiments SDK","description":"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.","language":"python","status":"active","last_verified":"Thu Apr 16","install":{"commands":["pip install sagemaker-experiments"],"cli":null},"imports":["from sagemaker.experiments import Experiment","from sagemaker.experiments import Trial","from sagemaker.experiments.tracker import Tracker"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import os\nimport sagemaker\nfrom sagemaker.experiments import Experiment, Trial\nfrom sagemaker.experiments.tracker import Tracker\n\n# Ensure a SageMaker session is available. In a SageMaker Studio or Job,\n# a session is usually automatically configured.\n# For local execution, ensure AWS credentials and region are set up (e.g., via environment vars).\ntry:\n    sess = sagemaker.Session()\nexcept Exception:\n    # Fallback for local execution outside a SageMaker context if default fails\n    import boto3\n    print(\"Creating sagemaker.Session with boto3.Session for local execution.\")\n    sess = sagemaker.Session(boto3.Session(region_name=os.environ.get(\"AWS_REGION\", \"us-east-1\")))\n\nexperiment_name = f\"my-quickstart-experiment-{os.getpid()}\"\ntrial_name = f\"my-quickstart-trial-{os.getpid()}\"\n\n# 1. Create an Experiment\n# Using .create() ensures a new experiment; .load() would retrieve an existing one.\nmy_experiment = Experiment.create(\n    experiment_name=experiment_name,\n    description=\"A simple quickstart experiment for sagemaker-experiments\",\n    sagemaker_session=sess\n)\nprint(f\"Created Experiment: {my_experiment.experiment_name}\")\n\n# 2. Create a Trial within the Experiment\nmy_trial = Trial.create(\n    trial_name=trial_name,\n    experiment_name=experiment_name,\n    sagemaker_session=sess\n)\nprint(f\"Created Trial: {my_trial.trial_name}\")\n\n# 3. Use a Tracker to log parameters and metrics (e.g., simulating a training run)\nwith Tracker.create(display_name=\"TrainingJobComponent\", sagemaker_session=sess) as tracker:\n    tracker.log_parameters({\"learning_rate\": 0.01, \"epochs\": 10, \"optimizer\": \"Adam\"})\n    tracker.log_metrics({\"accuracy\": 0.85, \"loss\": 0.15, \"f1_score\": 0.82})\n    print(f\"Logged data to TrialComponent: {tracker.trial_component.trial_component_name}\")\n    \n    # Associate the tracker's automatically created trial component with our trial\n    my_trial.add_trial_component(tracker.trial_component)\n\nprint(\"Experiment, Trial, and TrialComponent created and data logged.\")\nprint(\"You can view these in SageMaker Studio under the Experiments tab.\")\n\n# Optional: Clean up created resources (uncomment to enable)\n# print(\"Cleaning up resources...\")\n# my_trial.delete_all_trial_components()\n# my_trial.delete()\n# my_experiment.delete()\n# print(\"Cleanup complete.\")","lang":"python","description":"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}