amazon-sagemaker-examples icon indicating copy to clipboard operation
amazon-sagemaker-examples copied to clipboard

using monotonicity constraints - XGBoost as a built-in algorithm

Open csetzkorn opened this issue 2 years ago • 3 comments

Let is assume I have a dataset with 3 independent valiables/predictors/features. So far I used code along those lines successfully:

hyperparameters = {
        'max_depth': '10',
        'num_round': '100',
        'objective': 'count:poisson'
}

output_path = 's3://{}/{}/output'.format(s3_bucket_name, s3_prefix)

estimator = sagemaker.estimator.Estimator(image_uri=sagemaker.image_uris.retrieve("xgboost", region_name, "1.5-1"), 
                                          hyperparameters=hyperparameters,
                                          role=role_arn,
                                          instance_count=1, 
                                          instance_type='ml.m5.2xlarge',
                                          #instance_type='local', 
                                          volume_size=1, # 1 GB 
                                          output_path=output_path)

estimator.fit({'train': s3_input_train, 'validation': s3_input_val})

However, when I try to impose a monotonicity constraint on the 2nd feature like so:

hyperparameters = {
        'max_depth': '10',
        'num_round': '100',
        'objective': 'count:poisson',
        'tree_method': 'exact', 
        'monotone_constraints': '(0,-1,0)'
}

output_path = 's3://{}/{}/output'.format(s3_bucket_name, s3_prefix)

estimator = sagemaker.estimator.Estimator(image_uri=sagemaker.image_uris.retrieve("xgboost", region_name, "1.5-1"), 
                                          hyperparameters=hyperparameters,
                                          role=role_arn,
                                          instance_count=1, 
                                          instance_type='ml.m5.2xlarge',
                                          #instance_type='local', 
                                          volume_size=1, # 1 GB 
                                          output_path=output_path)

estimator.fit({'train': s3_input_train, 'validation': s3_input_val})

Any ideas? Maybe this is not possible for the "built-in algorithm"?

PS:

Errors:

Traceback (most recent call last): File "c:\Repos\ds_cs_ipt2\modeling\fit_occupancy_model_in_sagemaker.py", line 67, in <module> estimator.fit({'train': s3_input_train, 'validation': s3_input_val}) File "C:\Python\Python310\lib\site-packages\sagemaker\estimator.py", line 956, in fit self.latest_training_job.wait(logs=logs) File "C:\Python\Python310\lib\site-packages\sagemaker\estimator.py", line 1957, in wait self.sagemaker_session.logs_for_job(self.job_name, wait=True, log_type=logs) File "C:\Python\Python310\lib\site-packages\sagemaker\session.py", line 3798, in logs_for_job self._check_job_status(job_name, description, "TrainingJobStatus") File "C:\Python\Python310\lib\site-packages\sagemaker\session.py", line 3336, in _check_job_status raise exceptions.UnexpectedStatusException( sagemaker.exceptions.UnexpectedStatusException: Error for Training job sagemaker-xgboost-2022-05-25-11-11-03-685: Failed. Reason: AlgorithmError: framework error: Traceback (most recent call last): File "/miniconda3/lib/python3.7/site-packages/sagemaker_xgboost_container/algorithm_mode/train.py", line 233, in train_job feval=configured_feval, callbacks=callbacks, xgb_model=xgb_model, verbose_eval=False) File "/miniconda3/lib/python3.7/site-packages/xgboost/training.py", line 196, in train early_stopping_rounds=early_stopping_rounds) File "/miniconda3/lib/python3.7/site-packages/xgboost/training.py", line 51, in _train_internal bst = Booster(params, [dtrain] + [d[0] for d in evals]) File "/miniconda3/lib/python3.7/site-packages/xgboost/core.py", line 1334, in __init__ params = self._configure_constraints(params) File "/miniconda3/lib/python3.7/site-packages/xgboost/core.py", line 1400, in _configure_constraints ] = self._transform_monotone_constrains(value) File "/miniconda3/lib/python3.7/site-packages/xgboost/core.py", line 1361, in _transform_monotone_constrains constrained_features = set(value.keys

csetzkorn avatar May 25 '22 11:05 csetzkorn

I'm experiencing the same issue, and have tried setting the constraints to be a tuple and for it to be the string representation of the tuple. I followed the resolution of this issue https://github.com/aws/sagemaker-xgboost-container/issues/120 precisely, but have a similar error message to yours.

AlgorithmError: framework error: Traceback (most recent call last): File "/miniconda3/lib/python3.8/site-packages/sagemaker_containers/_trainer.py", line 84, in train entrypoint() File "/miniconda3/lib/python3.8/site-packages/sagemaker_xgboost_container/training.py", line 93, in main train(framework.training_env()) File "/miniconda3/lib/python3.8/site-packages/sagemaker_xgboost_container/training.py", line 89, in train run_algorithm_mode() File "/miniconda3/lib/python3.8/site-packages/sagemaker_xgboost_container/training.py", line 63, in run_algorithm_mode sagemaker_train( File "/miniconda3/lib/python3.8/site-packages/sagemaker_xgboost_container/algorithm_mode/train.py", line 122, in sagemaker_train validated_train_config = hyperparameters.validate(train_config) File "/miniconda3/lib/python3.8/site-packages/sagemaker_algorithm_toolkit/hyperparameter_validation.py", line 306, in validate self.hyperparameters[hp].validate_dependencies(value, dependencies) File "/miniconda3

kevin-hu-datascience avatar Aug 05 '22 00:08 kevin-hu-datascience

I would also like to know how to set these constraints via the console. I am experiencing the same issue.

StanKolpakoff avatar Jun 20 '23 22:06 StanKolpakoff

any news on this please? just looking at this again

csetzkorn avatar Oct 20 '23 18:10 csetzkorn