python Lightgbm分类器与gpu

6ie5vjzr  于 2023-11-16  发布在  Python
关注(0)|答案(3)|浏览(135)
model = lgbm.LGBMClassifier(
    n_estimators=1250,
    num_leaves=128,
    learning_rate=0.009,
    verbose=1
)

字符串
使用LGBM分类器,现在有没有办法在GPU上使用它?

e1xvtsh3

e1xvtsh31#

首先,你需要为GPU构建LightGBM,比如:

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

字符串
之后,你可以在参数中使用device="gpu"来在GPU上训练你的模型,比如:

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


lgbm.LGBMClassifier(device='gpu')


并为较大的数据集加速:

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)
insrf1ej

insrf1ej2#

LightGBM on the GPU博客文章提供了LightGBM与GPU支持安装的全面说明。它描述了安装过程中可能发生的几个错误以及使用Anaconda时应采取的步骤。

6g8kf2rb

6g8kf2rb3#

不推荐使用setup.py安装。Sergey回答的最后一行应替换为:

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

字符串
目前仅在Linux上,如果你的gpu是CUDA兼容的(CUDA已经在你的PATH中),你可以把最后一行替换为

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


并在params{'device':'cuda'}中指定

相关问题