Sagemaker Lightgbm Distributed Training Dask
Amazon SageMaker LightGBM Distributed training using Dask
This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.
Losing customers is costly for any business. Identifying unhappy customers early on gives you a chance to offer them incentives to stay. This notebook describes using machine learning (ML) for the automated identification of unhappy customers, also known as customer churn prediction. ML models rarely give perfect predictions though, so this notebook is also about how to incorporate the relative costs of prediction mistakes when determining the financial outcome of using ML.
This notebook demonstrates the use of distributed training for Amazon SageMaker’s implementation of the LightGBM with Dask.
In this notebook, we demonstrate two use cases:
- How to distributedly train a tabular model using Dask on the customer churn dataset.
- How to use the trained tabular model to perform inference, i.e., classifying new samples.
Note: This notebook was tested in Amazon SageMaker Studio on ml.t3.medium instance with Python 3 (Data Science) kernel.
1. Set Up
Before executing the notebook, there are some initial steps required for setup. This notebook requires latest version of sagemaker and ipywidgets.
To train and host on Amazon SageMaker, we need to setup and authenticate the use of AWS services. Here, we use the execution role associated with the current notebook instance as the AWS account role with SageMaker access. It has necessary permissions, including access to your data in S3.
2. Data Preparation and Visualization
Mobile operators have historical records on which customers ultimately ended up churning and which continued using the service. We can use this historical information to construct an ML model of one mobile operator’s churn using a process called training. After training the model, we can pass the profile information of an arbitrary customer (the same profile information that we used to train the model) to the model, and have the model predict whether this customer is going to churn. Of course, we expect the model to make mistakes. After all, predicting the future is tricky business! But we’ll learn how to deal with prediction errors.
The dataset we use is publicly available and was mentioned in the book Discovering Knowledge in Data by Daniel T. Larose. It is attributed by the author to the University of California Irvine Repository of Machine Learning Datasets. Let’s download and read that dataset in now:
By modern standards, it’s a relatively small dataset, with only 5,000 records, where each record uses 21 attributes to describe the profile of a customer of an unknown US mobile operator. The attributes are:
State: the US state in which the customer resides, indicated by a two-letter abbreviation; for example, OH or NJ
Account Length: the number of days that this account has been active
Area Code: the three-digit area code of the corresponding customer’s phone number
Phone: the remaining seven-digit phone number
Int’l Plan: whether the customer has an international calling plan: yes/no
VMail Plan: whether the customer has a voice mail feature: yes/no
VMail Message: the average number of voice mail messages per month
Day Mins: the total number of calling minutes used during the day
Day Calls: the total number of calls placed during the day
Day Charge: the billed cost of daytime calls
Eve Mins, Eve Calls, Eve Charge: the billed cost for calls placed during the evening
Night Mins, Night Calls, Night Charge: the billed cost for calls placed during nighttime
Intl Mins, Intl Calls, Intl Charge: the billed cost for international calls
CustServ Calls: the number of calls placed to Customer Service
Churn?: whether the customer left the service: true/false
The last attribute, Churn?, is known as the target attribute: the attribute that we want the ML model to predict. Because the target attribute is binary, our model will be performing binary prediction, also known as binary classification.
Let’s begin exploring the data:
We can see immediately that: - State appears to be quite evenly distributed. - Phone takes on too many unique values to be of any practical use. It’s possible that parsing out the prefix could have some value, but without more context on how these are allocated, we should avoid using it. - Most of the numeric features are surprisingly nicely distributed, with many showing bell-like gaussianity. VMail Message is a notable exception.
Next let’s look at the relationship between each of the features and our target variable.
We convert the target attribute to binary value and move it to the first column of the dataset to meet requirements of SageMaker built-in tabular algorithms (For an example, see SageMaker LightGBM documentation).
We identify the column indexes of the categorical attribute, which is required by LightGBM, CatBoost, and TabTransformer algorithm (AutoGluon-Tabular has built-in feature engineering to identify the categorical attribute automatically, and thus does not require such input).
LightGBM official documentation requires that all categorical features should be encoded as non-negative integers.
We split the churn dataset into train, validation, and test set using stratified sampling. Validation set is used for early stopping and AMT. Test set is used for performance evaluations in the end. Next, we upload them into a S3 path for training.
The structure of the S3 path for training should be structured as below.
- The supported input data format for training is
csv. You are allowed to put more than 1 data file under both train and valdiation channel. The name of data file can be any one as long as it ends with.csv. - The first column corresponds to the target and the rest of columns correspond to features. This follows the convention of SageMaker XGBoost algorithm.
- The
cat_idx.jsonis categorical column indexes. It contains a dictionary of a key-value pair. The key can be any string. The value is the list of column indexes of categorical features. The index starts with value 1 as value 0 corresponds to the target variable. Please see example above to format thecat_idx.json. - For the validation data, we encourage you to include one data file under its channel such that the all of the validation data points can be assigned to one machine. Thus, the validation score is for all of the validation data points and can be easily parsed by the AMT for hyperparameter optimization.
- Current distributed training only supports CPU.
-- train
-- data_1.csv
-- data_2.csv
-- data_3.csv
-- cat_idx.json
-- validation
-- data.csv
For demonstartion purpose on including multiple files under the training channel, we simply duplicate the training data multiple times as shown below.
3. Distributedly Train A SageMaker LightGBM Model with AMT
3.1. Retrieve Training Artifacts
Here, we retrieve the training docker container, the training algorithm source, and the tabular algorithm. Note that model_version="*" fetches the latest model.
For the training algorithm, we have four choices in this demonstration for classification task.
- LightGBM: To use this algorithm, specify
train_model_idaslightgbm-classification-modelin the cell below.
For regression task, the train_model_id is lightgbm-regression-model.
3.2. Set Training Parameters
Now that we are done with all the setup that is needed, we are ready to train our tabular algorithm. To begin, let us create a sageMaker.estimator.Estimator object. This estimator will launch the training job.
There are two kinds of parameters that need to be set for training. The first one are the parameters for the training job. These include: (i) Training data path. This is S3 folder in which the input data is stored, (ii) Output path: This the s3 folder in which the training output is stored. (iii) Training instance type: This indicates the type of machine on which to run the training.
The second set of parameters are algorithm specific training hyper-parameters.
For algorithm specific hyper-parameters, we start by fetching python dictionary of the training hyper-parameters that the algorithm accepts with their default values. This can then be overridden to custom values. For the evaluation metric that is used by early stopping and automatic model tuning, we choose auc score. Note. LightGBM does not have built-in F1 score supported. See LightGBM documentation.
3.3. Train with Automatic Model Tuning
Amazon SageMaker automatic model tuning, also known as hyperparameter tuning, finds the best version of a model by running many training jobs on your dataset using the algorithm and ranges of hyperparameters that you specify. It then chooses the hyperparameter values that result in a model that performs the best, as measured by a metric that you choose. We will use a HyperparameterTuner object to interact with Amazon SageMaker hyperparameter tuning APIs.
- Note. In this notebook, we set AMT budget (total tuning jobs) as 10 for each of the tabular algorithm except AutoGluon-Tabular. For AutoGluon-Tabular, it succeeds by ensembling multiple models and stacking them in multiple layers.
3.4. Start Training
We start by creating the estimator object with all the required assets and then launch the training job.
- To enable distributed training, you only need specify the number of instances to be more than 1.
- You might need increase the argument volumn_size if your dataset size is larger than the default value (30GB). Otherwise, you may see insufficient disk memory error.
3.5. Deploy and Run Inference on the Trained Tabular Model
In this section, you learn how to query an existing endpoint and make predictions of the examples you input. For each example, the model will output the probability of the sample for each class in the model. Next, the predicted class label is obtained by taking the class label with the maximum probability over others.
We start by retrieving the artifacts and deploy the tabular_estimator that we trained.
Next, we read the customer churn test data into pandas data frame, prepare the ground truth target and predicting features to send into the endpoint.
Below is the screenshot of the first 5 examples in the test set.
The following code queries the endpoint you have created to get the prediction for each test example.
The query_endpoint() function returns an array-like of shape (num_examples, num_classes), where each row indicates
the probability of the example for each class in the model. The num_classes is 2 in above test data.
Next, the predicted class label is obtained by taking the class label with the maximum probability over others for each example.
3.6. Evaluate the Prediction Results Returned from the Endpoint
We evaluate the predictions results returned from the endpoint by following two ways.
-
Visualize the predictions results by plotting the confusion matrix.
-
Measure the prediction results quantitatively.
Notebook CI Test Results
This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.