sklearn OMP:拟合模型时出现错误#15

时间:2013-12-12 20:56:11

标签: python scikit-learn enthought canopy

我最近卸载了一个很好的Enthought Canopy 32位工作副本,并安装了Canopy版本1.1.0(64位)。当我尝试使用sklearn来适应模型时,我的内核崩溃了,我收到以下错误:

The kernel (user Python environment) has terminated with error code 3. This may be due to a bug in your code or in the kernel itself.

Output captured from the kernel process is shown below.

OMP: Error #15: Initializing libiomp5md.dll, but found mk2iomp5md.dll already initialized.
OMP: Hint: This means that multiple copies of the OpenMP runtime have been linked into the program. That is dangerous, since it can degrade performance or cause incorrect results. The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://www.intel.com/software/products/support/.

相同的代码在Canopy的32位下运行得很好。代码实际上只是一个linear_model.SGDClassifier(loss ='log')的简单拟合(Logistic回归的相同错误,没有尝试过其他模型)

我该如何解决这个问题?

4 个答案:

答案 0 :(得分:3)

我已阅读有关英特尔支持研究 (http://www.intel.com/software/products/support/) 的文档以及这种情况下的原因,包括。对我来说,是 numpy 图书馆。 我已经单独安装了它,也作为 PyTorch 安装的一部分进行了安装。 所以它给出了一个错误。 基本上,您应该创建一个新环境,并在那里安装所有依赖项。

答案 1 :(得分:2)

我遇到了同样的问题,来自于numpy和canopy的冲突装置。通过写作解决了这个问题:

import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

不是一个优雅的解决方案,但它为我完成了这项工作。

答案 2 :(得分:0)

几乎可以肯定的是,通过设置环境参数可以克服此错误

import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"

但是,不建议您设置此参数(如错误消息中所述)。相反,您可以通过运行以下命令来尝试设置conda环境而不使用英特尔数学内核库:

conda install nomkl

如果您使用的版本是基于MKL的,则在执行此操作后可能需要再次安装一些软件包(尽管conda应该为您完成此操作)。如果没有,那么您将需要执行以下操作:

  

通常会包含MKL或依赖于包含MKL的软件包的安装软件包,例如scipynumpypandas。 Conda将安装这些软件包的非MKL版本及其依赖项   (请参阅here

对于macOS,nomkl是一个不错的选择,因为MKL实际上已经由Apple的Accelerate Framework提供了优化,后者已经使用了OpenMP。实际上,这就是触发错误(“ ... OpenMP运行时的多个副本...”)的原因,看来(如this answer中所述)。

答案 3 :(得分:0)

也许这个解决方案对 sklearn 也有帮助。面对 tensorflow 的相同错误 #15,尽管有帮助,但迄今为止(2021 年 2 月 5 日)没有一个解决方案完全有效。但是,我确实设法解决了它,同时避免了:使用 dylib 库抖动、从源代码安装或设置环境变量 KMP_DUPLICATE_LIB_OK=TRUE 及其作为“不安全、不受支持、未记录的解决方法”的缺点及其潜在的“崩溃或默默地产生不正确的结果”。

问题在于,尽管加载了 nomkl 包,但 conda 并未获取 tensorflow (v2.0.0) 的非 mkl 构建。最终使该解决方案起作用的是:

  • 确保我是从 defaults 通道加载包(即从具有非 mkl 版本的 tensorflow 的通道。截至 2021 年 2 月 5 日,conda-forge 没有 2.0 或 更大)。
  • 指定我想要的 tensorflow 版本的精确构建:tensorflow>=2.*=eigen_py37h153756e_0。如果没有这个,conda 会继续加载 mkl_... 版本的包,尽管 nomkl 包也被加载。

我使用以下 environment.yml 文件(根据 conda documentation for managing environments)创建了一个 conda 环境:

name: tf_nomkl
channels:
  - conda-forge
  - defaults
dependencies:
  - nomkl
  - python>=3.7
  - numpy
  - scipy
  - pandas
  - jupyter
  - jupyterlab
  - nb_conda
  - nb_conda_kernels
  - ipykernel
  - pathlib
  - matplotlib
  - seaborn
  - tensorflow>=2.*=eigen_py37h153756e_0

您可以尝试在没有 environment.yml 文件的情况下执行相同操作,但如果可以,最好一次性加载环境中所需的所有包。 此解决方案适用于 MacOS Big Sur v11.1。