如何在 Sagemaker 2 中使用序列化器和反序列化器 [英] How to use Serializer and Deserializer in Sagemaker 2

查看:36
本文介绍了如何在 Sagemaker 2 中使用序列化器和反序列化器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 conda_python3 内核启动 Sagemaker 笔记本,并遵循 示例 随机砍伐森林.

I spin up a Sagemaker notebook using the conda_python3 kernel, and follow the example Notebook for Random Cut Forest.

在撰写本文时, 附带的 Sagemaker SDKconda_python3 是 1.72.0 版本,但我想使用新功能,所以我更新了我的 notebook 以使用最新的

As of this writing, the Sagemaker SDK that comes with conda_python3 is version 1.72.0, but I want to use new features, so I update my notebook to use the latest

%%bash
pip install -U sagemaker

我看到它更新了.

print(sagemaker.__version__)

# 2.4.1

从 1.x 版到 2.x 版的变化是 序列化器/反序列化器类

A change from version 1.x to 2.x was the serializer/deserializer classes

以前(在 1.72.0 版中)我会更新我的预测器以使用正确的序列化器/反序列化器,并且可以在我的模型上运行推理

Previously (in version 1.72.0) I'd update my predictor to use the proper serializer/deserializer, and could run inference on my model

from sagemaker.predictor import csv_serializer, json_deserializer


rcf_inference = rcf.deploy(
    initial_instance_count=1,
    instance_type='ml.m4.xlarge',
)

rcf_inference.content_type = 'text/csv'
rcf_inference.serializer = csv_serializer
rcf_inference.accept = 'application/json'
rcf_inference.deserializer = json_deserializer

results = rcf_inference.predict(some_numpy_array)

(注意这一切都来自 示例

我尝试像这样使用 sagemaker 2.4.1 复制它

I try and replicate this using sagemaker 2.4.1 like so

from sagemaker.deserializers import JSONDeserializer
from sagemaker.serializers import CSVSerializer

rcf_inference = rcf.deploy(
    initial_instance_count=1,
    instance_type='ml.m5.xlarge',
    serializer=CSVSerializer,
    deserializer=JSONDeserializer
)

results = rcf_inference.predict(some_numpy_array)

我收到一个错误

TypeError: serialize() missing 1 required positional argument: 'data'

我知道我错误地使用了serliaizer/deserializer,但找不到关于如何使用它的好文档

I know I'm using the serliaizer/deserializer incorrectly, but can't find good documentation on how this should be used

推荐答案

为了使用新的序列化器/反序列化器,你需要初始化它们,例如:

in order to use the new serializers/deserializers, you will need to init them, for example:

from sagemaker.deserializers import JSONDeserializer
from sagemaker.serializers import CSVSerializer

rcf_inference = rcf.deploy(
    initial_instance_count=1,
    instance_type='ml.m5.xlarge',
    serializer=CSVSerializer(),
    deserializer=JSONDeserializer()
)

这篇关于如何在 Sagemaker 2 中使用序列化器和反序列化器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