

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

# 用於訓練的一般資料格式
<a name="cdf-training"></a>

若要準備訓練，您可以使用各種 AWS 服務預先處理資料，包括 Amazon EMR AWS Glue、Amazon Redshift、Amazon Relational Database Service 和 Amazon Athena。預先處理完畢後，請將資料發佈到 Amazon S3 儲存貯體上。為了進行訓練，資料必須先經過一系列轉換和轉變的過程，例如：
+ 訓練資料序列化 (由您處理) 
+ 訓練資料還原序列化 (由演算法處理) 
+ 訓練模型序列化 (由演算法處理) 
+ 訓練模型還原序列化 (選擇性，由您處理) 

在演算法的訓練部分使用 Amazon SageMaker AI 時，請務必一次上傳所有資料。若該位置新增了更多資料，則需進行新的訓練呼叫，以建立全新的模型。

**Topics**
+ [內建演算法支援的內容類型](#cdf-common-content-types)
+ [使用樞紐分析模式](#cdf-pipe-mode)
+ [使用 CSV 格式](#cdf-csv-format)
+ [使用 RecordIO 格式](#cdf-recordio-format)
+ [訓練模型還原序列化](#td-deserialization)

## 內建演算法支援的內容類型
<a name="cdf-common-content-types"></a>

下表列出了一些常見支援的[https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_Channel.html#SageMaker-Type-Channel-ContentType](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_Channel.html#SageMaker-Type-Channel-ContentType)值以及使用它們的算法：

內建演算法的類型


| ContentType | 演算法 | 
| --- | --- | 
| 應用程式/x - 影像 | 物件偵測演算法、語意分割 | 
| 應用程式/X - 錄音 | 物件偵測演算法 | 
| application/x-recordio-protobuf | 因式分解機, K 平均數, k-NN, Latent Dirichlet Allocation, 線性學習, NTM, PCA, RCF, Sequence-to-Sequence | 
| application/jsonlines | BlazingText, DeepAR | 
| 影像/JPEG | 物件偵測演算法、語意分割 | 
| 圖片/png | 物件偵測演算法、語意分割 | 
| text/csv | IP Insights, K 平均數, k-NN, Latent Dirichlet Allocation, 線性學習, NTM, PCA, RCF, XGBoost | 
| 文字/libsvm | XGBoost | 

如需每個演算法使用的參數摘要，請參閱文件或此[資料表](https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-algo-docker-registry-paths.html)以取得個別演算法的資訊。

## 使用樞紐分析模式
<a name="cdf-pipe-mode"></a>

在*管道模式下*，您的訓練任務會直接從 Amazon Simple Storage Service (Amazon S3) 串流資料。串流可以為訓練任務提供更快的啟動時間和更高的輸送量。這與*檔案模式*相反，Amazon S3 中的資料會存放在訓練執行個體磁碟區上。檔案模式則使用存放最終模型成品和完整訓練資料集的磁碟空間。透過以管道模式直接從 Amazon S3 串流資料，您可以減少訓練執行個體的 Amazon 彈性區塊存放區容量大小。管道模式僅需足夠磁碟空間來存放您的最終模型成品。請參閱 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AlgorithmSpecification.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AlgorithmSpecification.html)以取得訓練輸入模式的詳細資訊。

## 使用 CSV 格式
<a name="cdf-csv-format"></a>

許多 Amazon SageMaker AI 演算法能以 CSV 格式的資料支援訓練。若要使用 CSV 格式的資料進行訓練，請在輸入資料通道規格中指定**text/csv**為 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_Channel.html#SageMaker-Type-Channel-ContentType](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_Channel.html#SageMaker-Type-Channel-ContentType)。Amazon SageMaker AI 要求 CSV 檔案沒有標題記錄，且目標變數位於第一欄中。若要執行不具有目標的無監督學習演算法，請指定內容類型中標籤欄的數目。例如，在此案例中為 **'content\_type=text/csv;label\_size=0'**。有關詳細資訊，請參閱[現在，將管道模式與 CSV 資料集結合使用，以便更快地對 Amazon SageMaker AI 內建演算法進行訓練](https://aws.amazon.com/blogs/machine-learning/now-use-pipe-mode-with-csv-datasets-for-faster-training-on-amazon-sagemaker-built-in-algorithms/)。

## 使用 RecordIO 格式
<a name="cdf-recordio-format"></a>

在 protobuf recordIO 格式中，SageMaker AI 會將資料集的每項觀察都轉換為二進位表示法，成為一組 4 個位元組的浮點，再將其載入 protobuf 值欄位。如果您使用 Python 來準備資料，強烈建議您使用這些現有的轉換。不過，如果您使用其他語言，下列 protobuf 定義檔提供您用於將資料轉換為 SageMaker AI protobuf 格式的結構描述。

**注意**  
如需說明如何將常用 numPy 陣列轉換為 protobuf recordIO 格式的範例，請參閱*[入門指南：因式分解機搭配使用 MNIST](https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_amazon_algorithms/factorization_machines_mnist/factorization_machines_mnist.html)*。

```
syntax = "proto2";

 package aialgs.data;

 option java_package = "com.amazonaws.aialgorithms.proto";
 option java_outer_classname = "RecordProtos";

 // A sparse or dense rank-R tensor that stores data as doubles (float64).
 message Float32Tensor   {
     // Each value in the vector. If keys is empty, this is treated as a
     // dense vector.
     repeated float values = 1 [packed = true];

     // If key is not empty, the vector is treated as sparse, with
     // each key specifying the location of the value in the sparse vector.
     repeated uint64 keys = 2 [packed = true];

     // An optional shape that allows the vector to represent a matrix.
     // For example, if shape = [ 10, 20 ], floor(keys[i] / 20) gives the row,
     // and keys[i] % 20 gives the column.
     // This also supports n-dimensonal tensors.
     // Note: If the tensor is sparse, you must specify this value.
     repeated uint64 shape = 3 [packed = true];
 }

 // A sparse or dense rank-R tensor that stores data as doubles (float64).
 message Float64Tensor {
     // Each value in the vector. If keys is empty, this is treated as a
     // dense vector.
     repeated double values = 1 [packed = true];

     // If this is not empty, the vector is treated as sparse, with
     // each key specifying the location of the value in the sparse vector.
     repeated uint64 keys = 2 [packed = true];

     // An optional shape that allows the vector to represent a matrix.
     // For example, if shape = [ 10, 20 ], floor(keys[i] / 10) gives the row,
     // and keys[i] % 20 gives the column.
     // This also supports n-dimensonal tensors.
     // Note: If the tensor is sparse, you must specify this value.
     repeated uint64 shape = 3 [packed = true];
 }

 // A sparse or dense rank-R tensor that stores data as 32-bit ints (int32).
 message Int32Tensor {
     // Each value in the vector. If keys is empty, this is treated as a
     // dense vector.
     repeated int32 values = 1 [packed = true];

     // If this is not empty, the vector is treated as sparse with
     // each key specifying the location of the value in the sparse vector.
     repeated uint64 keys = 2 [packed = true];

     // An optional shape that allows the vector to represent a matrix.
     // For Exmple, if shape = [ 10, 20 ], floor(keys[i] / 10) gives the row,
     // and keys[i] % 20 gives the column.
     // This also supports n-dimensonal tensors.
     // Note: If the tensor is sparse, you must specify this value.
     repeated uint64 shape = 3 [packed = true];
 }

 // Support for storing binary data for parsing in other ways (such as JPEG/etc).
 // This is an example of another type of value and may not immediately be supported.
 message Bytes {
     repeated bytes value = 1;

     // If the content type of the data is known, stores it.
     // This allows for the possibility of using decoders for common formats
     // in the future.
     optional string content_type = 2;
 }

 message Value {
     oneof value {
         // The numbering assumes the possible use of:
         // - float16, float128
         // - int8, int16, int32
         Float32Tensor float32_tensor = 2;
         Float64Tensor float64_tensor = 3;
         Int32Tensor int32_tensor = 7;
         Bytes bytes = 9;
     }
 }

 message Record {
     // Map from the name of the feature to the value.
     //
     // For vectors and libsvm-like datasets,
     // a single feature with the name `values`
     // should be specified.
     map<string, Value> features = 1;

     // An optional set of labels for this record.
     // Similar to the features field above, the key used for
     // generic scalar / vector labels should be 'values'.
     map<string, Value> label = 2;

     // A unique identifier for this record in the dataset.
     //
     // Whilst not necessary, this allows better
     // debugging where there are data issues.
     //
     // This is not used by the algorithm directly.
     optional string uid = 3;

     // Textual metadata describing the record.
     //
     // This may include JSON-serialized information
     // about the source of the record.
     //
     // This is not used by the algorithm directly.
     optional string metadata = 4;

     // An optional serialized JSON object that allows per-record
     // hyper-parameters/configuration/other information to be set.
     //
     // The meaning/interpretation of this field is defined by
     // the algorithm author and may not be supported.
     //
     // This is used to pass additional inference configuration
     // when batch inference is used (e.g. types of scores to return).
     optional string configuration = 5;
 }
```

建立協定緩衝區後，請將其存放在 Amazon SageMaker AI 可存取的 Amazon S3 位置，並且可做為 `create_training_job` 中的 `InputDataConfig` 的部分來傳遞。

**注意**  
針對所有 Amazon SageMaker AI 演算法，`InputDataConfig` 的 `ChannelName` 必須設定為 `train`。部分演算法也支援驗證或測試 `input channels`。這些通常會透過使用鑑效組來評估模型的效能。鑑效組不會用於初始訓練，但可用來進一步微調模型。

## 訓練模型還原序列化
<a name="td-deserialization"></a>

Amazon SageMaker AI 模型會以 model.tar.gz 格式存放在 `create_training_job` 呼叫之 `OutputDataConfig` `S3OutputPath` 參數所指定的 S3 儲存貯體內。S3 儲存貯體必須與筆記本執行個體位於相同的 AWS 區域。建立託管模型時，這類模型成品大多可以指定。也可以在您筆記本執行個體中開啟和檢視。當 `model.tar.gz` 解壓縮後，其含有序列化的 Apache MXNet 物件 `model_algo-1`。舉例而言，您可以如下所示，將 K 平均數模型載入記憶體內並加以檢視：

```
import mxnet as mx
print(mx.ndarray.load('model_algo-1'))
```