Anaconda下 Gensim FAST_VERSION 无效的解决方法

来源:互联网 发布:电脑软件制作 编辑:程序博客网 时间:2024/06/06 11:49

环境:ubuntu 14.04, anaconda python 2.7

在Anaconda环境下安装gensim,直接安装是无法使用FAST_MODE的。因为anaconda中带有的scipy是没有BLAS原生库支持的。这样安装的gensim调用scipy中的算法时,无法使用C语言原生库进行计算,速度会比较慢。
表现出来的现象就是,

UserWarning: C extension not loaded for Word2Vec, training will be slow. Install a C compiler and reinstall gensim for fast training.

一开始遇到这个问题,我以为是因为在安装gensim时没有C编译器,所以相应类库无法从源码编译。但安装了build-essential 之后重新安装gensim,问题依旧存在。而且在使用pip或anaconda安装gensim时并没有编译的过程,即使下载源码编译并且使用 python setup.py build_ext --inplace。于是断定问题应该出在间接调用的模块中。

经过搜索,在google group 中进一步定位到问题:

The scipy version (0.16.0) I am using does not have the scipy.linalg.blas.fblas module as it has been depracated. The Cython/C wrapper for the fast bit of word2vec and doc2vec however still expect that module to be there. Everything compiles fine but importing gensim.models.word2vec_inner fails because it can’t find the fblas module in scipy.
– Matti Lyra

至此,已经发现问题所在,是gensim间接调用scipy的线性代数模块时出现找不到C语言类库。gensim本身带有的错误提示具有相当的误导性,仅仅重新安装gensim本身并不会解决这个问题。

在ubuntu下直接使用pip安装scipy会出现无法找到Lapack支持的问题,网上列出了许多解决方法,如安装依赖库

sudo apt-get install libblas-dev liblapack-dev libatlas-base-dev gfortran

或使用apt-get install python-scipy,靠apt-get来自动化解决依赖问题。但这些方法并不适用anaconda环境。
后来找到了一篇博文,介绍如何使用pip安装scipy,直接通过环境变量引导pip发现系统lapack支持:

pip uninstall numpy ## only if numpy is already installedpip uninstall scipy ## only if scipy is already installedexport LAPACK=/usr/lib/liblapack.soexport ATLAS=/usr/lib/libatlas.sopip install numpypip install scipy

最终解决了gensim.models.word2vec.FAST_VERSION的问题。

因为在网上找了许多资料,没有从头到尾解决这个问题的,所以记录下探究历程以及解决方式,方便后来人。

更多关于gensim性能优化的信息请参阅这篇博文。

1 0
原创粉丝点击