Reputation: 23
I'm trying to implement the best estimator from a hyperparameter tuning job into a pipeline object to deploy an endpoint.
I've read the docs in a best effort to include the results from the tuning job in the pipeline, but I'm having trouble creating the Model() class object.
# This is the hyperparameter tuning job
tuner.fit({'train': s3_train, 'validation': s3_val},
include_cls_metadata=False)
#With a standard Model (Not from the tuner) the process was as follows:
scikit_learn_inferencee_model_name = sklearn_preprocessor.create_model()
xgb_model_name = Model(model_data=xgb_model.model_data, image=xgb_image)
model_name = 'xgb-inference-pipeline-' + timestamp_prefix
endpoint_name = 'xgb-inference-pipeline-ep-' + timestamp_prefix
sm_model = PipelineModel(
name=model_name,
role=role,
models=[
scikit_learn_inferencee_model_name,
xgb_model_name])
sm_model.deploy(initial_instance_count=1, instance_type='ml.c4.xlarge',
endpoint_name=endpoint_name)
I would like to be able to cleanly instantiate a model object using my results from the tuning job and pass it into the PipelineModel object. Any guidance is appreciated.
Upvotes: 2
Views: 644
Reputation: 1213
I think you are on the right track. Do you get any error? Refer this notebook for instantiating the model from the tuner and use in inference pipeline.
Editing previous response based on the comment. To create model from the best training job of the hyperparameter tuning job, you can use below snippet
from sagemaker.tuner import HyperparameterTuner
from sagemaker.estimator import Estimator
from sagemaker.model import Model
# Attach to an existing hyperparameter tuning job.
xgb_tuning_job_name = 'my_xgb_hpo_tuning_job_name'
xgb_tuner = HyperparameterTuner.attach(xgb_tuning_job_name)
# Get the best XGBoost training job name from the HPO job
xgb_best_training_job = xgb_tuner.best_training_job()
print(xgb_best_training_job)
# Attach estimator to the best training job name
xgb_best_estimator = Estimator.attach(xgb_best_training_job)
# Create model to be passed to the inference pipeline
xgb_model = Model(model_data=xgb_best_estimator.model_data,
role=sagemaker.get_execution_role(),
image=xgb_best_estimator.image_name)
Upvotes: 1