LoginSignup
14
15

More than 5 years have passed since last update.

TensorFlowをソースからインストールしてみた

Posted at

最近よく聞くようになった機械学習やディープラーニングというものに興味が出てきたので、Googleが提供する機械学習ライブラリTensorFlowをMacにソースからインストールしてみました。
その時にメモです。

※ 公式ドキュメントのDownload and Setupに従って実施しました。

環境など

  • Mac OSX El Capitan(10.11.3)
  • Python 2.7.10
  • Homebrewインストール済

インストール手順

今回インストールしたTensorFlowのバージョンはr0.7です。
また、GPUモードは外しています。

1. ソースをgit clone

任意の場所にソースをgit cloneしてきます。

$ git clone -b r0.7 --recurse-submodules https://github.com/tensorflow/tensorflow

2. 周辺ツールをインストール

必要なツール群があるようなので、Homebrewがeasy_installを使ってインストールします。

$ brew install bazel swig
$ sudo easy_install pip
$ sudo easy_install -U six
$ sudo easy_install -U numpy
$ sudo easy_install wheel

numpyはwarnningが出たけれどとりあえず無視しても大丈夫そうです。

3. TensorFlowのインストール

3.1. configure

TensolFlowのHomeディレクトリでconfigureを実行します。

$ ./configure
Please specify the location of python. [Default is /usr/bin/python]: # 特に問題なければEnter

Do you wish to build TensorFlow with GPU support? [y/N] # 今回はGPUサポートは入れないのでNoにしました

No GPU support will be enabled for TensorFlow
Configuration finished

3.2. pipパッケージの構築

bazel経由でpipパッケージを構築します。

$ bazel build -c opt //tensorflow/tools/pip_package:build_pip_package
Extracting Bazel installation...
Sending SIGTERM to previous Bazel server (pid=43550)... done.
.......
(省略)
Target //tensorflow/tools/pip_package:build_pip_package up-to-date:
  bazel-bin/tensorflow/tools/pip_package/build_pip_package
INFO: Elapsed time: 535.683s, Critical Path: 399.63s

結構時間がかかる上にwarningが大量に出ましたが無事終了しました。

ちなみに、bazelのバージョンが0.1.4以上でないとエラーになるようです(issue)。
私も最初エラーになったのでbazelをbrew upgradeで0.2.0まで上げるとうまくいきました。

次に以下のコマンドでwhlを生成。

$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
2016年 2月27日 土曜日 16時13分30秒 JST : === Using tmpdir: /var/folders/dy/20s_5s4n5cd3y8nj8nghhmv40000gn/T/tmp.XXXXXXXXXX.2gywc8YW
/var/folders/dy/20s_5s4n5cd3y8nj8nghhmv40000gn/T/tmp.XXXXXXXXXX.2gywc8YW ~/git/tensorflow
2016年 2月27日 土曜日 16時13分30秒 JST : === Building wheel
~/git/tensorflow
2016年 2月27日 土曜日 16時13分34秒 JST : === Output wheel file is in: /tmp/tensorflow_pkg

3.3. インストール

生成されたwhlからpip installでTensolFlowをインストールします。
whlファイル名は環境によって異なるらしいので適宜読み替えてください。
また、TensorFlowのドキュメントにはsudoついてないですが、自分の環境ではsudoつけないとPermissionErrorになってしまいました。

$ sudo pip install /tmp/tensorflow_pkg/tensorflow-0.7.1-py2-none-any.whl
The directory '/Users/hoge/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/hoge/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Processing /tmp/tensorflow_pkg/tensorflow-0.7.1-py2-none-any.whl
Requirement already satisfied (use --upgrade to upgrade): six>=1.10.0 in /Library/Python/2.7/site-packages/six-1.10.0-py2.7.egg (from tensorflow==0.7.1)
Collecting protobuf==3.0.0b2 (from tensorflow==0.7.1)
  Downloading protobuf-3.0.0b2-py2.py3-none-any.whl (326kB)
    100% |████████████████████████████████| 327kB 1.0MB/s
Requirement already satisfied (use --upgrade to upgrade): wheel in /Library/Python/2.7/site-packages/wheel-0.29.0-py2.7.egg (from tensorflow==0.7.1)
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.10.1 in /Library/Python/2.7/site-packages/numpy-1.11.0b3-py2.7-macosx-10.11-intel.egg (from tensorflow==0.7.1)
Requirement already satisfied (use --upgrade to upgrade): setuptools in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python (from protobuf==3.0.0b2->tensorflow==0.7.1)
Installing collected packages: protobuf, tensorflow
Successfully installed protobuf-3.0.0b2 tensorflow-0.7.1

これでインストール完了です。

Exampleをやってみる

Train your first TensorFlow neural net modelというのがあったので動作確認の意味も含めてやってみました。

インストールしたTensorFlowのHomeディレクトリで以下を実行します。

$ cd tensorflow/models/image/mnist/
$ python convolutional.py
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting data/train-images-idx3-ubyte.gz
Extracting data/train-labels-idx1-ubyte.gz
Extracting data/t10k-images-idx3-ubyte.gz
Extracting data/t10k-labels-idx1-ubyte.gz
Initialized!
Step 0 (epoch 0.00), 3.1 ms
Minibatch loss: 12.054, learning rate: 0.010000
Minibatch error: 90.6%
Validation error: 84.6%
Step 100 (epoch 0.12), 203.5 ms
Minibatch loss: 3.279, learning rate: 0.010000
Minibatch error: 6.2%
Validation error: 6.8%
Step 200 (epoch 0.23), 199.0 ms
Minibatch loss: 3.479, learning rate: 0.010000
Minibatch error: 15.6%
Validation error: 3.7%
Step 300 (epoch 0.35), 192.2 ms
Minibatch loss: 3.214, learning rate: 0.010000
Minibatch error: 9.4%
Validation error: 3.2%
...

ちゃんと動きました。
ちなみに、待っててもずっと終わらないので見飽きたらCtrl-Cで止めましょう。

所感

若干ハマりつつもそれほど時間もかからずインストールすることができました。
そんなに難しくなかったので、masterを常に追っていきたいとか実際のコードや動作が気になるようなCoreな方はやはりソースからインストールした方がいろいろメリットがあるのではないかと思います。

今後としてはソースをみながら機械学習の学習を進めていきたいです。

※ ちなみにソースからインストールしなくてももちろん利用可能です。
こちらも非常に簡単です。

[ソースはいらないという方]
Pip Install

[自分の環境を汚さずちょっと試してみたいという方]
Virtualenv installation
or
Docker installation

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