LoginSignup
17

More than 5 years have passed since last update.

(初心者向け)TensorFlowをインストールする

Last updated at Posted at 2017-06-22

MacbookでTensorFlowでAIを使ってみよう、という企画です
(初心者向け)TensorFlow用に環境を確認するでTensorFlowをMacbookで動かすための環境を確認しました

1. TensorFlowのインストール手順を確認する

2. virtualenvを用意しTensorFlowをインストールする

  • 上記サイトのページが改善されている可能性があるので、上記サイトのページを読みながら進めてもらうことをお薦めします
  • Python 3を使います
  • ターミナルを開き
  • 次のコマンドでvirtualenvをインストールします
pipのインストール
 sudo easy_install pip
virtualenvのインストール
 sudo pip install --upgrade virtualenv 
  • 次のコマンドで「tf」という名前でTensorFlow用の仮想環境(virtualenv)を用意します
TensorFlow用の仮想環境を用意
virtualenv --system-site-packages -p python3 tf
  • 上記のステップで「python3 does not exist」というエラーになってしまうかたはこのページ末尾の「Macbookへのpython 3のインストール」手順をご参照ください。
  • ページ末尾のpython3のインストールが完了されたかたは、ここから継続しますので、上記のvirtualenv --system-site-packages -p python3 tfをやり直してください。

  • 作成したTensorFlow用仮想環境(virtualenv)へ入るには、上記と同じディレクトリから以下のコマンドを入力します

仮想環境へ入る
source tf/bin/activate
MacBook-Pro:tftest tohru$ source tf/bin/activate
(tf) MacBook-Pro:tftest tohru$ 

  • プロンプトに(tf)が表示され仮想環境に入っていることを示しています。このあとのハンズオンはこの(tf)プロンプトから実行していきます

  • 仮想環境に入っている状態で、python --versionコマンドでこの仮想環境でpython3が使用可能になっていることを確認します

python3を確認
(tf) MacBook-Pro:tftest tohru$ python --version
Python 3.6.2

3. TensorFlowをインストールする

  • TensorFlow仮想環境に入っている(プロンプトに(tf)と表示)ターミナルで、次のコマンドでTensorFlowをインストールします
TensorFlowをインストール!
pip3 install --upgrade tensorflow  
  • もしバージョンを固定してtensorflowをインストールしたい場合には以下のように指定します(下記はtensorflow 1.4をインストールしたい場合)
TensorFlow1.4をインストール!
pip3 install tensorflow==1.4  
  • しばらくいろいろな表示が続いたあと、下記のような出力が得られればインストールは成功です
TensorFlowインストール完了
 :
Successfully installed markdown-2.6.9 protobuf-3.4.0 tensorflow-1.3.0 tensorflow-tensorboard-0.1.5
(tf) MacBook-Pro:tftest tohru$ 

  • TensorFlowが正しくインストールされているかを確認するために簡単なプログラムを実行してみます
  • TensorFlow仮想環境に入っている(プロンプトに(tf)と表示)ターミナルで、pythonと入力し、pythonスクリプト稼働環境を起動します
pythonを起動
(tf) MacBook-Pro:tftest tohru$ python
Python 3.6.2 (default, Jul 23 2017, 15:12:46) 
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

  • pythonのプロンプト(>>>)が表示されているターミナルで、下記のプログラムをコピペして貼り付けて実行してみます
インストール確認プログラム
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
  • 上記のプログラムが実行され、下記が出力されればTensorFlowは正しくインストールされています
インストールを確認
 :
>>> print(sess.run(hello))
b'Hello, TensorFlow!'
  • pythonの環境から出るには[control]+Dを押します
  • (参考)TensorFlow仮想環境から出るには以下のコマンドを使用します。(まあターミナルを閉じてしまってもかまわないわけですが。)
仮想環境から出る
(tf)$ deactivate 
  • プロンプトの(tf)がなくなり通常のshell環境へ戻ったことがわかります

4. jupyter notebookをインストール

  • このあとのハンズオンで使用するjupyter notebookをインストールします
  • TensorFlow仮想環境に入っている(プロンプトに(tf)と表示)ターミナルで、pip3 install jupyter notebookを実行します
(tf) MacBook-Pro:tftest tohru$ pip3 install jupyter notebook
Collecting jupyter
  Using cached jupyter-1.0.0-py2.py3-none-any.whl
  :
  • 下記のような出力でインストールが成功したことを確認します
 :
Successfully installed MarkupSafe-1.0 appnope-0.1.0 decorator-4.1.2 (...中略...) testpath-0.3.1 tornado-4.5.2 traitlets-4.3.2 wcwidth-0.1.7 widgetsnbextension-3.0.2
(tf) MacBook-Pro:tftest tohru$ 

