

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

# Amazon SageMaker AI 提供訓練資訊的方式
<a name="your-algorithms-training-algo-running-container"></a>

本節會說明 SageMaker AI 如何讓 Docker 容器可以使用訓練資訊，如訓練資料、超參數和其他組態資訊等等。

當您將 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html) 請求傳送至 SageMaker AI 來啟動模型訓練時，您需要指定包含訓練演算法之 Docker 映像的 Amazon Elastic Container Registry (Amazon ECR) 路徑。您亦需指定 Amazon Simple Storage Service (Amazon S3) 的位置，其為訓練資料和演算法專屬參數的存放位置。SageMaker AI 提供這項資訊供 Docker 容器使用，讓您的訓練演算法也可以利用。本節說明我們如何讓這項資訊可供 Docker 容器使用。如需建立訓練工作的資訊，請參閱 `CreateTrainingJob`。有關 SageMaker AI 容器組織資訊的方式的詳細資訊，請參閱 [SageMaker 訓練和推論工具組](amazon-sagemaker-toolkits.md)。

**Topics**
+ [超參數](#your-algorithms-training-algo-running-container-hyperparameters)
+ [環境變數](#your-algorithms-training-algo-running-container-environment-variables)
+ [輸入資料組態](#your-algorithms-training-algo-running-container-inputdataconfig)
+ [訓練資料](#your-algorithms-training-algo-running-container-trainingdata)
+ [分散式訓練組態](#your-algorithms-training-algo-running-container-dist-training)

## 超參數
<a name="your-algorithms-training-algo-running-container-hyperparameters"></a>

 SageMaker AI 讓 `CreateTrainingJob` 請求中的超參數可以在 `/opt/ml/input/config/hyperparameters.json` 檔案的 Docker 容器使用。

以下是 `hyperparameters.json` 中的超參數組態範例，用來指定在 `CreateTrainingJob` 作業中 [XGBoost](https://docs.aws.amazon.com/sagemaker/latest/dg/xgboost.html) 的 `num_round` 和 `eta` 超參數。

```
{
    "num_round": "128",
    "eta": "0.001"
}
```

如需可用於 SageMaker AI 內建 XGBoost 演算法的超參數完整清單，請參閱 [XGBoost 超參數](https://docs.aws.amazon.com/sagemaker/latest/dg/xgboost_hyperparameters.html)。

您可以調整的超參數，依您正在訓練的演算法而定。如需可用於 SageMaker AI 內建演算法的超參數清單，可以在[使用 Amazon SageMaker AI 內建演算法或預訓練模型](https://docs.aws.amazon.com/sagemaker/latest/dg/algos.html)內的演算法連結下的**超參數**中找到。

## 環境變數
<a name="your-algorithms-training-algo-running-container-environment-variables"></a>

SageMaker AI 會在您的容器中設定下列環境變數：
+ TRAINING\$1JOB\$1NAME – 在請求 `CreateTrainingJob` 的 `TrainingJobName` 參數中指定。
+ TRAINING\$1JOB\$1ARN – 訓練工作的 Amazon Resource Name (ARN)，會被傳回，以做為 `CreateTrainingJob` 回應中的 `TrainingJobArn`。
+ `CreateTrainingJob` 請求中[環境](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html#sagemaker-CreateTrainingJob-request-Environment)參數指定的所有環境變數。

## 輸入資料組態
<a name="your-algorithms-training-algo-running-container-inputdataconfig"></a>

SageMaker AI 讓 `CreateTrainingJob` 請求中的 `InputDataConfig` 參數資料通道資訊，可在 Docker 容器中的 `/opt/ml/input/config/inputdataconfig.json` 檔案中取得。

舉例來說，假設您在請求中指定三個資料通道 (`train`、`evaluation` 與 `validation`)，則 SageMaker AI 會提供下列 JSON：

```
{
  "train" : {"ContentType":  "trainingContentType",
             "TrainingInputMode": "File",
             "S3DistributionType": "FullyReplicated",
             "RecordWrapperType": "None"},
  "evaluation" : {"ContentType":  "evalContentType",
                  "TrainingInputMode": "File",
                  "S3DistributionType": "FullyReplicated",
                  "RecordWrapperType": "None"},
  "validation" : {"TrainingInputMode": "File",
                  "S3DistributionType": "FullyReplicated",
                  "RecordWrapperType": "None"}
}
```

**注意**  
如上所示，SageMaker AI 僅會將各個資料通道的相關資訊 (例如通道名稱、內容類型) 提供給容器。若您指定 EFS 或 FSxLustre 作為輸入資料來源，`S3DistributionType` 將設為 `FullyReplicated`。

## 訓練資料
<a name="your-algorithms-training-algo-running-container-trainingdata"></a>

[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateTrainingJob.html) 請求中 `AlgorithmSpecification` 的 `TrainingInputMode` 參數會指定如何將訓練資料集提供給您的容器。以下是可用的輸入模式：
+ **`File` 模式**

  如果您使用 `File` 模式作為 `TrainingInputMode` 的值，SageMaker AI 會在容器中設定下列參數。
  + 您的 `TrainingInputMode` 參數以 “檔案” 形式被寫入 `inputdataconfig.json`。
  + 您的資料通道目錄被寫入 `/opt/ml/input/data/channel_name`。

  如果您使用 `File` 模式，SageMaker AI 會為每個通道建立一個目錄。例如，您擁有三個通道，分別名為 `training`、`validation` 和 `testing`，則 SageMaker AI 會在 Docker 容器中建立三個目錄：
  + `/opt/ml/input/data/training`
  + `/opt/ml/input/data/validation`
  + `/opt/ml/input/data/testing`

  `File` 模式支援下列資料來源：
  + Amazon Simple Storage Service (Amazon S3)
  + Amazon Elastic File System (Amazon EFS)
  + Amazon FSx for Lustre
**注意**  
使用檔案系統資料來源 (例如 Amazon EFS 和 Amazon FSx) 的通道必須使用 `File` 模式。在這種情況下，通道中提供的目錄路徑掛載於 `/opt/ml/input/data/channel_name`。
+ **`FastFile` 模式**

  如果您使用 `FastFile` 模式作為您的 `TrainingInputNodeParameter`，SageMaker AI 會在您的容器中設定下列參數。
  + 與 `File` 模式類似，在 `FastFile` 模式下，您的 `TrainingInputMode` 參數以 “檔案” 形式被寫入 `inputdataconfig.json`。
  + 您的資料通道目錄被寫入 `/opt/ml/input/data/channel_name`。

  `FastFile` 模式支援下列資料來源。
  + Amazon S3

  如果您使用 `FastFile` 模式，通道目錄會以唯讀權限掛載。

  過去 `File` 模式一向早於 `FastFile` 模式。為了確保向下相容性，只要將 `TrainingInputMode` 參數設定為 `inputdataconfig.json.` 內的 `File`，支援 `File` 模式的演算法也可以順暢地使用 `FastFile` 模式。
**注意**  
使用 `FastFile` 模式的通道必須使用 “S3Prefix” 的 `S3DataType`。  
`FastFile` 模式會顯示資料夾視圖，使用斜線 (`/`) 做為分隔符號，將 Amazon S3 物件分成多個資料夾。`S3Uri` 字首不應對應至部分的資料夾名稱。例如，如果 Amazon S3 資料集包含 `s3://amzn-s3-demo-bucket/train-01/data.csv`，則 `s3://amzn-s3-demo-bucket/train` 或 `s3://amzn-s3-demo-bucket/train-01` 不可做為 `S3Uri` 字首。  
建議在結尾使用斜線來定義對應於資料夾的通道。例如，資料夾 `train-01` 的 `s3://amzn-s3-demo-bucket/train-01/` 通道。結尾沒有斜線時，若有其他資料夾 `s3://amzn-s3-demo-bucket/train-011/` 或檔案 `s3://amzn-s3-demo-bucket/train-01.txt/` 存在，則通道將不明確。
+ **`Pipe` 模式**
  + `TrainingInputMode` 參數寫入 `inputdataconfig.json`：“管道”
  + Docker 容器中的資料通道目錄：`/opt/ml/input/data/channel_name_epoch_number`
  + 支援的資料來源：Amazon S3

  您需要從每個通道的個別的管道讀取。例如，您擁有三個通道，且名稱分別為 `training`、`validation` 與 `testing`，則需要從下列管道讀取：
  + `/opt/ml/input/data/training_0, /opt/ml/input/data/training_1, ...`
  + `/opt/ml/input/data/validation_0, /opt/ml/input/data/validation_1, ...`
  + `/opt/ml/input/data/testing_0, /opt/ml/input/data/testing_1, ...`

  請循序讀取管道。例如，如果您有名為 `training` 的通道，請依此順序讀取管道：

  1. 在讀取模式中開啟 `/opt/ml/input/data/training_0`，並閱讀至檔案結尾 (EOF)，或者您已完成第一個 epoch，則請盡快關閉管道檔案。

  1. 關閉第一個管道檔案之後，請尋找 `/opt/ml/input/data/training_1` 並讀取它，直到完成第二個 epoch，以此類推。

  若指定 epoch 的檔案尚不存在，則您可能需要重試執行程式碼，直到該管道建立為止。通道類型之間並沒有順序限制。例如，您可以讀取 `training` 通道的多個 epoch，但只有在準備好的時候才開始讀取 `validation` 通道。或者，若演算法有需求，您亦可同時讀取這兩個管道。

  如需 Jupyter 筆記本的範例，示範如何在自有的容器使用管道模式，請參閱 [Bring your own pipe-mode algorithm to Amazon SageMaker AI](https://github.com/aws/amazon-sagemaker-examples/blob/main/advanced_functionality/pipe_bring_your_own/pipe_bring_your_own.ipynb)。

  

SageMaker AI 模型訓練支援高效能 S3 Express One Zone 目錄儲存貯體，作為檔案模式、快速檔案模式和管道模式的資料輸入位置。若要使用 S3 Express One Zone，請輸入 S3 Express One Zone 目錄儲存貯體的位置，而非 Amazon S3 一般用途儲存貯體。為 IAM 角色提供 ARN，其中包含必要的存取控制和許可政策。如需詳細資訊，請參閱 [AmazonSageMakerFullAccesspolicy](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AmazonSageMakerFullAccess.html)。您只能使用 Amazon S3 受管金鑰 (SSE-S3) 在伺服器端加密的目錄儲存貯體中加密 SageMaker AI 輸出資料。目前不支援使用 AWS KMS 金鑰 (SSE-KMS) 進行伺服器端加密，以將 SageMaker AI 輸出資料儲存在目錄儲存貯體中。如需詳細資訊，請參閱 [S3 Express One Zone](https://docs.aws.amazon.com/AmazonS3/latest/userguide/s3-express-one-zone.html)。

## 分散式訓練組態
<a name="your-algorithms-training-algo-running-container-dist-training"></a>

如要使用多個容器執行分散式訓練，SageMaker AI 會在 `/opt/ml/input/config/resourceconfig.json` 檔案中提供所有容器的資訊。

此 JSON 檔案包含所有容器的資訊，讓您可以啟用容器間的通訊。且 `File` 與 `Pipe` 模式演算法皆可透過 SageMaker AI 使用此檔案。該檔案提供下列資訊：
+ `current_host` — 容器網路上目前容器的名稱。例如 `algo-1`。您可隨時變更主機值。但請勿針對此變數撰寫具備特定值的程式碼。
+ `hosts` — 容器網路上所有容器名稱的清單，依詞典編纂方式排序。例如，三節點叢集的排序為：`["algo-1", "algo-2", "algo-3"]`。各容器可利用這些名稱，尋找容器網路上其他容器的位址。您可隨時變更主機值。但請勿針對這些變數撰寫具備特定值的程式碼。
+ `network_interface_name` — 對您的容器公開的網路介面名稱。例如，執行訊息傳遞介面 (MPI) 的容器可以使用這項資訊來設定網路介面名稱。
+ `/etc/hostname` 或 `/etc/hosts` 檔案中的資訊可能有誤，請勿使用該資訊。
+ 主機名稱資訊可能無法立即提供演算法容器使用。當叢集中的節點可供使用時，建議您在主機名稱解析作業中新增重試政策。

以下是三節點叢集的第 1 節點範例檔案：

```
{
    "current_host": "algo-1",
    "hosts": ["algo-1","algo-2","algo-3"],
    "network_interface_name":"eth1"
}
```