user11173832
user11173832

Reputation:

Lightgbm classifier with gpu

model = lgbm.LGBMClassifier(
    n_estimators=1250,
    num_leaves=128,
    learning_rate=0.009,
    verbose=1
)

Using the LGBM classifier, is there a way to use this with GPU these days?

Upvotes: 11

Views: 26600

Answers (3)

Sergey Bushmanov
Sergey Bushmanov

Reputation: 25249

2024 edition, as simple as this:

git clone --recursive https://github.com/Microsoft/LightGBM
cd LightGBM
sh ./build-python.sh install --cuda

Note the --cuda flag.

It's advised on github:

Do you have NVIDIA GPUs? If so, could you please try the "device": "cuda" version instead? It's better maintained, does more work on the GPU, and should be much faster.

The below timing still applies; however, I should mention on smallish datasets cpu outperforms cuda.

Old answer

First, you need to build LightGBM for GPU, like:

git clone --recursive https://github.com/Microsoft/LightGBM 
cd LightGBM && mkdir build && cd build
cmake -DUSE_GPU=1 ..
make -j4
pip uninstall lightgbm
cd ../python-package/ && python setup.py install

After that you may use device="gpu" in params to train your model on GPU, like:

lgbm.train(params={'device'='gpu'}, ...)

or

lgbm.LGBMClassifier(device='gpu')

And speed up for a largish dataset:

from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
import lightgbm as lgbm
X,y = make_classification(n_samples=10000000, n_features=100, n_classes=2)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25)
%%timeit
model = lgbm.LGBMClassifier(device="gpu")
model.fit(X_train, y_train)
19.9 s ± 163 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%%timeit
model = lgbm.LGBMClassifier(device="cpu")
model.fit(X_train, y_train)
1min 23s ± 46.4 s per loop (mean ± std. dev. of 7 runs, 1 loop each)

Upvotes: 16

Rakin Baten
Rakin Baten

Reputation: 21

Installation with setup.py is being deprecated. The last line of Sergey's answer should be replaced with:

cd ../python-package
sh ./build-python.sh install --gpu

Currently only on linux and if your gpu is CUDA compatible (with CUDA already in your PATH) you can replace the last line with

sh ./build-python.sh install --cuda

and specify in the params {'device':'cuda'}

Upvotes: 2

Sergey Khutornoy
Sergey Khutornoy

Reputation: 601

LightGBM on the GPU blog post provides comprehensive instructions on LightGBM with GPU support installation. It describes several errors that may occur during installation and steps to take when Anaconda is used.

Upvotes: 4

Related Questions