0

我正在尝试使用 GPU 加速我的 XGBClassifier。

我已经尝试过这段代码,但 GPU 和 CPU 需要相同的时间:

import xgboost as xgb
from sklearn.datasets import fetch_covtype
from sklearn.model_selection import train_test_split
import time

# Fetch dataset using sklearn
cov = fetch_covtype()
X = cov.data
y = cov.target

# Create 0.75/0.25 train/test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, train_size=0.75,
                                                    random_state=42)

# Specify sufficient boosting iterations to reach a minimum
num_round = 100

# Leave most parameters as default
param = {'objective': 'multi:softmax', # Specify multiclass classification
         'num_class': 8, # Number of possible output classes
         'tree_method': 'gpu_hist' # Use GPU accelerated algorithm
         }

# Convert input data from numpy to XGBoost format
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)

gpu_res = {} # Store accuracy result
tmp = time.time()
# Train model
xgb.train(param, dtrain, num_round, evals=[(dtest, 'test')], evals_result=gpu_res)
print("GPU Training Time: %s seconds" % (str(time.time() - tmp)))

# Repeat for CPU algorithm
tmp = time.time()
param['tree_method'] = 'hist'
cpu_res = {}
xgb.train(param, dtrain, num_round, evals=[(dtest, 'test')], evals_result=cpu_res)
print("CPU Training Time: %s seconds" % (str(time.time() - tmp)))

    [0] test-mlogloss:1.47278
    [1] test-mlogloss:1.20606
    [2] test-mlogloss:1.03133
    [3] test-mlogloss:0.91109
    [4] test-mlogloss:0.82384
    [5] test-mlogloss:0.75852
    [6] test-mlogloss:0.70788
    [7] test-mlogloss:0.66899
    [8] test-mlogloss:0.63753
    [9] test-mlogloss:0.61094
    [10]    test-mlogloss:0.59005
    [11]    test-mlogloss:0.57204
    [12]    test-mlogloss:0.55773
.....
    [97]    test-mlogloss:0.32834
    [98]    test-mlogloss:0.32719
    [99]    test-mlogloss:0.32589
    GPU Training Time: 17.633554697036743 seconds

    [0] test-mlogloss:1.47291
    [1] test-mlogloss:1.20547
    [2] test-mlogloss:1.03109
    [3] test-mlogloss:0.91091
    [4] test-mlogloss:0.82473
    .....

    [98]    test-mlogloss:0.32501
    [99]    test-mlogloss:0.32282
    CPU Training Time: 17.619328022003174 seconds

使用 nvidia-smi

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 470.57.02    Driver Version: 470.57.02    CUDA Version: 11.4     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  NVIDIA GeForce ...  On   | 00000000:01:00.0 Off |                  N/A |
| N/A   48C    P8     5W /  N/A |   2666MiB /  5934MiB |      6%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A      1051      G   /usr/lib/xorg/Xorg                227MiB |
|    0   N/A  N/A      1364      G   /usr/bin/gnome-shell               49MiB |
|    0   N/A  N/A      3003      G   /usr/lib/firefox/firefox          191MiB |
|    0   N/A  N/A     78204      G   ...AAAAAAAAA= --shared-files       32MiB |
|    0   N/A  N/A    100461      G   /usr/lib/firefox/firefox            1MiB |
|    0   N/A  N/A    102349      G   .../debug.log --shared-files        7MiB |
|    0   N/A  N/A    102856      C   ...s/rapids-21.06/bin/python     1103MiB |
|    0   N/A  N/A    103345      G   /usr/lib/firefox/firefox            1MiB |
|    0   N/A  N/A    103615      C   ...s/rapids-21.06/bin/python     1043MiB |

到底是怎么回事?我忘了安装一些东西吗?Actuakky,CPU 比 GPU 快...

我使用了本教程,一切正常(过程中没有错误)

https://www.youtube.com/watch?v=hfef_ZF7kEo&t=572s

关于为什么不起作用的任何想法?

4

0 回答 0