用于训练的常见数据格式
为了准备进行训练,您可以使用各种 Amazon 服务对数据进行预处理,包括 Amazon Glue、Amazon EMR、Amazon Redshift、Amazon Redshift、Amazon Relational Database Service 和 Amazon Athena。在进行预处理后,将数据发布到 Amazon S3 存储桶。对于训练,数据需要经过一系列的转换和变换,包括:
-
训练数据序列化 (由您处理)
-
训练数据反序列化 (由算法处理)
-
训练模型序列化 (由算法处理)
-
训练后的模型反序列化 (可选,由您处理)
在算法的训练部分中使用 Amazon SageMaker 时,请确保一次性上传所有数据。如果向该位置添加更多数据,则需发起新的训练调用以构建全新模型。
内置算法支持的内容类型
下表列出了一些通常支持的 ContentType
值和使用它们的算法:
内置算法的内容 ContentType | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
ContentType | 算法 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
application/x-image | 对象检测算法,语义分割 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
application/x-recordio |
对象检测算法 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
application/x-recordio-protobuf |
因子分解机、K-Means、k-NN、Latent Dirichlet Allocation、线性学习器、NTM、PCA、RCF、Sequence-to-Sequence |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
application/jsonlines |
BlazingText,DeepAR |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
image/jpeg |
对象检测算法,语义分割 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
image/png |
对象检测算法,语义分割 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
text/csv |
IP 洞察、K-Means、K-NN、潜在狄利克雷分配、线性学习器、NTM、PCA、RCF、XGBoost |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
text/libsvm |
XGBoost |
有关每种算法支持的参数的摘要,请参阅各个算法的文档或此表。
使用管道模式
在管道模式中,您的训练作业直接从 Amazon Simple Storage Service (Amazon S3) 流式传输数据。流式传输可以为训练作业提供更快的启动时间和更好的吞吐量。这与文件模式相反,这种模式下来自 Amazon S3 的数据存储在训练实例卷上。文件模式需要使用磁盘空间来存储最终的模型构件和完整的训练数据集。在管道模式下,通过直接从 Amazon S3 流式传输数据,可以减少训练实例的 Amazon Elastic Block Store 卷的大小。管道模式只需要足够的磁盘空间来存储最终模型构件。有关训练输入模式的更多详细信息,请参阅AlgorithmSpecification
。
使用 CSV 格式
很多 Amazon SageMaker 算法都支持使用 CSV 格式的数据进行训练。要使用 CSV 格式的数据进行训练,请在输入数据通道规范中,将 text/csv
指定作为 ContentType
。Amazon SageMaker 要求 CSV 文件没有标题记录,且目标变量在第一列中。要运行没有目标的自主学习算法,请指定内容类型中的标签列数。例如,在此示例中,'content_type=text/csv;label_size=0'
。有关使用 CSV 格式的笔记本示例,请参阅乳腺癌预测
使用 RecordIO 格式
在 protobuf recordIO 格式中,SageMaker 将数据集中的每个观察结果转换为二进制表示形式(一组 4 字节的浮点数),然后将其加载到 protobuf 值字段。如果您使用 Python 进行数据准备,我们强烈建议您使用这些现有的转换。但是,如果您使用的是其他语言,则下面的 protobuf 定义文件提供了可用于将数据转换为 SageMaker protobuf 格式的架构。
注意
有关说明如何将常用 numPy 数组转换为 protobuf recordIO 格式的示例,请参阅使用因子分解机分析 MNIST 数据集简介
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 可访问的 Amazon S3 位置,该位置可在 create_training_job
中作为 InputDataConfig
的一部分传递。
注意
对于所有 Amazon SageMaker 算法,InputDataConfig
中的 ChannelName
必须设置为 train
。一些算法还支持验证或测试 input
channels
。它们通常用于通过使用保留数据集来评估模型的性能。保留数据集不用于初始训练,但可用于进一步调整模型。
训练后的模型反序列化
在 create_training_job
调用的 OutputDataConfig
S3OutputPath
参数中指定的 S3 存储桶中,Amazon SageMaker 模型存储为 model.tar.gz。S3 存储桶必须与笔记本实例位于同一 Amazon 区域。您可以在创建托管模型时指定这些模型构件中的大多数模型构件。您也可以在笔记本实例中打开并查看它们。在展开 model.tar.gz
时,它将包含 model_algo-1
,后者是一个序列化的 Apache MXNet 对象。例如,您可以使用以下命令将 k-means 模型加载到内存中并进行查看:
import mxnet as mx print(mx.ndarray.load('model_algo-1'))