使用 Amazon SageMaker 实验记录参数和指标 - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

使用 Amazon SageMaker 实验记录参数和指标

本指南介绍如何使用 Amazon SageMaker 实验记录参数和指标。 SageMaker实验由运行组成,每次运行都包含单个模型训练交互组件的所有输入、参数、配置和结果。

您可以使用 @remote 装饰器或 RemoteExecutor API 记录来自 Remote 函数的参数和指标。

要记录 Remote 函数中的参数和指标,请选择下列方法之一:

  • 使用 SageMaker 实验库中的实例化在远程函数中运行Run的 SageMaker 实验。有关更多信息,请参阅创建 Amazon SageMaker 实验

  • load_run SageMaker实验库中的远程函数中使用该函数。这将加载在 Remote 函数外部声明的 Run 实例。

以下各节介绍如何使用前面列出的方法通过 SageMaker 实验运行创建和跟踪谱系。这些章节还描述了 SageMaker 培训不支持的案例。

使用 @remote 装饰器与 SageMaker 实验集成

您可以在中实例化实验 SageMaker,也可以从远程函数内部加载当前 SageMaker实验。以下部分说明如何使用任一方法。

使用实验创建实 SageMaker 验

您可以创建实验中运行的 SageMaker 实验。为此,您需要将实验名称、运行名称和其他参数传递入 Remote 函数中。

以下代码示例导入实验名称、运行名称以及每次运行期间要记录的参数。在训练循环中,会随着时间的推移记录参数 param_1param_2。常用参数可能包括批处理大小或纪元。在此示例中,在训练循环中,会随着时间的推移记录运行的指标 metric_ametric_b。其他常见指标可能包括 accuracyloss

from sagemaker.remote_function import remote from sagemaker.experiments.run import Run # Define your remote function @remote def train(value_1, value_2, exp_name, run_name): ... ... #Creates the experiment with Run( experiment_name=exp_name, run_name=run_name, ) as run: ... #Define values for the parameters to log run.log_parameter("param_1", value_1) run.log_parameter("param_2", value_2) ... #Define metrics to log run.log_metric("metric_a", 0.5) run.log_metric("metric_b", 0.1) # Invoke your remote function train(1.0, 2.0, "my-exp-name", "my-run-name")

使用 @remote 装饰器启动的作业加载当前 SageMaker 实验

使用 SageMaker 实验库中的load_run()函数从运行上下文中加载当前运行对象。您也可以在 Remote 函数中使用 load_run() 函数。加载由运行对象上的 with 语句本地初始化的运行对象,如以下代码示例所示。

from sagemaker.experiments.run import Run, load_run # Define your remote function @remote def train(value_1, value_2): ... ... with load_run() as run: run.log_metric("metric_a", value_1) run.log_metric("metric_b", value_2) # Invoke your remote function with Run( experiment_name="my-exp-name", run_name="my-run-name", ) as run: train(0.5, 1.0)

加载在使用 RemoteExecutor API 启动的作业中的当前实验运行

如果您的作业是使用 RemoteExecutor API 启动的,您也可以加载当前运行的 SageMaker 实验。以下代码示例展示了如何将 RemoteExecutor API 与 SageMaker 实验load_run函数一起使用。您这样做是为了加载当前运行的 SageMaker 实验并在提交的作业中捕获指标RemoteExecutor

from sagemaker.experiments.run import Run, load_run def square(x): with load_run() as run: result = x * x run.log_metric("result", result) return result with RemoteExecutor( max_parallel_job=2, instance_type="ml.m5.large" ) as e: with Run( experiment_name="my-exp-name", run_name="my-run-name", ): future_1 = e.submit(square, 2)

使用 @remote 装饰器为代码添加注释时,不支持 SageMaker 实验用途

SageMaker 不支持将Run类型对象传递给 @remote 函数或使用全局Run对象。以下示例显示了将引发 SerializationError 的代码。

以下代码示例尝试将 Run 类型对象传递给 @remote 装饰器,但会生成错误。

@remote def func(run: Run): run.log_metrics("metric_a", 1.0) with Run(...) as run: func(run) ---> SerializationError caused by NotImplementedError

以下代码示例尝试使用在 Remote 函数外部实例化的全局 run 对象。在代码示例中,train() 函数是在 with Run 上下文中定义的,中引用了全局运行对象。在调用 train() 时,它会生成一个错误。

with Run(...) as run: @remote def train(metric_1, value_1, metric_2, value_2): run.log_parameter(metric_1, value_1) run.log_parameter(metric_2, value_2) train("p1", 1.0, "p2", 0.5) ---> SerializationError caused by NotImplementedError