

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 定義指標和環境變數
<a name="automatic-model-tuning-define-metrics-variables"></a>

調校任務會使用評估效能的指標，針對啟動的訓練任務將超參數最佳化。本指南說明如何定義指標，以便您使用自訂演算法進行訓練，或使用 Amazon SageMaker AI 的內建演算法。內文也說明如何在自動模型調校 (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 Logs。如需更多資訊，請參閱[使用 Amazon CloudWatch 記錄 Amazon SageMaker AI 事件](https://docs.aws.amazon.com/sagemaker/latest/dg/logging-cloudwatch.html)。

針對調校任務的目標指標，請選擇其中一個指標，以便內建演算法將其發出。如需可用的指標清單，請參閱模型調校一節，了解[使用 Amazon SageMaker AI 內建演算法或預先訓練的模型](https://docs.aws.amazon.com/sagemaker/latest/dg/algos.html)適當之演算法。

您最多可選擇 40 個指標以在[調校任務](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_HyperParameterAlgorithmSpecification.html)中進行監控。選取其中一個指標作為目標指標。超參數調校任務會傳回針對目標指標執行效能最佳的[訓練任務](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 (regex) 中的常規表達式。這會用來搜尋範例日誌輸出，並擷取四個不同指標的數值。

```
[
    {
        "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 AI [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_HyperParameterTrainingJobDefinition.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_HyperParameterTrainingJobDefinition.html) 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 
          }
      ],
      ...,
   },
   ...,        
}
```