

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

# 定义指标和环境变量
<a name="automatic-model-tuning-define-metrics-variables"></a>

调优作业使用指标来评估性能，从而优化其启动的训练作业的超参数。本指南介绍如何定义指标，以便您可以使用自定义算法进行训练，或者使用 Amazon A SageMaker I 的内置算法。本指南还将介绍如何在自动模型调优 (AMT) 作业期间指定环境变量。

## 定义指标
<a name="automatic-model-tuning-define-metrics"></a>

Amazon SageMaker AI 超参数调整会解析您的机器学习算法`stdout`和`stderr`流，以查找指标，例如损失或验证精度。这些指标表明了模型在数据集上的表现。

以下部分将介绍如何使用两种类型的算法进行训练：内置算法和自定义算法。

### 使用内置算法进行训练
<a name="automatic-model-tuning-define-metrics-builtin"></a>

如果您使用 [SageMaker AI 内置算法](https://docs.aws.amazon.com/sagemaker/latest/dg/algos.html)之一，则已经为您定义了指标。此外，内置算法会自动将指标发送至超参数调优以进行优化。这些指标也会写入 Amazon CloudWatch 日志。有关更多信息，请参阅[使用亚马逊记录亚马逊 SageMaker AI 事件 CloudWatch](https://docs.aws.amazon.com/sagemaker/latest/dg/logging-cloudwatch.html)。

对于调优作业的目标指标，可选择内置算法发出的指标之一。有关可用指标的列表，请参阅 “[使用 Amazon A SageMaker I 内置算法或预训练模型” 中的模型调整部分，了解相应算法](https://docs.aws.amazon.com/sagemaker/latest/dg/algos.html)。

您可以在[调优作业](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_HyperParameterAlgorithmSpecification.html)中选择最多 40 个指标进行监控。请选择其中一个指标作为目标指标。超参数调优作业将根据目标指标，返回性能最佳的[训练作业](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeHyperParameterTuningJob.html#sagemaker-DescribeHyperParameterTuningJob-response-BestTrainingJob)。

**注意**  
超参数调优会自动发送一个额外的超参数 `_tuning_objective_metric`，将目标指标传递给调优作业，以便在训练期间使用。

### 使用自定义算法进行训练
<a name="automatic-model-tuning-define-metrics-custom"></a>

本部分将介绍如何定义自己的指标，以便使用自定义算法进行训练。使用自定义算法时，请确保算法至少将一个指标写入 `stderr` 或 `stdout`。超参数调优会解析这些流，以找到可表明模型在数据集上的表现的算法指标。

您可以通过为调优作业所监控的每个指标指定名称和正则表达式，以定义自定义指标。然后，通过 `AlgorithmSpecification` 的 `MetricDefinitions` 字段中的 `TrainingJobDefinition` 参数，将这些指标定义传递给 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateHyperParameterTuningJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateHyperParameterTuningJob.html) API。

以下显示了由训练算法写入 `stderr` 或 `stdout` 的日志的示例输出。

```
GAN_loss=0.138318;  Scaled_reg=2.654134; disc:[-0.017371,0.102429] real 93.3% gen 0.0% disc-combined=0.000000; disc_train_loss=1.374587;  Loss = 16.020744;  Iteration 0 took 0.704s;  Elapsed=0s
```

以下代码示例演示了如何在 Python 中使用正则表达式。此代码用于搜索示例日志输出，并捕获四个不同指标的数字值。

```
[
    {
        "Name": "ganloss",
        "Regex": "GAN_loss=(.*?);",
    },
    {
        "Name": "disc-combined",
        "Regex": "disc-combined=(.*?);",
    },
    {
        "Name": "discloss",
        "Regex": "disc_train_loss=(.*?);",
    },
    {
        "Name": "loss",
        "Regex": "Loss = (.*?);",
    },
]
```

在正则表达式中，圆括号 `()` 用于将正则表达式的各个部分组合在一起。
+ 对于代码示例中定义的 `loss` 指标，表达式 `(.*?);` 将捕获确切文本 `"Loss="` 与第一个分号 (`;`) 字符之间的任何字符。
+ 字符 `.` 指示正则表达式匹配任何字符。
+  字符 `*` 表示匹配零个或多个字符。
+ 字符 `?` 表示仅在 `;` 字符首次出现之前进行捕获。

代码示例中定义的损失指标将从示例输出中捕获 `Loss = 16.020744`。

选择您定义的指标之一作为调优作业的目标指标。如果您使用的是 SageMaker API，请在发送给[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateHyperParameterTuningJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateHyperParameterTuningJob.html)操作的`HyperParameterTuningJobConfig`参数的`HyperParameterTuningJobObjective`字段中指定`name`密钥的值。

## 指定环境变量
<a name="automatic-model-tuning-define-variables"></a>

SageMaker AI AMT 在调优作业中优化超参数，以找到模型性能的最佳参数。您可以使用环境变量配置调优作业，以更改其行为。您还可以在调优作业中使用训练期间使用的环境变量。

如果要使用调整作业中的环境变量或指定新的环境变量，请在 SageMaker A [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_HyperParameterTrainingJobDefinition.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_HyperParameterTrainingJobDefinition.html)I API `Environment` 中输入字符串值。将此训练作业定义传递给 [CreateHyperParameterTuningJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateHyperParameterTuningJob.html)API。

例如，可将环境变量 `SM_LOG_LEVEL` 设置为以下值，以定制 Python 容器的输出。

```
NOTSET=0
DEBUG=10
INFO=20
WARN=30
ERROR=40
CRITICAL=50
```

例如，要将日志级别设置为`10`以调试容器日志，请在内部设置环境变量 [HyperParameterTrainingJobDefinition](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_HyperParameterTrainingJobDefinition.html)，如下所示。

```
{
   "[HyperParameterTuningJobConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateHyperParameterTuningJob.html#sagemaker-CreateHyperParameterTuningJob-request-HyperParameterTuningJobConfig)": { 
   ...,
   }
   "[TrainingJobDefinition](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateHyperParameterTuningJob.html#sagemaker-CreateHyperParameterTuningJob-request-TrainingJobDefinition)": { 
      ...,
      "Environment" : [
          {
            "SM_LOG_LEVEL": 10 
          }
      ],
      ...,
   },
   ...,        
}
```