<jupyter_start><jupyter_text>Train and deploy a regression model with the Amazon SageMaker XGBoost Algorithm using Script Mode_**Distributed training for regression with Amazon SageMaker XGBoost script mode**_--- RuntimeThis notebook takes approximately 5 minutes to run. Contents1. [Introduction](Introduction)1. [Setup](Setup)  1. [Fetch the dataset](Fetch-the-dataset)  1. [Ingest data](Ingest-data)1. [Create an XGBoost training script](Create-an-XGBoost-training-script)1. [Train the XGBoost model](Train-the-XGBoost-model)1. [Deploy the XGBoost model](Deploy-the-XGBoost-model)1. [Cleanup](Cleanup)---  IntroductionThis notebook demonstrates the use of Amazon SageMaker XGBoost to train and host a regression model. [XGBoost (eXtreme Gradient Boosting)](https://xgboost.readthedocs.io) is a popular and efficient machine learning (ML) algorithm used for regression and classification tasks on tabular datasets. It implements a technique know as gradient boosting on trees, and performs remarkably well in ML competitions, and gets a lot of attention from customers. We use the [Abalone dataset](https://www.csie.ntu.edu.tw/~cjlin/libsvmtools/datasets/regression.html), originally from the [UCI ML repository](https://archive.ics.uci.edu/ml/datasets/abalone). In this libsvm converted version, the nominal feature (Male/Female/Infant) has been converted into a real valued feature as required by XGBoost. The age of the abalone snail is predicted from eight physical measurements.  --- SetupThis notebook was tested in Amazon SageMaker Studio on a ml.t3.medium instance on the Python 3 (Data Science) kernel.Let's start by specifying:1. The S3 bucket and prefix to use for the training and model data. This should be within the same region as the Notebook Instance, training, and hosting.1. The IAM role ARN used to grant access to your data.<jupyter_code>!pip3 install -U sagemaker
import os
import boto3
import re
import sagemaker

role = sagemaker.get_execution_role()
region = sagemaker.Session().boto_region_name

bucket = sagemaker.Session().default_bucket()
prefix = "sagemaker/DEMO-xgboost-dist-script"<jupyter_output><empty_output><jupyter_text>Fetch the datasetThe following methods split the data into train/test/validation datasets and upload files to S3.<jupyter_code>import io
import boto3
import random

random.seed(42)


def data_split(
    FILE_DATA,
    DATA_DIR,
    FILE_TRAIN_BASE,
    FILE_TRAIN_1,
    FILE_VALIDATION,
    FILE_TEST,
    PERCENT_TRAIN_0,
    PERCENT_TRAIN_1,
    PERCENT_VALIDATION,
    PERCENT_TEST,
):
    data = [l for l in open(FILE_DATA, "r")]
    train_file_0 = open(DATA_DIR + "/" + FILE_TRAIN_0, "w")
    train_file_1 = open(DATA_DIR + "/" + FILE_TRAIN_1, "w")
    valid_file = open(DATA_DIR + "/" + FILE_VALIDATION, "w")
    tests_file = open(DATA_DIR + "/" + FILE_TEST, "w")

    num_of_data = len(data)
    num_train_0 = int((PERCENT_TRAIN_0 / 100.0) * num_of_data)
    num_train_1 = int((PERCENT_TRAIN_1 / 100.0) * num_of_data)
    num_valid = int((PERCENT_VALIDATION / 100.0) * num_of_data)
    num_tests = int((PERCENT_TEST / 100.0) * num_of_data)

    data_fractions = [num_train_0, num_train_1, num_valid, num_tests]
    split_data = [[], [], [], []]

    rand_data_ind = 0

    for split_ind, fraction in enumerate(data_fractions):
        for i in range(fraction):
            rand_data_ind = random.randint(0, len(data) - 1)
            split_data[split_ind].append(data[rand_data_ind])
            data.pop(rand_data_ind)

    for l in split_data[0]:
        train_file_0.write(l)

    for l in split_data[1]:
        train_file_1.write(l)

    for l in split_data[2]:
        valid_file.write(l)

    for l in split_data[3]:
        tests_file.write(l)

    train_file_0.close()
    train_file_1.close()
    valid_file.close()
    tests_file.close()


def write_to_s3(fobj, bucket, key):
    return (
        boto3.Session(region_name=region)
        .resource("s3")
        .Bucket(bucket)
        .Object(key)
        .upload_fileobj(fobj)
    )


def upload_to_s3(bucket, channel, filename):
    fobj = open(filename, "rb")
    key = prefix + "/" + channel
    url = "s3://{}/{}/{}".format(bucket, key, filename)
    print("Writing to {}".format(url))
    write_to_s3(fobj, bucket, key)<jupyter_output><empty_output><jupyter_text>Ingest dataNext, we read the dataset from the existing repository into memory, for preprocessing prior to training. This processing could be done *in situ* by SageMaker Processing, Amazon Athena, Apache Spark in Amazon EMR, Amazon Redshift, etc., assuming the dataset is present in the appropriate location. Then, the next step would be to transfer the data to S3 for use in training. For small datasets, such as this one, reading into memory isn't onerous, though it would be for larger datasets.<jupyter_code>s3 = boto3.client("s3")

# Load the dataset
FILE_DATA = "abalone"
s3.download_file(
    "sagemaker-sample-files", f"datasets/tabular/uci_abalone/abalone.libsvm", FILE_DATA
)

# Split the downloaded data into train/test/validation files
FILE_TRAIN_0 = "abalone.train_0"
FILE_TRAIN_1 = "abalone.train_1"
FILE_VALIDATION = "abalone.validation"
FILE_TEST = "abalone.test"
PERCENT_TRAIN_0 = 35
PERCENT_TRAIN_1 = 35
PERCENT_VALIDATION = 15
PERCENT_TEST = 15

DATA_DIR = "data"

if not os.path.exists(DATA_DIR):
    os.mkdir(DATA_DIR)

data_split(
    FILE_DATA,
    DATA_DIR,
    FILE_TRAIN_0,
    FILE_TRAIN_1,
    FILE_VALIDATION,
    FILE_TEST,
    PERCENT_TRAIN_0,
    PERCENT_TRAIN_1,
    PERCENT_VALIDATION,
    PERCENT_TEST,
)
# Upload the files to the S3 bucket
upload_to_s3(bucket, "train/train_0.libsvm", DATA_DIR + "/" + FILE_TRAIN_0)
upload_to_s3(bucket, "train/train_1.libsvm", DATA_DIR + "/" + FILE_TRAIN_1)
upload_to_s3(bucket, "validation/validation.libsvm", DATA_DIR + "/" + FILE_VALIDATION)
upload_to_s3(bucket, "test/test.libsvm", DATA_DIR + "/" + FILE_TEST)<jupyter_output><empty_output><jupyter_text>Create an XGBoost training scriptSageMaker can now run an XGBoost script using the XGBoost estimator. When run on SageMaker, a number of helpful environment variables are available to access properties of the training environment, such as:- `SM_MODEL_DIR`: A string representing the path to the directory to write model artifacts to. Any artifacts saved in this folder are uploaded to S3 for model hosting after the training job completes.- `SM_OUTPUT_DIR`: A string representing the filesystem path to write output artifacts to. Output artifacts may include checkpoints, graphs, and other files to save, not including model artifacts. These artifacts are compressed and uploaded to S3 to the same S3 prefix as the model artifacts.When two input channels, `train` and `validation`, are used in the call to the XGBoost estimator's `fit()` method, the following environment variables are set, following the format `SM_CHANNEL_[channel_name]`:- `SM_CHANNEL_TRAIN`: A string representing the path to the directory containing data in the 'train' channel.- `SM_CHANNEL_VALIDATION`: Same as above, but for the 'validation' channel.A typical training script loads data from the input channels, configures training with hyperparameters, trains a model, and saves a model to the `model_dir` so that it can be hosted later. Hyperparameters are passed to your script as arguments and can be retrieved with an `argparse.ArgumentParser` instance. For example, the script that we run in this notebook is provided as the accompanying file (`abalone.py`) and also shown below:```pythonimport argparseimport jsonimport loggingimport osimport pandas as pdimport pickle as pklfrom sagemaker_containers import entry_pointfrom sagemaker_xgboost_container.data_utils import get_dmatrixfrom sagemaker_xgboost_container import distributedimport xgboost as xgbdef _xgb_train(params, dtrain, evals, num_boost_round, model_dir, is_master):    """Run xgb train on arguments given with rabit initialized.    This is our rabit execution function.    :param args_dict: Argument dictionary used to run xgb.train().    :param is_master: True if current node is master host in distributed training,                        or is running single node training job.                        Note that rabit_run includes this argument.    """    booster = xgb.train(params=params,                        dtrain=dtrain,                        evals=evals,                        num_boost_round=num_boost_round)    if is_master:        model_location = model_dir + '/xgboost-model'        pkl.dump(booster, open(model_location, 'wb'))        logging.info("Stored trained model at {}".format(model_location))if __name__ == '__main__':    parser = argparse.ArgumentParser()     Hyperparameters are described here.    parser.add_argument('--max_depth', type=int,)    parser.add_argument('--eta', type=float)    parser.add_argument('--gamma', type=int)    parser.add_argument('--min_child_weight', type=int)    parser.add_argument('--subsample', type=float)    parser.add_argument('--verbosity', type=int)    parser.add_argument('--objective', type=str)    parser.add_argument('--num_round', type=int)    parser.add_argument('--tree_method', type=str, default="auto")    parser.add_argument('--predictor', type=str, default="auto")     Sagemaker specific arguments. Defaults are set in the environment variables.    parser.add_argument('--output_data_dir', type=str, default=os.environ.get('SM_OUTPUT_DATA_DIR'))    parser.add_argument('--model_dir', type=str, default=os.environ.get('SM_MODEL_DIR'))    parser.add_argument('--train', type=str, default=os.environ.get('SM_CHANNEL_TRAIN'))    parser.add_argument('--validation', type=str, default=os.environ.get('SM_CHANNEL_VALIDATION'))    parser.add_argument('--sm_hosts', type=str, default=os.environ.get('SM_HOSTS'))    parser.add_argument('--sm_current_host', type=str, default=os.environ.get('SM_CURRENT_HOST'))    args, _ = parser.parse_known_args()     Get SageMaker host information from runtime environment variables    sm_hosts = json.loads(args.sm_hosts)    sm_current_host = args.sm_current_host    dtrain = get_dmatrix(args.train, 'libsvm')    dval = get_dmatrix(args.validation, 'libsvm')    watchlist = [(dtrain, 'train'), (dval, 'validation')] if dval is not None else [(dtrain, 'train')]    train_hp = {        'max_depth': args.max_depth,        'eta': args.eta,        'gamma': args.gamma,        'min_child_weight': args.min_child_weight,        'subsample': args.subsample,        'verbosity': args.verbosity,        'objective': args.objective,        'tree_method': args.tree_method,        'predictor': args.predictor,    }    xgb_train_args = dict(        params=train_hp,        dtrain=dtrain,        evals=watchlist,        num_boost_round=args.num_round,        model_dir=args.model_dir)    if len(sm_hosts) > 1:         Wait until all hosts are able to find each other        entry_point._wait_hostname_resolution()         Execute training function after initializing rabit.        distributed.rabit_run(            exec_fun=_xgb_train,            args=xgb_train_args,            include_in_training=(dtrain is not None),            hosts=sm_hosts,            current_host=sm_current_host,            update_rabit_args=True        )    else:         If single node training, call training method directly.        if dtrain:            xgb_train_args['is_master'] = True            _xgb_train(**xgb_train_args)        else:            raise ValueError("Training channel must have data to train model.")def model_fn(model_dir):    """Deserialize and return fitted model.    Note that this should have the same name as the serialized model in the _xgb_train method    """    model_file = 'xgboost-model'    booster = pkl.load(open(os.path.join(model_dir, model_file), 'rb'))    return booster``` Because the container imports your training script, always put your training code in a main guard `(if __name__=='__main__':)` so that the container does not inadvertently run your training code at the wrong point in execution.For more information about training environment variables, please visit the [SageMaker Training Toolkit](https://github.com/aws/sagemaker-training-toolkit).  Train the XGBoost modelAfter setting training parameters, we kick off training, and poll for status until training is complete.To run our training script on SageMaker, we construct a sagemaker.xgboost.estimator.XGBoost estimator, which accepts several constructor arguments:* __entry_point__: The path to the Python script that SageMaker runs for training and prediction.* __role__: Role ARN* __train_instance_type__ *(optional)*: The type of SageMaker instances for training.* __sagemaker_session__ *(optional)*: The session used to train on SageMaker.* __hyperparameters__ *(optional)*: A dictionary passed to the train function as hyperparameters.<jupyter_code>hyperparams = {
    "max_depth": "5",
    "eta": "0.2",
    "gamma": "4",
    "min_child_weight": "6",
    "subsample": "0.7",
    "objective": "reg:squarederror",
    "num_round": "50",
    "verbosity": "2",
}

