XGBoost:奇怪的 运行-多核时间

XGBoost: Strange Run-Time with Multiple Cores

我在 2017 MacBook Pro 上使用 XGBoost 0.7 和 Python 3.6。我运行:

sysctl -n hw.ncpu

然后回来

8

所以我测试多线程工作正常,几乎一切看起来都很正常。我看到了多达 8 个内核的改进。但是,当我使用 numthreads=-1 时,我的性能很糟糕。

具体来说,我 运行 使用来自 Kaggle 的 Otto train.csv 文件:

from pandas import read_csv
from xgboost import XGBClassifier
from sklearn.preprocessing import LabelEncoder
import time
from matplotlib import pyplot
# load data
data = read_csv('train.csv')
dataset = data.values
# split data into X and y
X = dataset[:,0:94]
y = dataset[:,94]
# encode string class values as integers
label_encoded_y = LabelEncoder().fit_transform(y)
# evaluate the effect of the number of threads
results = []
num_threads = [2, 4, 8, 16, -1]
for n in num_threads:
    start = time.time()
    model = XGBClassifier(nthread=n)
    model.fit(X, label_encoded_y)
    elapsed = time.time() - start
    print(n, elapsed)
    results.append(elapsed)

我回来了:

2 48.96274280548096
4 26.73108983039856
8 24.160531997680664
16 24.71382975578308
-1 91.67938613891602

根据 xgboost 文档,numthreads=-1 应该使用您机器上的所有可用内核。那么我不应该得到最好的性能,至少和 8 核一样好吗?

谢谢!

According to the xgboost docs, numthreads=-1 should use all available cores on your machine.

不确定你从哪里得到的,但在 XGB source code, nthread <= 0 means None, which will internally use the single threaded implementation 中。这就是为什么你的速度几乎是双线程执行时间的两倍。