在 sagemaker 托管模型中的哪个位置可以对端点应用预处理?

问题描述 投票:0回答:1

在 sagemaker 中创建 catboost 模型非常简单。

tabular_estimator = Estimator( role=sagemaker_role, image_uri=train_image_uri, etc ) 
.
部署端点也相当容易。

但是,一旦我这样做了,就很难再做其他事情了。就我而言,我希望在端点收到请求后对其进行一些预处理。

例如,如果有人发送:

{ model: 'endpoint_001', body: ["cow", "cow", "pig"]}   

如何找到插入 python 脚本的正确位置,用该脚本将请求转换为正确的结构,又名,

{ model: 'endpoint_001', body: ["1", "1", "2"]}   
.

模型期望什么?

我尝试过查看大量过时的、杂乱无章的 AWS 文档。

amazon-web-services amazon-sagemaker endpoint
1个回答
0
投票

要实现您所描述的转变,您可以执行以下操作:

1.创建 Python 脚本(例如 preprocess.py):

在此脚本中,定义一个函数来接受输入请求并执行所需的转换。将此脚本保存为例如 preprocess.py。

def preprocess_request(request):
    # Your custom logic to transform input strings to numerical values

2.修改估算器创建以包含自定义脚本:

为 CatBoost 模型创建“估算器”时,请确保包含 preprocess.py 脚本。

tabular_estimator = Estimator(
    role=sagemaker_role,
    image_uri=train_image_uri,
    entry_point='preprocess.py',  # Specify the name of your preprocessing script
    # other parameters...
)

在端点中应用预处理:~

您可以利用“input_fn”函数对端点应用预处理。此功能允许您自定义传入请求在传递给模型之前的处理方式。

3.使用input_fn中的自定义预处理函数:


from preprocess import preprocess_request

def input_fn(request_body, request_content_type):
    # Perform preprocessing on the request_body
    processed_body = preprocess_request(request_body)

    # Return the processed input in the format expected by your model
    return processed_body, request_content_type

4.部署模型:

像以前一样使用更新后的 input_fn 部署模型。

希望这是找到一些信息,如果内容超出了内容,请告诉我需要什么。

© www.soinside.com 2019 - 2024. All rights reserved.