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

Load a Picked or Joblib Pre trained ML Model to Sagemaker and host as endpoint

如果我在 Using pickle 或 Joblib 中有经过训练的模型。 让我们说它的逻辑回归或 XGBoost。

我想在没有 运行 训练作业的情况下将该模型作为端点托管在 AWS Sagemaker 中。 如何实现。

#Lets Say myBucketName contains model.pkl
model = joblib.load('filename.pkl')  
# X_test = Numpy Array 
model.predict(X_test)  

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

以 Scikit Learn 为例,您可以从这个 public 演示中获得灵感 https://github.com/awslabs/amazon-sagemaker-examples/blob/master/sagemaker-python-sdk/scikit_learn_randomforest/Sklearn_on_SageMaker_end2end.ipynb

第 1 步:在 s3://<your path>/model.tar.gz

中保存在 S3 中压缩的工件(例如 joblib)

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

%%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

第 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')

第 4 步:部署!

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