我想知道,哪个更好地与 GridSearchCV( ..., n_jobs = ... )
一起使用来为模型选择最佳参数集, n_jobs = -1
或 n_jobs
较大的数字,
如 n_jobs = 30
?
基于 Sklearn 文档:
n_jobs = -1
means that the computation will be dispatched on all the CPUs of the computer.
在我的 PC 上,我有一个 Intel i3 CPU,它有 2 个内核和 4 个线程,这是否意味着如果我设置 n_jobs = -1
,它会隐含地等于 n_jobs = 2
?
最佳答案
... does that mean if I set
n_jobs = -1
, implicitly it will be equal ton_jobs = 2
?
这个很简单:
python(GridSearchCV()
中的 scipy/joblib)用于检测 CPU 核心的数量,如果请求是通过 n_jobs = -1
设置。
在某些可以综合模拟 CPU/内核的虚拟机案例中,结果并不像您已知的 Intel CPU/i3 案例那样微不足道。
如果有疑问,可以用一个简单的案例测试这个(在一个非常小的数据集上,而不是成熟的模型空间搜索......)然后让故事继续下去来证明这一点。
import psutil; print( "{0:17s}{1:} CPUs PHYSICAL".format(
"psutil:",
psutil.cpu_count( logical = False ) ) )
pass; print( "{0:17s}{1:} CPUs LOGICAL".format(
"psutil:",
psutil.cpu_count( logical = True ) ) )
...
类似的主机平台“ self 检测”可能会报告不同系统/设置的更多详细信息:
'''
sys: linux
3.6.1 (default, Jun 27 2017, 14:35:15) .. [GCC 7.1.1 20170622 (Red Hat 7.1.1-3)]
multiprocessing: 1 CPU(s)
psutil: 1 CPUs PHYSICAL
psutil: 1 CPUs LOGICAL
psutil: psutil.cpu_freq( per_cpu = True ) not able to report. ?( v5.1.0+ )
psutil: 5.0.1
psutil: psutil.cpu_times( per_cpu = True ) not able to report. ?( vX.Y.Z+ )
psutil: 5.0.1
psutil: svmem(total=1039192064, available=257290240, percent=75.2, used=641396736, free=190361600, active=581107712, inactive=140537856, buffers=12210176, cached=195223552, shared=32768)
numexpr: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ModuleNotFoundError: No module named 'numexpr'.
joblib: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ModuleNotFoundError: No module named 'joblib'.
sklearn/joblib: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ModuleNotFoundError: No module named 'sklearn.externals.joblib'
'''
或者
''' [i5]
>>> numexpr.print_versions()
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Numexpr version: 2.5
NumPy version: 1.10.4
Python version: 2.7.13 |Anaconda 4.0.0 (32-bit)| (default, May 11 2017, 14:07:41) [MSC v.1500 32 bit (Intel)]
AMD/Intel CPU? True
VML available? True
VML/MKL version: Intel(R) Math Kernel Library Version 11.3.1 Product Build 20151021 for 32-bit applications
Number of threads used by default: 4 (out of 4 detected cores)
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
'''
... which is better to use with GridSearchCV to pick the best parameter set for a model,
n_jobs = -1
orn_jobs
with a big number liken_jobs = 30
?
对此没有简单的“一刀切”答案:
Scikit 工具(以及许多其他遵循这种做法的工具)用于在使用 n_jobs
指令时生成所需数量的并发进程实例(以便从共享 GIL 锁步进中逃脱 - 如果对细节感兴趣,请阅读其他地方的更多信息)。
这个过程实例化不是免费的(时间方面,即花费相当数量的[TIME]
-域成本,还有空间-明智的,即花费至少 n_jobs
倍于 [SPACE]
中单个 python 进程实例的 RAM 分配/strong>-域)。
鉴于此,您的战斗是与双刃剑的战斗。
尝试“underbook”CPU 将使(一些)CPU 核心可能空闲。
尝试“超额预订”RAM-空间会使您的性能比预期更差,因为虚拟内存会导致操作系统交换,从而使您的机器学习规模的数据访问时间从~ 10+[ns]
慢了 100,000 多倍 ~ 10+ [ms]
这可不是什么令人高兴的事。
n_jobs = a_reasonable_amount_of_processes
的整体效果是 Amdahl's Law ( the re-formulated one, not an add-on overhead-naive version ) 的主题,因此将有一个实际的最优峰值(最大值),即有多少 CPU 内核将有助于改善一个人的处理意图,超过这个峰值,开销成本(为 [TIME]
绘制的草图 -和 [SPACE]
- 上面的域)实际上会降低任何潜在的积极影响预期。
在生产中对大型数据集使用 RandomForestRegressor()
后,我可以告诉您 [SPACE]
-domain 是你试图进一步增加 n_jobs
的最大敌人,没有任何系统级调整可以克服这个边界(因此越来越多的超低延迟 RAM 和越来越多的(真实)CPU 核心是进入任何更大的 n_jobs
计算计划的唯一实用方法。
关于python - 如何在 GridSearchCV( ..., n_jobs = ... ) 中找到最佳进程数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50183080/