instance_type = "ml.m5.2xlarge"
output_path = "s3://{}/{}/{}/output".format(bucket, prefix, "abalone-dist-xgb")
content_type = "libsvm"
# Open Source distributed script mode
from sagemaker.session import Session
from sagemaker.inputs import TrainingInput
from sagemaker.xgboost.estimator import XGBoost

session = Session()
script_path = "abalone.py"

xgb_script_mode_estimator = XGBoost(
    entry_point=script_path,
    framework_version="1.5-1",  # Note: framework_version is mandatory
    hyperparameters=hyperparams,
    role=role,
    instance_count=2,
    instance_type=instance_type,
    output_path=output_path,
)

train_input = TrainingInput(
    "s3://{}/{}/{}/".format(bucket, prefix, "train"), content_type=content_type
)
validation_input = TrainingInput(
    "s3://{}/{}/{}/".format(bucket, prefix, "validation"), content_type=content_type
)<jupyter_output><empty_output><jupyter_text>Train an XGBoost Estimator on the Abalone data Training is as simple as calling `fit()` on the Estimator. This starts a SageMaker training job that downloads the data, invokes the entry point code (in the provided script file), and saves any model artifacts that the script creates.<jupyter_code>xgb_script_mode_estimator.fit({"train": train_input, "validation": validation_input})<jupyter_output><empty_output><jupyter_text>Deploy the XGBoost modelAfter training, we use the estimator to create an Amazon SageMaker endpoint – a hosted and managed prediction service to perform inference.You can also optionally specify other functions to customize the deserialization of the input request (`input_fn()`), serialization of the predictions (`output_fn()`), and how predictions are made (`predict_fn()`). The defaults work for our current use-case so we don’t need to define them.<jupyter_code>predictor = xgb_script_mode_estimator.deploy(
    initial_instance_count=1, instance_type="ml.m5.2xlarge"
)
test_file = DATA_DIR + "/" + FILE_TEST
with open(test_file, "r") as f:
    payload = f.read()
runtime_client = session.sagemaker_runtime_client
response = runtime_client.invoke_endpoint(
    EndpointName=predictor.endpoint_name, ContentType="text/libsvm", Body=payload
)
result = response["Body"].read().decode("ascii")
print("Predicted values are {}.".format(result))<jupyter_output><empty_output><jupyter_text>CleanupWhen you're done with this exercise, please run the cell below to delete the hosted endpoint and avoid any additional charges.<jupyter_code>predictor.delete_model()
predictor.delete_endpoint()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09747942386831275, 'fraction_numerical': 0.007973251028806584, 'mean_word_length': 4.430516759776537, 'pattern_counts': {'":': 10, '<': 29, '<?xml version=': 0, '>': 30, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '341180', 'license': 'Apache-2.0', 'n_tokens_mistral': 4816, 'n_tokens_neox': 4662, 'n_words': 1429, 'path': 'introduction_to_amazon_algorithms/xgboost_abalone/xgboost_abalone_dist_script_mode.ipynb', 'repo_name': 'AmintorDusko/amazon-sagemaker-examples', 'size': '15552'}