5. Jupyter notebookの稼働を確認します

  • jupyter notebook で起動します
  • 8888にアクセスしJupyter notebookの稼働を確認します
jupyter-notebookを起動
(tf)MacBook-Pro:jn tohru$ jupyter notebook
[I 12:43:15.142 NotebookApp] Serving notebooks from local directory: /Users/tohru/Documents/t/jn
[I 12:43:15.142 NotebookApp] 0 active kernels 
[I 12:43:15.142 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/?token=9c1766248add66ef456c2b16007ca1fbb7a65d341115a422
[I 12:43:15.142 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 12:43:15.143 NotebookApp] 

    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:

スクリーンショット 2017-08-28 13.38.49.png

  • Jupyter notebookの稼働を確認したら、ターミナル画面で"[Cntl]+C” → "y"で終了させます。
jupyter-notebookを終了させる
The Jupyter Notebook is running at: http://localhost:8888/?token=84965097bba3f7a41de339757f5f09c7f09728d91547d1f4
Shutdown this notebook server (y/[n])? y
[C 18:45:35.896 NotebookApp] Shutdown confirmed
[I 18:45:35.896 NotebookApp] Shutting down kernels
[I 18:45:36.201 NotebookApp] Kernel shutdown: 2a66ab7b-074a-4d97-af21-07fab1064555
[I 18:45:36.201 NotebookApp] Kernel shutdown: 72669938-e2df-45cb-bb1b-74371ee44197
[I 18:45:36.202 NotebookApp] Kernel shutdown: 2c593e93-5ecd-4736-86e4-d095a5ce951f
(tf) MacBook-Pro:tftest tohru$ 

6. matplotlibをインストール

  • 前項と同様に、このあとのハンズオンで使用するmatplotlibをインストールします
  • TensorFlow仮想環境に入っている(プロンプトに(tf)と表示)ターミナルで、pip3 install matplotlibを実行します
matplotlibをインストール
(tf) MacBook-Pro:tftest tohru$ pip3 install matplotlib
Collecting matplotlib
  Using cached matplotlib-2.0.2-cp36-cp36m-macosx_10_6_intel
  :
  • 下記のような出力でインストールが成功したことを確認します
matplotlibインストール成功を確認
 :
Successfully installed cycler-0.10.0 matplotlib-2.0.2 pyparsing-2.2.0 pytz-2017.2
(tf) MacBook-Pro:tftest tohru$ 

7. ターミナルが終了・閉じてしまった場合

  • ターミナルを閉じてしまったり、終了してしまった場合には、Macbook下部のDockから、またはファインダーの「アプリケーション」→「ユーティリティー」から「ターミナル」を起動します
  • 起動したターミナル画面でcd Desktop/tftestなどで移動し、source tf/bin/activateで仮想環境へ入り直します。

* MacbookへのPython 3のインストール

  • ステップ2の「virtualenvを用意しTensorFlowをインストールする」のvirtualenv --system-site-packages -p python3 tf で "python3 does not exist"エラーとなるかたは、Macbook内にpython3が入っていない可能性があります。下記の手順でpython3をインストールします
  • 1. 以下のコマンドでhomebrewをインストールします。homebrewをインストール済みのかたは当ステップはスキップしてください。参照:Homebrew macOS 用パッケージマネージャー
homebrewをインストールするコマンド
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

homebrewをインストール
Macbook-Pro:tftest tohru$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/share/doc/homebrew
 :
==> Installation successful!

==> Homebrew has enabled anonymous aggregate user behaviour analytics.
Read the analytics documentation (and how to opt-out) here:
  https://docs.brew.sh/Analytics.html

==> Next steps:
- Run `brew help` to get started
- Further documentation: 
    https://docs.brew.sh
Macbook-Pro:tftest tohru$ 

  • homebrewがインストールされたら、brew install python3でpython3 をインストールします。環境(Macbookの能力)によりこのステップは完了までに10分程度を要する可能性があります。
python3をインストール
Macbook-Pro:tftest tohru$ brew install python3
==> Installing dependencies for python3: pkg-config, readline, sqlite, gdbm, openssl, xz
==> Installing python3 dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.2.sierra.bo
 :
See: https://docs.brew.sh/Homebrew-and-Python.html
==> Summary
〓  /usr/local/Cellar/python3/3.6.2: 7,959 files, 110.4MB, built in 6 minutes 37 seconds
Macbook-Pro:tftest tohru$ 

ここまでで Macbookにpython3のインストールが完了されたら、「2. virtualenvを用意しTensorFlowをインストールする」へ戻り、virtualenv --system-site-packages -p python3 tfのステップから継続します。

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