

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

# 使用 Amazon SageMaker 实验记录参数和指标
<a name="train-remote-decorator-experiments"></a>

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

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

要记录 Remote 函数中的参数和指标，请选择下列方法之一：
+ 使用实验库中的实例化在远程函数`Run`中运行的 SageMaker AI SageMaker 实验。有关更多信息，请参阅[创建 Amazon A SageMaker I 实验](https://docs.aws.amazon.com/sagemaker/latest/dg/experiments-create.html)。
+ 在 `load_run` SageMaker AI 实验库中的远程函数中使用该函数。这将加载在 Remote 函数外部声明的 `Run` 实例。

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

## 使用 @remote 装饰器与 SageMaker 实验集成
<a name="train-remote-decorator-experiments-remote"></a>

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

### 使用实验创建实 SageMaker 验
<a name="train-remote-decorator-experiments-remote-create"></a>

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

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

```
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 实验
<a name="train-remote-decorator-experiments-remote-current"></a>

使用 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 启动的作业中的当前实验运行
<a name="train-remote-decorator-experiments-api"></a>

如果您的作业是使用 AP SageMaker I 启动的，您也可以加载当前运行的 A `RemoteExecutor` I 实验。以下代码示例展示了如何将 `RemoteExecutor` API 与 SageMaker 实验`load_run`函数一起使用。这样做是为了加载当前运行的 SageMaker AI 实验并在提交的作业中捕获指标`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 实验用途
<a name="train-remote-decorator-experiments-unsupported"></a>

SageMaker AI 不支持将`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
```