GPU-accelerated model training with cuML and XGBoost: Worked Example — Machine Learning With RAPIDS (NVIDIA-Certified Associate: Accelerated Data Science)
GPU-Accelerated Model Training with cuML and XGBoost: Worked Example This worked example demonstrates how to leverage RAPIDS cuML and XGBoost...
GPU-Accelerated Model Training with cuML and XGBoost: Worked Example
This worked example demonstrates how to leverage RAPIDS cuML and XGBoost libraries for GPU-accelerated machine learning model training, focusing on a realistic classification problem. We will walk through data preparation, model training, evaluation, and interpretation using GPU acceleration to significantly speed up the workflow.
Scenario
Suppose we have a dataset containing customer information and want to predict whether a customer will subscribe to a service (binary classification). The dataset includes numerical and categorical features.
Step 1: Data Preparation on GPU
First, load the dataset into a GPU DataFrame using cudf, RAPIDS’ GPU-accelerated DataFrame library.
- Import cudf and load CSV data directly to GPU memory.
- Handle missing values and encode categorical variables using GPU-accelerated methods.
Example:
Code Snippet
import cudfdf = cudf.read_csv('customer_data.csv')df['category_encoded'] = df['category'].astype('category').cat.codes
Step 2: Splitting Data
Use cuml.model_selection.train_test_split to split data into training and testing sets directly on the GPU.
Code Snippet
from cuml.model_selection import train_test_splitX = df.drop(['target'], axis=1)y = df['target']X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Step 3: Training a Classification Model with cuML
Train a GPU-accelerated logistic regression or random forest classifier using cuML.
- Initialize the model (e.g., cuml.LogisticRegression()).
- Fit the model on the training data.
Code Snippet
from cuml.linear_model import LogisticRegressionmodel = LogisticRegression()model.fit(X_train, y_train)
Step 4: Training with GPU-Accelerated XGBoost
For gradient boosting, use the GPU-enabled xgboost API by specifying the tree_method='gpu_hist' parameter.
- Convert data to xgboost.DMatrix format.
- Set parameters for binary classification and GPU usage.
- Train the model with xgb.train().
Code Snippet
import xgboost as xgbdtrain = xgb.DMatrix(X_train.to_pandas(), label=y_train.to_pandas())dtest = xgb.DMatrix(X_test.to_pandas(), label=y_test.to_pandas())params = {'objective': 'binary:logistic', 'tree_method': 'gpu_hist', 'eval_metric': 'auc'}bst = xgb.train(params, dtrain, num_boost_round=100)
Step 5: Model Evaluation
Evaluate the trained models using GPU-accelerated predictions and compute performance metrics such as accuracy, AUC, and confusion matrix.
- Predict on test data.
- Calculate metrics with RAPIDS or transfer results to CPU if needed.
Code Snippet
y_pred = model.predict(X_test)from cuml.metrics import accuracy_scoreaccuracy = accuracy_score(y_test, y_pred)
For XGBoost:
y_pred_prob = bst.predict(dtest)y_pred_label = (y_pred_prob > 0.5).astype(int)
Step 6: Interpretation of Results
Interpret the confusion matrix and other metrics to assess model generalization:
- True Positives (TP): Correct positive predictions
- False Positives (FP): Incorrect positive predictions
- True Negatives (TN): Correct negative predictions
- False Negatives (FN): Incorrect negative predictions
Use these to calculate precision, recall, and F1-score, which provide deeper insight into model performance beyond accuracy.
Summary
This example highlights how RAPIDS cuML and GPU-accelerated XGBoost enable efficient training and evaluation of machine learning models on GPUs. By keeping data and computation on the GPU, data scientists can achieve significant speedups compared to CPU-based workflows, which is critical for large datasets and iterative model tuning.
For more detailed documentation and examples, visit the official RAPIDS AI website: https://rapids.ai/ and XGBoost GPU documentation: https://xgboost.readthedocs.io/en/stable/gpu/index.html.
More in this topic
Ready to test your knowledge?
Put what you've learned into practice with a quick quiz and track your progress.
Test your knowledge →