将选定的或 Joblib 预训练的 ML 模型加载到 Sagemaker 并作为端点托管

use*_*922 5 amazon-web-services python-3.x amazon-sagemaker

如果我有一个使用 pickle 或 Joblib 训练过的模型。可以说是逻辑回归或 XGBoost。

我想将该模型作为端点托管在 AWS Sagemaker 中,而不运行训练作业。如何实现这一目标。

#Lets Say myBucketName contains model.pkl
model = joblib.load('filename.pkl')  
# X_test = Numpy Array 
model.predict(X_test)  
Run Code Online (Sandbox Code Playgroud)

我不感兴趣sklearn_estimator.fit('S3 Train, S3 Validate' ),我有经过训练的模型

Oli*_*ant 7

例如,对于 Scikit Learn,您可以从此公开演示中获得灵感https://github.com/awslabs/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/scikit_learn_randomforest/Sklearn_on_SageMaker_end2end.ipynb

步骤 1:将压缩的工件(例如 joblib)保存在 S3 中s3://<your path>/model.tar.gz

步骤2:使用反序列化函数创建推理脚本model_fn。(请注意,您还可以添加自定义推理函数input_fn,但对于 scikit predict_fnoutput_fn默认函数可以正常工作)

%%writefile inference_script.py. # Jupiter command to create file in case you're in Jupiter

import joblib
import os

def model_fn(model_dir):
    clf = joblib.load(os.path.join(model_dir, "model.joblib"))
    return clf
Run Code Online (Sandbox Code Playgroud)

步骤 3:创建一个模型,将工件与正确的容器关联起来

from sagemaker.sklearn.model import SKLearnModel

model = SKLearnModel(
    model_data='s3://<your path>/model.tar.gz',
    role='<your role>',
    entry_point='inference_script.py',
    framework_version='0.23-1')
Run Code Online (Sandbox Code Playgroud)

第 4 步:部署!

model.deploy(
    instance_type='ml.c5.large',  # choose the right instance type
    initial_instance_count=1)
Run Code Online (Sandbox Code Playgroud)