Skip to content

Commit

Permalink
Merge pull request rasbt#29 from rasbt/xgboost-cloud-gpu
Browse files Browse the repository at this point in the history
xgboost-gpu
  • Loading branch information
rasbt authored Jan 15, 2023
2 parents e5458d2 + 955b3f4 commit 8b96638
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
24 changes: 24 additions & 0 deletions cloud-resources/xgboost-lightning-gpu/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Training an XGBoost Classifier Using Cloud GPUs Without Worrying About Infrastructure



Code accompanying the blog article: [Training an XGBoost Classifier Using Cloud GPUs Without Worrying About Infrastructure]()



Run code as follows



```pip install lightning
# run XGBoost classifier locally
python my_xgboost_classifier.py
# run XGBoost classifier locally via Lightning (if you have a GPU)
pip install lightning
lightning run app xgboost-cloud-gpu.py --setup
# run XGBoost in Lightning cloud on a V100
lightning run app xgboost-cloud-gpu.py --cloud
```

30 changes: 30 additions & 0 deletions cloud-resources/xgboost-lightning-gpu/my_xgboost_classifier.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from xgboost import XGBClassifier
from joblib import dump


def run_classifier(save_as="my_model.joblib", use_gpu=False):
digits = datasets.load_digits()
features, targets = digits.images, digits.target
features = features.reshape(-1, 8*8)

X_train, X_test, y_train, y_test = train_test_split(features, targets, test_size=0.2, random_state=123)

if use_gpu:
model = XGBClassifier(tree_method='gpu_hist', gpu_id=0)
else:
model = XGBClassifier()

model.fit(X_train, y_train)
y_pred = model.predict(X_test)

accuracy = accuracy_score(y_test, y_pred)
print(f"Accuracy: {accuracy * 100.0:.2f}%")

dump(model, filename=save_as)


if __name__ == "__main__":
run_classifier()
26 changes: 26 additions & 0 deletions cloud-resources/xgboost-lightning-gpu/xgboost-cloud-gpu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!pip install xgboost
#!pip install scikit-learn

import lightning as L
from lightning.app.storage import Drive
from my_xgboost_classifier import run_classifier


class RunCode(L.LightningWork):
def __init__(self):

# available GPUs and costs: https://lightning.ai/pricing/consumption-rates
super().__init__(cloud_compute=L.CloudCompute("gpu-fast", disk_size=10))

# storage for outputs
self.model_storage = Drive("lit://checkpoints")

def run(self):
# run model code
model_path = "my_model.joblib"
run_classifier(save_as=model_path, use_gpu=True)
self.model_storage.put(model_path)


component = RunCode()
app = L.LightningApp(component)
7 changes: 4 additions & 3 deletions math/Four-matrix-multiplications.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@
"id": "72162938",
"metadata": {},
"source": [
"### 4.1) Columns times rows with dot products (matmuls)"
"## 4.1) Columns times rows with dot products (matmuls)"
]
},
{
Expand Down Expand Up @@ -251,9 +251,10 @@
"id": "5b4ae027-58ce-45e5-81ab-7babb030197d",
"metadata": {},
"source": [
"### 4.2) Columns times rows with outer products\n",
"## 4.2) Columns times rows with outer products\n",
"\n",
"For each row in A and each column in B, compute the outer product. The matrix AB is the sum over all these outer products."
"For each row in A and each column in B, compute the outer product. \n",
"The matrix AB is the sum over all these outer products."
]
},
{
Expand Down

0 comments on commit 8b96638

Please sign in to comment.