LoginSignup
22
24

More than 5 years have passed since last update.

anacondaのgensim word2vecよりpipのgensim word2vecの方が実行速度が3倍以上高速になった理由

Last updated at Posted at 2017-07-22

gensimのword2vec使ってみた

今回はgensimのword2vecを実行したときの実行速度が
conda installでインストールしたgensimをimportした場合と
pip installでインストールしたgensimをimportした場合で
実行速度が大幅に違ったというお話。
これはgensim Doc2vecでも同様に起こるようだ。
その原因を探った。
パット見では日本語記事で解説してるページはなさそう。(2017/Jul)
なお、本記事はすべて憶測であり、確証はない。

cf.

前置き

  • anaconda環境を構築(Linux or MacOS)
    • anacondaでgensimをインストールする
      1. gensimのword2vecを実行
      2. 実行時間計測
    • pipでgensimをインストールする
      1. gensimのword2vecを実行
      2. 実行時間計測
  • anacondaとpipで、gensim word2vecの実行時間を比較

anacondaを使ってgensimをインストールするとき

install時のlogも掲載

$ conda install --channel https://conda.anaconda.org/anaconda gensim
Fetching package metadata ...........
Solving package specifications: .

Package plan for installation in environment ...

The following NEW packages will be INSTALLED:

    bz2file:      0.98-py35_0         anaconda
    gensim:       2.2.0-np113py35_0   anaconda
    pywavelets:   0.5.2-np113py35_0   anaconda
    smart_open:   1.5.3-py35_0        anaconda

The following packages will be UPDATED:

    anaconda:     4.2.0-np111py35_0            --> custom-py35_0      anaconda
...
    scipy:        0.18.1-np111py35_0           --> 0.19.1-np113py35_0 anaconda
    statsmodels:  0.6.1-np111py35_1            --> 0.8.0-np113py35_0  anaconda

Proceed ([y]/n)? y
...
gensim-2.2.0-n 100% |####################################################| Time: 0:02:24  41.92 kB/s
...
$ conda list | grep gensim ### anacondaでgensimがinstallされていることを確認
gensim                    2.2.0               np113py35_0    anaconda

pipを使ってgensimをインストールするとき

install時のlogも掲載

$ pip install gensim
Collecting gensim
  Downloading gensim-2.2.0-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10
_x86_64.whl (6.4MB)
    100% |████████████████████████████████| 6.4MB 68kB/s
Collecting scipy>=0.19.0 (from gensim)
  Downloading scipy-0.19.1-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10
_x86_64.whl (16.1MB)
    100% |████████████████████████████████| 16.1MB 33kB/s
Requirement already satisfied (use --upgrade to upgrade): six>=1.5.0 in ./.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site
-packages (from gensim)
Collecting smart-open>=1.2.1 (from gensim)
  Downloading smart_open-1.5.3.tar.gz
Collecting numpy>=1.11.3 (from gensim)
  Downloading numpy-1.13.1-cp35-cp35m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10
_x86_64.whl (4.5MB)
    100% |████████████████████████████████| 4.5MB 76kB/s
Requirement already satisfied (use --upgrade to upgrade): boto>=2.32 in ./.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages (from smart-open>=1.2.1->gensim)
Collecting bz2file (from smart-open>=1.2.1->gensim)
  Using cached bz2file-0.98.tar.gz
Requirement already satisfied (use --upgrade to upgrade): requests in ./.pyenv/versions/anaconda3-4.2.0/lib/python3.5/site-packages (from smart-open>=1.2.1->gensim)
Building wheels for collected packages: smart-open, bz2file
  Running setup.py bdist_wheel for smart-open ... done
  Stored in directory: ...
  Running setup.py bdist_wheel for bz2file ... done
  Stored in directory: ...
  Running setup.py bdist_wheel for bz2file ... done
  Stored in directory: ...
Successfully built smart-open bz2file
Installing collected packages: numpy, scipy, bz2file, smart-open, gensim
  Found existing installation: numpy 1.11.1
    Uninstalling numpy-1.11.1:
      Successfully uninstalled numpy-1.11.1
  Found existing installation: scipy 0.18.1
    Uninstalling scipy-0.18.1:
      Successfully uninstalled scipy-0.18.1
Successfully installed bz2file-0.98 gensim-2.2.0 numpy-1.13.1 scipy-0.19.1 smart-open-1.5.3

$ conda list | grep gensim ### pipでgensimがinstallされていることを確認
gensim                    2.2.0                     <pip>
  • 同じマシン、同じword2vecソースコードでそれぞれのライブラリのパフォーマンスを比較する
gensim_word2vec.py(python3)
import cython
from gensim.models import word2vec
import logging
import sys
import numpy

# logging.basicConfig() ### 処理速度(word/s)の出力方法忘れた〜🙅
sentences = word2vec.Text8Corpus('./input.txt')

model = word2vec.Word2Vec(
    sentences#,
    # ...
)

fname = './output.model'
model.wv.save_word2vec_format(fname,binary=False)

結果

  • conda installしたgensim word2vec
    • とても遅い(pipの3倍以上の実行時間)
  • pip installしたgensim word2vec

conda installとpip installの違い

installの仕組み

conda installとpip installではbuildする場所が違う。
処理の順序を追いながら上記インストール時のlogをよく読んでみると分かる。

  • conda installではすでにどこかでbuildされたpackageをfetchするだけ。
  • pip installはlocalの実行環境と互換のあるwheelを取得した後、localの実行環境でsetup.pyを実行してbuildしている。

buildするタイミングでlocalの実行環境に合うようライブラリが最適化される。つまり、

  • conda installされたライブラリはlocalの実行環境で最適化されているとは言えない。
  • pip installされたライブラリはlocalの実行環境で高性能を発揮できるよう最適化されている。

gensim word2vecの性能に関するパラメータ

ビルドされたgensimのword2vecやDoc2vecのパフォーマンス最適化はFAST_VERSIONというパラメータを調べれば分かるようだ。

stack over flowとかで調べてみると、ビルドするマシンに入っているCコンパイラによってgensimのビルドを最適化させているようだ。多分、、、

FAST_VERSION 意味
1 最適でない
0 最適化されている
-1 Cコンパイラ入ってない(?)

なのかな???
パラメータが示す詳しい意味はぶっちゃけよくわからないので各人の調査に任せたい。

  • conda installしたgensimのFAST_VERSION ... 1
anacondaを使用した場合
$ conda list | grep gensim
gensim                    2.2.0               np113py35_0    anaconda
$ python
Python 3.5.2 |Anaconda custom (x86_64)| (default, Jul  2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from gensim.models import word2vec
>>> word2vec.FAST_VERSION
1
  • pip installしたgensimのFAST_VERSION ... 0
pipを使用した場合
$ conda list | grep gensim
gensim                    2.2.0                     <pip>
$ python
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul  2 2016, 17:52:12)
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from gensim.models import word2vec
>>> word2vec.FAST_VERSION
0

anacondaとpipの使い分け

ライブラリを管理するのはanacondaが便利かもしれない。
が、word2vecやDoc2vec等、Deep Learning系の重い処理をしたい場合は、
pipでinstallしたものを使用した方が良さそう…。(?)

22
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
24