Search code examples
xgboostamazon-sagemaker

xgboost sagemaker train failure


I have type error when I run for training on sagemaker by using xgboost conatiner. Please advise me to fix the issue.

container = 'southeast-2','783357654285.dkr.ecr.ap-southeast-2.amazonaws.com/sagemaker- xgboost:latest'`

train_input = TrainingInput(s3_data='s3://{}/train'.format(bucket, prefix), content_type='csv')
validation_input = TrainingInput(s3_data='s3://{}/validation/'.format(bucket, prefix), content_type='csv')

sess = sagemaker.Session()

xgb = sagemaker.estimator.Estimator(
container,
role, 
instance_count=1,
instance_type='ml.t2.medium',
output_path='s3://{}/output'.format(bucket, prefix),
sagemaker_session=sess
)

xgb.set_hyperparameters(
max_depth=5,
eta=0.1,
gamma=4,
min_child_weight=6,
subsample=0.8,
silent=0,
objective="binary:logistic",
num_round=25,
)

xgb.fit({"train": train_input, "validation": validation_input})

TypeError Traceback (most recent call last) in 21 ) 22 ---> 23 xgb.fit({"train": train_input, "validation": validation_input})

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/sagemaker/estimator.py in fit(self, inputs, wait, logs, job_name, experiment_config) 685 * TrialComponentDisplayName is used for display in Studio. 686 """ --> 687 self._prepare_for_training(job_name=job_name) 688 689 self.latest_training_job = _TrainingJob.start_new(self, inputs, experiment_config)

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/sagemaker/estimator.py in _prepare_for_training(self, job_name) 446 constructor if applicable. 447 """ --> 448 self._current_job_name = self._get_or_create_name(job_name) 449 450 # if output_path was specified we use it otherwise initialize here.

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/sagemaker/estimator.py in _get_or_create_name(self, name) 435 return name 436 --> 437 self._ensure_base_job_name() 438 return name_from_base(self.base_job_name) 439

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/sagemaker/estimator.py in _ensure_base_job_name(self) 420 # honor supplied base_job_name or generate it 421 if self.base_job_name is None: --> 422 self.base_job_name = base_name_from_image(self.training_image_uri()) 423 424 def _get_or_create_name(self, name=None):

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/sagemaker/utils.py in base_name_from_image(image) 95 str: Algorithm name, as extracted from the image name. 96 """ ---> 97 m = re.match("^(.+/)?([^:/]+)(:[^:]+)?$", image) 98 algo_name = m.group(2) if m else image 99 return algo_name

~/anaconda3/envs/mxnet_p36/lib/python3.6/re.py in match(pattern, string, flags) 170 """Try to apply the pattern at the start of the string, returning 171 a match object, or None if no match was found.""" --> 172 return _compile(pattern, flags).match(string) 173 174 def fullmatch(pattern, string, flags=0):

TypeError: expected string or bytes-like object


Solution

  • import sagemaker
    from sagemaker.inputs import TrainingInput
    from sagemaker.serializers import CSVSerializer
    from sagemaker.session import TrainingInput
    from sagemaker import image_uris
    from sagemaker.session import Session
    
    # initialize hyperparameters
    hyperparameters = {
        "max_depth":"5",
        "eta":"0.1",
        "gamma":"4",
        "min_child_weight":"6",
        "subsample":"0.7",
        "objective":"binary:logistic",
        "num_round":"25"}
    
    # set an output path where the trained model will be saved
    bucket = sagemaker.Session().default_bucket()
    output_path = 's3://{}/{}/output'.format(bucket, 'rain-xgb-built-in-algo')
    
    
    # this line automatically looks for the XGBoost image URI and builds an 
    XGBoost container.
    # specify the repo_version depending on your preference.
    xgboost_container = sagemaker.image_uris.retrieve("xgboost", 'ap-southeast- 
    2', "1.3-1")
    
    
    # construct a SageMaker estimator that calls the xgboost-container
    estimator = sagemaker.estimator.Estimator(image_uri=xgboost_container, 
                                          hyperparameters=hyperparameters,
                                          role=sagemaker.get_execution_role(),
                                          instance_count=1, 
                                          instance_type='ml.m5.large', 
                                          volume_size=5, # 5 GB 
                                          output_path=output_path)
    
    
    
     # define the data type and paths to the training and validation datasets
    
     train_input = TrainingInput("s3://{}/{}/".format(bucket,'train'), 
     content_type='csv')
     validation_input = TrainingInput("s3://{}/{}".format(bucket,'validation'), 
     content_type='csv')
    
    
     # execute the XGBoost training job
     estimator.fit({'train': train_input, 'validation': validation_input})
    

    I have rewritten as above and could run training. thank you !