

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# トレーニングの共通データ形式
<a name="cdf-training"></a>

トレーニングの準備として、 AWS Glue Amazon EMR、Amazon Redshift、Amazon Relational Database Service、Amazon Athena など、さまざまな AWS サービスを使用してデータを前処理できます。前処理の後、データを 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 | アルゴリズム | 
| --- | --- | 
| application/x-image | オブジェクト検出アルゴリズム、セマンティックセグメンテーション | 
| application/x-recordio | オブジェクト検出アルゴリズム | 
| application/x-recordio-protobuf | 因数分解機、K-Means、k-NN、潜在的ディリクレ割り当て、線形学習、NTM、PCA、RCF、Sequence-to-Sequence | 
| application/jsonlines | BlazingText、DeepAR | 
| image/jpeg | オブジェクト検出アルゴリズム、セマンティックセグメンテーション | 
| image/png | オブジェクト検出アルゴリズム、セマンティックセグメンテーション | 
| text/csv | IP Insights、K-Means、k-NN、潜在的ディリクレ割り当て、線形学習、NTM、PCA、RCF、XGBoost | 
| text/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 Elastic Block Store ボリュームのサイズを縮小できます。パイプモードでは、最終的なモデルアーティファクトを保存するのに十分なディスク容量が必要です。トレーニング入力モードの詳細については、[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 形式のデータをトレーニングに使うには、入力データチャネルの仕様で [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) として **text/csv** を指定します。Amazon SageMaker AI では、CSV ファイルにヘッダーレコードを含めないことと、ターゲット変数を最初の列に含める必要があります。ターゲットが設定されていない教師なし学習アルゴリズムを実行するには、コンテンツタイプでラベル列の数を指定します。たとえば、この場合は **'content\_type=text/csv;label\_size=0'** と指定します。詳細については、「[Now use Pipe mode with CSV datasets for faster training on Amazon SageMaker AI built-in algorithms](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 形式に変換する方法を示す例については、「[An Introduction to Factorization Machines with MNIST」(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 モデルは、`create_training_job` 呼び出しの `OutputDataConfig` `S3OutputPath` パラメータで指定された S3 バケットの model.tar.gz として保存されます。S3 バケットは、ノートブックインスタンスと同じ AWS リージョンにある必要があります。ホスティングモデルを作成しているときに、これらのほとんどのモデルアーティファクトを指定することができます。ノートブックインスタンスで開いて確認することもできます。`model.tar.gz` が解凍されると、`model_algo-1` が含まれています。これは、シリアル化された Apache MXNet オブジェクトです。たとえば、次を使用して k-means モデルをメモリにロードし、表示します。

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