そごうソフトウェア研究所

SOA、開発プロセス、ITアーキテクチャなどについて書いています。Twitterやってます@rsogo

macOSにTensorFlowをセットアップする (2018/04更新)

マニュアルInstalling TensorFlow on macOS  |  TensorFlowでオススメされているvirtualenvを使った、セットアップを行います。

このドキュメントは2018/04/29にMacBook Pro 2017年モデルに環境構築をするために、再確認して更新しています。

使用した環境

  • macOS High Sierra
  • Tensorflow 1.8
  • pip
    Pythonのパッケージ管理ツール
  • Virtualenv
    Virtualenv — virtualenv 15.1.0 documentation
    Virtualenvは、独立したPython実行環境を作ることができるツールです。Virtualenvを使うことで、他の環境から影響を受けたり、与えたりすることがないので、好き放題実験することができます。

事前準備

macOSにHomebrewのセットアップを行っています。

1. Python環境の構築

pyenvでPythonの複数環境を構築して、Python3をインストールしていきます。

1.1. pyenvをHomebrewでインストール

$ brew install pyenv

1.2. Python3をインストール

利用できるPythonのバージョンを確認

$ pyenv install -l
Available versions:
  2.1.3
  2.2.3

(略)

  3.6.2
  3.6.3
  3.6.4
  3.6.5
  3.7.0b2
  3.7-dev
  3.8-dev
  activepython-2.7.14
  
  (略)

Python 3をインストール

$ pyenv install 3.6.5
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.6.5.tar.xz...
-> https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
Installing Python-3.6.5...
python-build: use readline from homebrew
Installed Python-3.6.5 to /Users/rsogo/.pyenv/versions/3.6.5

2. Virtualenvのインストール

pipを使ってVirtualenvをインストールします。 --upgradeは関連パッケージに新しいバージョンがある場合は、更新するというオプションです。

$ sudo pip install --upgrade virtualenv
The directory '/Users/rsogo/Library/Logs/pip' or its parent directory is not owned by the current user and the debug log has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag.
The directory '/Users/rsogo/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 the -H flag.
You are using pip version 6.0.6, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
The directory '/Users/rsogo/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 the -H flag.
Collecting virtualenv
  Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
    100% |################################| 1.8MB 1.9MB/s 
Installing collected packages: virtualenv

Successfully installed virtualenv-15.1.0

3. Tensorflowの環境を作る

3.1 VirtualenvでTensorflowの環境を作る

-pオプションでセットアップしたpython3の環境を指定して、Virtualenv環境を作ります。 tensorflowというディレクトリをターゲットとしています。ディレクトリ名は任意です。

rsogo-Mac-Book-2:~ rsogo$ virtualenv --system-site-packages -p ~/.pyenv/versions/3.6.5/bin/python3.6 tensorflow
Running virtualenv with interpreter /Users/rsogo/.pyenv/versions/3.6.5/bin/python3.6
Using base prefix '/Users/rsogo/.pyenv/versions/3.6.5'
New python executable in /Users/rsogo/tensorflow/bin/python3.6
Also creating executable in /Users/rsogo/tensorflow/bin/python
Installing setuptools, pip, wheel...done.

3.2. Virtualenv環境を有効にする

cd tensorflow/
$ source bin/activate
(tensorflow) Ryohei-no-MacBook-Pro:tensorflow rsogo$ 

これで、作ったVirtualenv環境に入っています

3.3. Tensorflowのインストール

$ pip3 install --upgrade tensorflow
Collecting tensorflow
  Downloading tensorflow-1.2.1-cp36-cp36m-macosx_10_11_x86_64.whl (34.1MB)
    100% |████████████████████████████████| 34.1MB 37kB/s 

(略)

Successfully built protobuf markdown html5lib
Installing collected packages: six, protobuf, numpy, markdown, backports.weakref, html5lib, bleach, werkzeug, tensorflow
Successfully installed backports.weakref-1.0rc1 bleach-1.5.0 html5lib-0.9999999 markdown-2.6.8 numpy-1.13.1 protobuf-3.3.0 six-1.10.0 tensorflow-1.2.1 werkzeug-0.12.2

4. 動作確認

4.1. チュートリアルをやってみる

下記のソースをtutorial1.pyという名前で作成します。

import tensorflow as tf
node1 = tf.constant(3.0, dtype=tf.float32)
node2 = tf.constant(4.0) # also tf.float32 implicitly
print(node1, node2)

sess = tf.Session()
print(sess.run([node1, node2]))

node3 = tf.add(node1, node2)
print("node3: ", node3)
print("sess.run(node3): ", sess.run(node3))

4.2. 実行してみます

$ python tutorial1.py 
Tensor("Const:0", shape=(), dtype=float32) Tensor("Const_1:0", shape=(), dtype=float32)
2017-08-06 00:32:11.351356: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-06 00:32:11.351380: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-08-06 00:32:11.351385: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-06 00:32:11.351390: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
[3.0, 4.0]
node3:  Tensor("Add:0", shape=(), dtype=float32)
sess.run(node3):  7.0

動いてそうですね。