【发布时间】:2017-03-10 15:26:10
【问题描述】:
出于某种原因,sklearn.ensemble 中的 RandomForestClassifier.fit 在我的本地计算机上仅使用了 2.5GB 内存,但在我的服务器上使用了几乎 7GB 的内存,并且训练集完全相同。
没有导入的代码差不多是这样的:
y_train = data_train['train_column']
x_train = data_train.drop('train_column', axis=1)
# Difference in memory consuming starts here
clf = RandomForestClassifier(n_estimators=100, random_state=42)
clf = clf.fit(x_train, y_train)
preds = clf.predict(data_test)
我的本地机器是 16GB 内存和 4 核 CPU 的 macbook pro 我的服务器是 digitalocean 云上的 Ubuntu 服务器,具有 8 GB 内存和 4 核 CPU。
sklearn 版本为 0.18,Python 版本为 3.5.2
我什至无法想象可能的原因,任何帮助都会非常有帮助。
更新
内存错误出现在fit方法内的这段代码中:
# Parallel loop: we use the threading backend as the Cython code
# for fitting the trees is internally releasing the Python GIL
# making threading always more efficient than multiprocessing in
# that case.
trees = Parallel(n_jobs=self.n_jobs, verbose=self.verbose,
backend="threading")(
delayed(_parallel_build_trees)(
t, self, X, y, sample_weight, i, len(trees),
verbose=self.verbose, class_weight=self.class_weight)
for i, t in enumerate(trees))
更新 2
关于我的系统的信息:
# local
Darwin-16.1.0-x86_64-i386-64bit
Python 3.5.2 (default, Oct 11 2016, 05:05:28)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)]
NumPy 1.11.2
SciPy 0.18.1
Scikit-Learn 0.18
# server
Linux-3.13.0-57-generic-x86_64-with-Ubuntu-16.04-xenial
Python 3.5.1 (default, Dec 18 2015, 00:00:00)
[GCC 4.8.4]
NumPy 1.11.2
SciPy 0.18.1
Scikit-Learn 0.18
还有我的 numpy 配置:
# server
>>> np.__config__.show()
blas_opt_info:
libraries = ['openblas', 'openblas']
define_macros = [('HAVE_CBLAS', None)]
library_dirs = ['/usr/local/lib']
language = c
openblas_info:
libraries = ['openblas', 'openblas']
define_macros = [('HAVE_CBLAS', None)]
library_dirs = ['/usr/local/lib']
language = c
lapack_opt_info:
libraries = ['openblas', 'openblas']
define_macros = [('HAVE_CBLAS', None)]
library_dirs = ['/usr/local/lib']
language = c
blas_mkl_info:
NOT AVAILABLE
openblas_lapack_info:
libraries = ['openblas', 'openblas']
define_macros = [('HAVE_CBLAS', None)]
library_dirs = ['/usr/local/lib']
language = c
# local
>>> np.__config__.show()
blas_opt_info:
extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']
define_macros = [('NO_ATLAS_INFO', 3), ('HAVE_CBLAS', None)]
extra_compile_args = ['-msse3', '-I/System/Library/Frameworks/vecLib.framework/Headers']
blas_mkl_info:
NOT AVAILABLE
atlas_threads_info:
NOT AVAILABLE
lapack_mkl_info:
NOT AVAILABLE
openblas_lapack_info:
NOT AVAILABLE
atlas_info:
NOT AVAILABLE
atlas_3_10_blas_info:
NOT AVAILABLE
lapack_opt_info:
extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']
define_macros = [('NO_ATLAS_INFO', 3), ('HAVE_CBLAS', None)]
extra_compile_args = ['-msse3']
openblas_info:
NOT AVAILABLE
atlas_3_10_blas_threads_info:
NOT AVAILABLE
atlas_3_10_threads_info:
NOT AVAILABLE
atlas_3_10_info:
NOT AVAILABLE
atlas_blas_threads_info:
NOT AVAILABLE
atlas_blas_info:
NOT AVAILABLE
clf 对象的Repr 在两台机器上是相同的:
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
max_depth=None, max_features='auto', max_leaf_nodes=None,
min_impurity_split=1e-07, min_samples_leaf=1,
min_samples_split=2, min_weight_fraction_leaf=0.0,
n_estimators=100, n_jobs=1, oob_score=False, random_state=42,
verbose=0, warm_start=False)
【问题讨论】:
-
想到的一件事是,与您的 VPS 相比,您的 Macbook 上安装的 BLAS、LAPACK 和其他 C 库可能非常不同。使用不同的算法(甚至是相同算法的不同实现)来计算在将模型拟合到数据时使用的东西肯定可以解释内存爆炸的原因。首先要查看的是您在各自系统上的numpy information。
-
嗯,我的 numpy 在两台机器上都是一样的。正如我所见,唯一显着的区别是 GCC。
-
这不是 numpy 本身的版本,但库 numpy 是针对它编译的,这很可能在这里发挥作用。这也可能是 GCC 版本的差异。
-
我知道了,刚刚更新了问题
-
我看到的最大区别是您的 Ubuntu 库不是针对 SSE3 编译的,而您的 Apple 库是针对他们的几个库编译的。 SSE3 导致内存使用量激增对我来说似乎很可疑,尽管我不是该领域的专家。我不熟悉的 Apple 库,但我可以看到对这个问题负责。
标签: python machine-learning scikit-learn