清理 Amazon SageMaker 实验资源 - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

清理 Amazon SageMaker 实验资源

为避免产生不必要的费用,请删除不再需要的 Amazon SageMaker 实验资源。您无法通过 SageMaker 管理控制台或 Amazon SageMaker Studio UI 删除实验资源。本主题演示如何使用 Boto3 和实验开发工具包清理这些资源。有关实验开发工具包的更多信息,请参阅sagemaker-实验.

要删除实验,必须删除实验中的所有试验。要删除试验,您必须从试验中删除所有试验组件。要删除试验组件,必须从所有试验中删除该组件。

注意

试验组件可以独立于任何试验或实验而存在。您无需删除它们。如果您想重复使用它们,请注释掉tc.delete()在代码中。

使用实验开发工具包清除

使用实验软件开发工具包进行清理

import sys !{sys.executable} -m pip install sagemaker-experiments
import time from smexperiments.experiment import Experiment from smexperiments.trial import Trial from smexperiments.trial_component import TrialComponent

定义清理

def cleanup_sme_sdk(experiment): for trial_summary in experiment.list_trials(): trial = Trial.load(trial_name=trial_summary.trial_name) for trial_component_summary in trial.list_trial_components(): tc = TrialComponent.load( trial_component_name=trial_component_summary.trial_component_name) trial.remove_trial_component(tc) try: # comment out to keep trial components tc.delete() except: # tc is associated with another trial continue # to prevent throttling time.sleep(.5) trial.delete() experiment_name = experiment.experiment_name experiment.delete() print(f"\nExperiment {experiment_name} deleted")

呼叫清理

experiment_to_cleanup = Experiment.load( # Use experiment name not display name experiment_name="experiment-name") cleanup_sme_sdk(experiment_to_cleanup)

使用 Python 软件开发工具包进行清理

清理使用 Boto 3

import boto3 sm = boto3.Session().client('sagemaker')

定义清理机器人 3

def cleanup_boto3(experiment_name): trials = sm.list_trials(ExperimentName=experiment_name)['TrialSummaries'] print('TrialNames:') for trial in trials: trial_name = trial['TrialName'] print(f"\n{trial_name}") components_in_trial = sm.list_trial_components(TrialName=trial_name) print('\tTrialComponentNames:') for component in components_in_trial['TrialComponentSummaries']: component_name = component['TrialComponentName'] print(f"\t{component_name}") sm.disassociate_trial_component(TrialComponentName=component_name, TrialName=trial_name) try: # comment out to keep trial components sm.delete_trial_component(TrialComponentName=component_name) except: # component is associated with another trial continue # to prevent throttling time.sleep(.5) sm.delete_trial(TrialName=trial_name) sm.delete_experiment(ExperimentName=experiment_name) print(f"\nExperiment {experiment_name} deleted")

呼叫清理机器人 3

# Use experiment name not display name experiment_name = "experiment-name" cleanup_boto3(experiment_name)