10
model = lgbm.LGBMClassifier(n_estimators=1250, num_leaves=128,learning_rate=0.009,verbose=1)`enter code here`

using the LGBM classifier
is there way to use this with gpu this days?

Sergey Bushmanov
  • 23,310
  • 7
  • 53
  • 72

3 Answers3

15

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)
Sergey Bushmanov
  • 23,310
  • 7
  • 53
  • 72
  • 1
    after executing everything in the first code snippet, I can't do `import lightgbm as lgbm`. i am being shown `ModuleNotFoundError: No module named 'lightgbm'`. how do i fix this? – Naveen Reddy Marthala Sep 12 '20 at 16:41
  • @NaveenKumar install it – Sergey Bushmanov Sep 12 '20 at 18:26
  • You had this in first snippet `pip uninstall lightgbm`. So i tried it just like the snippets described. – Naveen Reddy Marthala Sep 12 '20 at 18:28
  • The first snippet (1) builds lightgbm with gpu support (2) just in case uninstalls existing lightgbm (3) installs compiled package. Your error says no lightgbm in the environment you try to execute `import lightgbm`. You should install it prior to import. – Sergey Bushmanov Sep 12 '20 at 18:31
  • Ok, where do i install ligjtgbm from, i certainly should not do pip install, right? – Naveen Reddy Marthala Sep 12 '20 at 18:33
  • 1
    The line `cd ../python-package/ && python setup.py install` installs the package. But you need to execute all the lines.... – Sergey Bushmanov Sep 12 '20 at 18:34
  • Thanks for your time. As you said, i may have missed that line. I will run all the lines. – Naveen Reddy Marthala Sep 12 '20 at 18:37
  • I was testing this on Google Colab. Here's all that I have done: lines on command line ```!git clone --recursive https://github.com/Microsoft/LightGBM %cd /content/LightGBM !mkdir build %cd build/ !cmake -DUSE_GPU=1 .. !make -j4 !pip uninstall lightgbm %cd ../python-package/ !python setup.py install``` lines in python: ```from lightgbm import LGBMClassifier LGBMClassifier(device='gpu', gpu_platform_id=0, gpu_device_id=0,....)``` shapes of my train set = (247570, 70) and validation set=(13031, 70). It still took me 20 minutes as when trained on CPU. What may have gone wrong? – Naveen Reddy Marthala Sep 13 '20 at 06:14
  • I could also see that GPU usage is as low as 0.54GB even during training. – Naveen Reddy Marthala Sep 13 '20 at 06:36
  • i need to replace `cmake -DUSE_GPU=1 ..` with `cmake -DUSE_GPU=1 -DOpenCL_LIBRARY=/usr/local/cuda/lib64/libOpenCL.so.1 -DOpenCL_INCLUDE_DIR=/usr/local/cuda/include/ ..` – Talha Anwar Aug 21 '22 at 07:30
  • How do you choose a different gpu? In my laptop it takes the intel gpu instead of the nVidia. – skan Aug 04 '23 at 20:02
  • In my Windows laptop it uses the integrated "slow" Intel GPU instead of the nVidia GPU. How can I force it it use the good one? device="cuda" doesn't work, nor gpu_device_id=0 or 1. I have the nvidia drivers and cudann installed. – skan Aug 04 '23 at 22:52
  • make -j4 says No targets specified and no makefile found. Stop. – skan Aug 04 '23 at 23:21
4

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.

1

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'}