0
votes

I'm using AWS SageMaker to run hyperparameter tuning to optimize an XGBoost model. I'm able to do so using the example code below. My question is: How can I then take the best hyperparameter tuning job and create a model via code? I'm aware that in the AWS console I can see the best job and click on "Create model", but I want to be able to do so using Python code.

sess = sagemaker.Session()
s3_input_train = sagemaker.s3_input(s3_data='s3://{}/{}/train'.format(bucket, prefix), content_type='csv')
s3_input_validation = sagemaker.s3_input(s3_data='s3://{}/{}/valid/'.format(bucket, prefix), content_type='csv')
xgb_cont = get_image_uri(region, 'xgboost', repo_version='0.90-1')
xgb = sagemaker.estimator.Estimator(xgb_cont, role, train_instance_count=1, train_instance_type='ml.m4.4xlarge',
                                    output_path='s3://{}/{}/output'.format(bucket, prefix), sagemaker_session=sess)
xgb.set_hyperparameters(eval_metric='rmse', objective='reg:squarederror', num_round=100)
hyperparameter_ranges = {'eta': ContinuousParameter(0, 1), 'min_child_weight': ContinuousParameter(1, 10),
                        'alpha': ContinuousParameter(0, 2), 'max_depth': IntegerParameter(1, 10)}
tuner = HyperparameterTuner(xgb, 
                            objective_metric_name='validation:rmse', 
                            objective_type='Minimize',
                            hyperparameter_ranges=hyperparameter_ranges, 
                            max_jobs=20, max_parallel_jobs=10)
tuner.fit({'train': s3_input_train, 'validation': s3_input_validation})
1
Yes, as advised by Gaurav Bansal above you can call the deploy method on the HyperParameterTuner object. More info here: sagemaker.readthedocs.io/en/stable/api/training/… - Ajay Mahendru

1 Answers

0
votes

Apparently I can deploy the best model using the following code:

tuner.deploy(initial_instance_count=1, instance_type='ml.m4.xlarge')