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

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

清理 Amazon SageMaker 实验资源

为避免产生不必要的费用,删除 Amazon SageMaker 您不再需要的实验资源。您不能通过 SageMaker 管理人员 Amazon SageMaker 单片UI。此主题将向您展示如何使用Boto3和实验SDK清理这些资源。有关实验SDK的更多信息,请参阅 剪草师-实验.

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

注意

试验组件可以独立于任何试验或实验而存在。您不必删除它们。如果您想重新使用它们,请发表评论 tc.delete() 代码。

使用实验SDK清理

使用实验SDK进行清理

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

定义cleanup_sme_sdk

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")

呼叫清理_sme_sdk

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

使用PythonSDK清理(Boto3)

使用Boto3清洁

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

定义清理boto3

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")

清理呼叫_boto3

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