LoginSignup
17
15

More than 5 years have passed since last update.

Mac (El Capitan) に Chainer, TensorFlow, DMTK をインストールする

Last updated at Posted at 2016-01-12

はじめに

※この記事は word2vec の各種実装の速度比較 の環境構築部分を抜き出したものですが、単体でも読める内容となっています。

機械学習のフレームワークである Chainer, TensorFlow, DMTK (Microsoft Distributed Machine Learning Toolkit) を Mac にインストールします (Ubuntu にインストールする手順 はこちら)。 Mac の場合は、最近の機種では NVIDIA の GPU が載っていないので、必然的に CPU モードでインストールすることになります。

なお、記事中で説明した手順を実行するスクリプトを含むリポジトリはここにあります: https://bitbucket.org/knzm/wordembedding-experiments

Chainer をインストールする

依存ライブラリを Homebrew で先にインストールしておきます。

$ brew tap homebrew/science
$ brew install openblas
$ brew install hdf5

virtualenv を作成して、

$ virtualenv ~/virtualenvs/chainer
$ source ~/virtualenvs/chainer/bin/activate

pip でインストール

$ pip install chainer

または開発版をインストールします。

$ git clone https://github.com/pfnet/chainer.git
$ cd chainer
$ pip install h5py
$ python setup.py develop --cupy-no-cuda

TensorFlow をインストールする

公式ドキュメント: https://www.tensorflow.org/versions/master/get_started/os_setup.html

virtualenv を作成して、

$ virtualenv ~/virtualenvs/tetnsorflow-cpu --system-site-packages
$ source ~/virtualenvs/tensorflow-cpu/bin/activate

公式に提供されている wheel ファイルをインストールします。

$ pip install https://storage.googleapis.com/tensorflow/mac/tensorflow-0.6.0-py2-none-any.whl

DMTK をインストールする

DMTK をビルドするには GCC 4.8 が必要なので、 Homebrew でインストールします。

$ brew install homebrew/versions/gcc48

wget コマンドがインストールされていなければインストールします。

$ brew install wget

途中 Makefile の中で GNU readlink を使っているので coreutils をインストールして有効化しておきます。

$ brew install coreutils
$ PATH=$(brew --prefix coreutils)/libexec/gnubin:$PATH

フレームワーク (multiverso) のビルド:

$ git clone https://github.com/Microsoft/multiverso.git
$ cd multiverso/third_party
$ CXX=g++-4.8 ./install.sh
$ rm lib/*.dylib
$ cd ..
$ make CXX=g++-4.8

ここで dylib を削除しているのは強制的にスタティックリンクさせるためです。 (参考: Forcing the Mac OS X linker to choose a static library )

次に個別のプロジェクトをビルドします。ここでは distributed_word_embedding を対象としますが、 lightlda, distributed_skipgram_mixture もほぼ同様の手順でビルドできます。

$ git clone https://github.com/Microsoft/distributed_word_embedding.git
$ cd distributed_word_embedding
$ ln -s ../multiverso

以下のようにソースコードを修正します。

--- src/memory_manager.h
+++ src/memory_manager.h
@@ -8,7 +8,8 @@
 #include <vector>
 #include <condition_variable>
 #include <cassert>
-#include <malloc.h>
+//#include <malloc.h>
+#include <cstdlib>
 #include <cstring>

 #include "constant.h"
diff --git a/src/util.h b/src/util.h
index a103b6b..c8842db 100644
--- a/src/util.h
+++ b/src/util.h
@@ -7,7 +7,8 @@

 #include <cstring>
 #include <cstdlib>
-#include <random>
+//#include <random>
+#include <cmath>
 #include <cassert>
 #include <exception>

また、 Makefile を修正して LD_FLAGS に -lpmpich を追加します。

--- Makefile
+++ Makefile
@@ -14,7 +14,7 @@

 INC_FLAGS = -I$(MULTIVERSO_INC)
 LD_FLAGS  = -L$(MULTIVERSO_LIB) -lmultiverso
-LD_FLAGS += -L$(THIRD_PARTY_LIB) -lzmq -lmpich -lmpl
+LD_FLAGS += -L$(THIRD_PARTY_LIB) -lzmq -lmpich -lpmpich -lmpl
 LD_FLAGS += -lpthread

 WORD_EMBEDDING_HEADERS = $(shell find $(PROJECT)/src -type f -name "*.h")

ビルド:

$ make CXX=g++-4.8

ビルドが終わると bin ディレクトリの下に実行ファイルが作られています。

17
15
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
17
15