如果我在 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 为例,您可以从这个公开演示中获得灵感 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
、predict_fn
、output_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)
关于python-3.x - 将 Picked 或 Joblib 预训练的 ML 模型加载到 Sagemaker 并作为端点托管,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63813624/