sagemaker.core.serializers.base#
Implements base methods for serializing data for an inference endpoint.
Classes
Abstract base class for creation of new serializers. |
|
|
Serialize data of various formats to a CSV-formatted string. |
|
Serialize data in any file by extracting raw bytes from the file. |
|
Serialize data by returning data without modification. |
|
Serialize data to a JSON Lines formatted string. |
|
Serialize data to a JSON formatted string. |
|
Serialize data of various formats to a LibSVM-formatted string. |
|
Serialize data to a buffer using the .npy format. |
|
Serialize a NumPy array for an inference request. |
|
Abstract base class for creation of new serializers. |
|
Serialize a sparse matrix to a buffer using the .npz format. |
|
Encode the string to utf-8 bytes. |
|
Serialize torch.Tensor to a buffer by converting tensor to numpy and call NumpySerializer. |
- class sagemaker.core.serializers.base.BaseSerializer[source]#
Bases:
ABCAbstract base class for creation of new serializers.
Provides a skeleton for customization requiring the overriding of the method serialize and the class attribute CONTENT_TYPE.
- abstract property CONTENT_TYPE#
The MIME type of the data sent to the inference endpoint.
- class sagemaker.core.serializers.base.CSVSerializer(content_type='text/csv')[source]#
Bases:
SimpleBaseSerializerSerialize data of various formats to a CSV-formatted string.
- class sagemaker.core.serializers.base.DataSerializer(content_type='file-path/raw-bytes')[source]#
Bases:
SimpleBaseSerializerSerialize data in any file by extracting raw bytes from the file.
- class sagemaker.core.serializers.base.IdentitySerializer(content_type='application/octet-stream')[source]#
Bases:
SimpleBaseSerializerSerialize data by returning data without modification.
This serializer may be useful if, for example, you’re sending raw bytes such as from an image file’s .read() method.
- class sagemaker.core.serializers.base.JSONLinesSerializer(content_type='application/jsonlines')[source]#
Bases:
SimpleBaseSerializerSerialize data to a JSON Lines formatted string.
- serialize(data)[source]#
Serialize data of various formats to a JSON Lines formatted string.
- Parameters:
data (object) – Data to be serialized. The data can be a string, iterable of JSON serializable objects, or a file-like object.
- Returns:
- The data serialized as a string containing newline-separated
JSON values.
- Return type:
str
- class sagemaker.core.serializers.base.JSONSerializer(content_type='application/json')[source]#
Bases:
SimpleBaseSerializerSerialize data to a JSON formatted string.
- class sagemaker.core.serializers.base.LibSVMSerializer(content_type='text/libsvm')[source]#
Bases:
SimpleBaseSerializerSerialize data of various formats to a LibSVM-formatted string.
The data must already be in LIBSVM file format: <label> <index1>:<value1> <index2>:<value2> …
It is suitable for sparse datasets since it does not store zero-valued features.
- serialize(data)[source]#
Serialize data of various formats to a LibSVM-formatted string.
- Parameters:
data (object) – Data to be serialized. Can be a string or a file-like object.
- Returns:
The data serialized as a LibSVM-formatted string.
- Return type:
str
- Raises:
ValueError – If unable to handle input format
- class sagemaker.core.serializers.base.NumpySerializer(dtype=None, content_type='application/x-npy')[source]#
Bases:
SimpleBaseSerializerSerialize data to a buffer using the .npy format.
- class sagemaker.core.serializers.base.RecordSerializer(content_type='application/x-recordio-protobuf')[source]#
Bases:
SimpleBaseSerializerSerialize a NumPy array for an inference request.
- class sagemaker.core.serializers.base.SimpleBaseSerializer(content_type='application/json')[source]#
Bases:
BaseSerializerAbstract base class for creation of new serializers.
This class extends the API of :class:~`sagemaker.serializers.BaseSerializer` with more user-friendly options for setting the Content-Type header, in situations where it can be provided at init and freely updated.
- property CONTENT_TYPE#
The data MIME type set in the Content-Type header on prediction endpoint requests.
- class sagemaker.core.serializers.base.SparseMatrixSerializer(content_type='application/x-npz')[source]#
Bases:
SimpleBaseSerializerSerialize a sparse matrix to a buffer using the .npz format.
- serialize(data)[source]#
Serialize a sparse matrix to a buffer using the .npz format.
Sparse matrices can be in the
csc,csr,bsr,diaorcooformats.- Parameters:
data (scipy.sparse.spmatrix) – The sparse matrix to serialize.
- Returns:
A buffer containing the serialized sparse matrix.
- Return type:
io.BytesIO
- class sagemaker.core.serializers.base.StringSerializer(content_type='text/plain')[source]#
Bases:
SimpleBaseSerializerEncode the string to utf-8 bytes.
- class sagemaker.core.serializers.base.TorchTensorSerializer(content_type='tensor/pt')[source]#
Bases:
SimpleBaseSerializerSerialize torch.Tensor to a buffer by converting tensor to numpy and call NumpySerializer.
- Parameters:
data (object) – Data to be serialized. The data must be of torch.Tensor type.
- Returns:
The data serialized as raw-bytes from the input.
- Return type:
raw-bytes