Anaconda下 Gensim FAST_VERSION 無效的解決方法

環境: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 installed
pip uninstall scipy ## only if scipy is already installed
export LAPACK=/usr/lib/liblapack.so
export ATLAS=/usr/lib/libatlas.so
pip install numpy
pip install scipy

最終解決了gensim.models.word2vec.FAST_VERSION的問題。

因爲在網上找了許多資料,沒有從頭到尾解決這個問題的,所以記錄下探究歷程以及解決方式,方便後來人。

更多關於gensim性能優化的信息請參閱這篇博文

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章