はじめに
TensorFlow 0.12からWindowsをサポートするようになりました。これにより、VirtualBoxやDockerを使う必要がなくなります。
【追記 2017/03/02】
2017/02/16にTensorFlow 1.00がリリースされました、遅ればせながら今回TensorFlowをバージョンアップしました。
【追記 2017/11/25】
2017/11/08にTensorFlow 1.40がリリースされました、遅ればせながら今回TensorFlowをバージョンアップしました。
TensorFlow 1.4.0 リリースノート(翻訳)
(tensorenv) C:\Users\(ユーザー)>pip install --ignore-installed --upgrade tensorflow
(tensorenv) C:\Users\(ユーザー)>python -c "import tensorflow; print(tensorflow.__version__);"
1.4.0
※TensorFlow(読み方:テンソルフローではなくテンサーフロー 参照:https://togetter.com/li/1027115)
qiita.com
yaju3d.hatenablog.jp
TensorFlowバージョンアップによる変更点
【追記 2017/03/08】
過去ブログのTensorFlow用プログラムでは、TensorFlowバージョンアップによりエラーや警告が出ますので、下記の点を修正してください。
tensorflow 1.0 error: ValueError: Only call sigmoid_cross_entropy_with_logits
with named arguments (labels=..., logits=..., ...)
https://github.com/tensorflow/tensorflow/issues/7814
tf.nn.sigmoid_cross_entropy_with_logits(u, y_)
↓ labels=, logits=を付ける
tf.nn.sigmoid_cross_entropy_with_logits(logits=u, labels=y_)
TensorFlowのinitialize_all_variablesがDeprecatedになった件
tf.initialize_all_variables
↓ 置き換える
tf.global_variables_initializer
環境
AnacondaとはPythonの数値計算環境を構築するために様々なパッケージをまとめた無料のディストリビューションです。 Anacondaをインストールをすると、NumPy,SciPy,matplotlib、scikit-learn等のパッケージがインストールされます。
下記サイトからAnaconda 4.2.0 For Windows PYTHON 3.5 64-BITをダウンロードしてください。
2017/11/25現在では、Anaconda 5.0.1になっています。
https://www.continuum.io/downloads#windows
自分は、「C:\Anaconda3」をインストール先にしました。
Anacondaのアップデート
2017/11/25現在では、Anaconda 5.0.1になっているので、全パッケージをアップデートしてみました。
(C:\Anaconda3) > conda list anaconda
# packages in environment at C:\Anaconda3:
#
anaconda 4.2.0 np111py35_0
anaconda-clean 1.0.0 py35_0
anaconda-client 1.5.1 py35_0
anaconda-navigator 1.3.1 py35_0
nb_anacondacloud 1.2.0 py35_0
下記コマンドを入力して、20~30分待ちます。
パッケージ管理を行う conda 自身を更新してから、他のパッケージを更新します。
通信エラーが出たら、再度「conda update --all」をします。
(C:\Anaconda3) > conda update conda
(C:\Anaconda3) > conda update --all
anacondaのバージョンがcustomになってしまったので、「conda update anaconda」を追加しました。
(C:\Anaconda3) > conda list anaconda
# packages in environment at C:\Anaconda3:
#
anaconda 5.0.0 py35h91a80f2_0
anaconda-clean 1.1.0 py35_0
anaconda-client 1.6.5 py35h18499f7_0
anaconda-navigator 1.6.8 py35h84e1151_0
anaconda-project 0.8.0 py35h54b767f_0
nb_anacondacloud 1.4.0 py35_0
AnacondaのTensorflow用環境を作成
Anaconda3 (64-bit)メニュー配下から「Anaconda Prompt」を選択します。
使用していくうちに開発環境を使い分けたい場合(共存できないライブラリがあったり新しいライブラリを試したいなど)がでてきます。そのような場合に環境管理システム(virtualenvやpyvenv)を使うのですが、Anacondaにはその上位版としてパッケージ管理システム(conda)があります。
参照:データサイエンティストを目指す人のpython環境構築 2016
今現在は開発環境を使い分ける必要がない方は、下記の「conda create」の作業は不要です。
「TensorFlowのインストール」に進んで下さい。
Tensorflow用環境を作成するため、コマンドプロンプト上で下記のコマンドを入力します。
名前は「tensorenv」にしました。envは環境(environment)の略称
※(ユーザー)のところは各自違います。
(C:\Anaconda3) C:\Users\(ユーザー)> conda create --name=tensorenv python=3.5
環境が作成されていきます、途中のProceedの確認は「y」を入力します。
Fetching package metadata .........
Solving package specifications: ..........
Package plan for installation in environment C:\Anaconda3\envs\tensorenv:
The following packages will be downloaded:
package | build
---------------------------|-----------------
pip-9.0.1 | py35_0 1.7 MB
The following NEW packages will be INSTALLED:
pip: 9.0.1-py35_0
python: 3.5.2-0
setuptools: 27.2.0-py35_1
vs2015_runtime: 14.0.25123-0
wheel: 0.29.0-py35_0
Proceed ([y]/n)? y
Fetching packages ...
pip-9.0.1-py35 100% |###############################| Time: 0:00:02 786.13 kB/s
Extracting packages ...
[ COMPLETE ]|##################################################| 100%
Linking packages ...
[ COMPLETE ]|##################################################| 100%
#
# To activate this environment, use:
# > activate tensorenv
#
# To deactivate this environment, use:
# > deactivate tensorenv
#
# * for power-users using bash, you must source
#
以下のコマンドで作った環境の一覧を表示できます。
(C:\Anaconda3) C:\Users\(ユーザー)>conda info -e
# conda environments:
#
tensorenv C:\Anaconda3\envs\tensorenv
root * C:\Anaconda3
作成した環境を有効にするには下記のコマンドを入力します。
※Windows版はsourceコマンドが無いため、activateからとなります。
(C:\Anaconda3) C:\Users\(ユーザー)>activate tensorenv
これで先頭に「(tensorenv)」が付くようになります。
(tensorenv) C:\Users\(ユーザー)>
ちなみに作成した環境から戻るには下記のコマンドを入力します。
(tensorenv) C:\Users\(ユーザー)> deactivate
TensorFlowのインストール
【追記】2017/11/25
TensorFlow 1.40が2017/11/08リリースされました。もはや普通に入れればいい。
(tensorenv) C:\Users\(ユーザー)>pip install --ignore-installed --upgrade tensorflow
Collecting tensorflow
Using cached tensorflow-1.4.0-cp35-cp35m-win_amd64.whl
︙
Successfully installed bleach-1.5.0 enum34-1.1.6 html5lib-0.9999999 markdown-2.6.9 numpy-1.13.3 protobuf-3.5.0.post1 setuptools-37.0.0 six-1.11.0 tensorflow-1.4.0 tensorflow-tensorboard-0.4.0rc3 werkzeug-0.12.2 wheel-0.30.0
Anaconda Navigatorを使えばもっとシンプルでインストールできる。
geeknavi.net
もはや、ごちゃごちゃしてしまっているが、下記はメモとして残しておく。
TensorFlowには、「CPU only」と「GPU enabled」の2種類が用意されています。今回は「CPU only」版をインストールします。
ファイル名はPython35にしているので「tensorflow-(version No)-cp35」となります。
バージョンは現時点(2016/12/06)で最新の「0.12.0」を入れます。
入力が長いのでクリップボードにコピーして貼り付ければいいです。
参照:https://www.tensorflow.org/versions/r0.12/get_started/os_setup.html#pip-installation-on-windows
【追記】2016/12/18
2016/12/18に確認したところ「0.12.0rc1」になっていましたので、下記は書き換えました。
(tensorenv) C:\Users\(ユーザー)>pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc1-cp35-cp35m-win_amd64.whl
【追記】2017/03/02
2017/02/16にV1.00がリリースされましたのでTensorFlowサイトに従いインストールしてみたところ、下記のエラー(not a supported wheel on this platform)が発生しました。
(tensorenv) C:\Users\(ユーザー)>pip install --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.0.0-cp35-cp35m-win_x86_64.whl
tensorflow-1.0.0-cp35-cp35m-win_x86_64.whl is not a supported wheel on this platform.
Anaconda用のWindows版でPython3.5のCPU版と何も間違ってないのにプラットフォーム違いなのは何でと、ネットで検索して同様のエラーの方が質問されていました。
stackoverflow.com
ここの回答によると、「win_x86_64.whl」版ではなく「win_amd64.whl」版をインストールしたら成功したとのこと。
下記の通りに無事、tensorflow 1.0.0 にバージョンアップ出来ました。
(tensorenv) C:\Users\(ユーザー)>pip install -U --ignore-installed --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-1.0.0-cp35-cp35m-win_amd64.whl
︙
Successfully installed appdirs-1.4.2 numpy-1.12.0 packaging-16.8 protobuf-3.2.0 pyparsing-2.1.10 setuptools-34.3.0 six-1.10.0 tensorflow-1.0.0 wheel-0.29.0
(tensorenv) C:\Users\(ユーザー)>python -c "import tensorflow; print(tensorflow.__version__);"
1.0.0
ちなみに拡張子「whl」はWheelの略で、Wheelとは最近になって導入された Python の新しいパッケージング規格です。whlファイルは単なる ZIP アーカイブなので unzip コマンドなどを使って容易に中身を見ることができます。
wheelはコンパイル済みのバイナリファイルを含むことができるのが大きな特徴です。
pip installするとその場でライブラリのコンパイルが走るのですが、コンパイル環境が無い場合はエラーでコケルというデメリットがあります。最近の pip が新しいパッケージング規格である Wheel をこっそりと裏側で使っています。
参照:Python: pip と Wheel キャッシュについて
インストールされます。(下記以降は記事当初の「0.12.0rc0」のままです。)
Collecting tensorflow==0.12.0rc0 from https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc0-cp35-cp35m-win_amd64.whl
Downloading https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc1-cp35-cp35m-win_amd64.whl (12.2MB)
100% |################################| 12.2MB 27kB/s
Collecting six>=1.10.0 (from tensorflow==0.12.0rc0)
Downloading six-1.10.0-py2.py3-none-any.whl
Collecting protobuf==3.1.0 (from tensorflow==0.12.0rc0)
Downloading protobuf-3.1.0-py2.py3-none-any.whl (339kB)
100% |################################| 348kB 475kB/s
Collecting numpy>=1.11.0 (from tensorflow==0.12.0rc0)
Downloading numpy-1.11.2-cp35-none-win_amd64.whl (7.6MB)
100% |################################| 7.6MB 38kB/s
Requirement already up-to-date: wheel>=0.26 in c:\anaconda3\envs\tensorenv\lib\site-packages (from tensorflow==0.12.0rc0)
Collecting setuptools (from protobuf==3.1.0->tensorflow==0.12.0rc0)
Downloading setuptools-30.2.0-py2.py3-none-any.whl (472kB)
100% |################################| 481kB 396kB/s
Installing collected packages: six, setuptools, protobuf, numpy, tensorflow
Found existing installation: setuptools 27.2.0
TensorFlowのインストールエラー
途中で下記のエラーが発生してインストールが止まります。
Cannot remove entries from nonexistent file c:\anaconda3\envs\tensorenv\lib\site-packages\easy-install.pth
パッケージの依存関係のコンフリクトが原因のようで、下記サイトの「2. pip で setuptools を TensorFlow と互換性があるものに upgrade する」で対応します。※下記サイトは、mac版です。
datalove.hatenadiary.jp
よって、下記コマンドを入力して再インストールします。
pip自体のupgradeが必要かも知れません。先に「pip install --upgrade pip」を実行するといいでしょう。
Permission Errorが発生した場合は、「python -m pip install --upgrade pip」 にする。
(tensorenv) C:\Users\(ユーザー)>pip install --upgrade -I setuptools
Collecting setuptools
Using cached setuptools-30.2.0-py2.py3-none-any.whl
Installing collected packages: setuptools
Successfully installed setuptools-30.2.0
(tensorenv) C:\Users\(ユーザー)>pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc0-cp35-cp35m-win_amd64.whl
Collecting tensorflow==0.12.0rc0 from https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc0-cp35-cp35m-win_amd64.whl
Using cached https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-0.12.0rc0-cp35-cp35m-win_amd64.whl
Collecting numpy>=1.11.0 (from tensorflow==0.12.0rc0)
Using cached numpy-1.11.2-cp35-none-win_amd64.whl
Collecting protobuf==3.1.0 (from tensorflow==0.12.0rc0)
Using cached protobuf-3.1.0-py2.py3-none-any.whl
Requirement already up-to-date: wheel>=0.26 in c:\anaconda3\envs\tensorenv\lib\site-packages (from tensorflow==0.12.0rc0)
Requirement already up-to-date: six>=1.10.0 in c:\anaconda3\envs\tensorenv\lib\site-packages (from tensorflow==0.12.0rc0)
Requirement already up-to-date: setuptools in c:\anaconda3\envs\tensorenv\lib\site-packages (from protobuf==3.1.0->tensorflow==0.12.0rc0)
Installing collected packages: numpy, protobuf, tensorflow
Successfully installed numpy-1.11.2 protobuf-3.1.0 tensorflow-0.12.0rc0
TensorFlowの動作確認
バージョンが表示されればインストール成功です。
(tensorenv) C:\Users\(ユーザー)>python -c "import tensorflow; print(tensorflow.__version__);"
0.12.0-rc0
Jupyter Notebookのインストール
Web上の実行環境である「Jupyter Notebook」はAnacondaと同時にインストールされているのですが、自分はTensorFlow専用の環境(tensorenv)を作成しているため、インストールする必要があるようです。そうしないと、「ImportError: No module named tensoflow」となります。
(tensorenv) C:\Users\(ユーザー)>pip install jupyter
【追記】2017/11/25
新しいバージョンでは専用に環境作成しても、 既にインストール済みとなります。
(tensorenv) C:\Users\(ユーザー)>pip install jupyter
Requirement already satisfied: jupyter in c:\anaconda3\lib\site-packages
Jupyterの起動
カレントフォルダを移動しておきます。
(tensorenv) C:\Users\(ユーザー)>cd C:\Anaconda3
(tensorenv) C:\Anaconda3 >jupyter notebook
Webブラウザが起動しますので、そこからanaconda3/envs/tensorenvフォルダまで移動させます。
更にtensorenvフォルダ配下に移動して、右横の「New」で新規ファイルを「Python 3」で作成します
それでは、下記サイトの演算を実際に実行してみましょう。
qiita.com
では、実際にTensorFlowを使って計算してみます。最初は、以下の式を書いてみます。
import tensorflow as tf
def x2_plus_b(x, b):
_x = tf.constant(x)
_b = tf.constant(b)
result = tf.square(_x)
result = tf.add(result, _b)
return result
print resultではエラーとなったため、print(result)に修正しました。
with tf.Session() as sess:
result = sess.run([x2_plus_b(2., 3.)])
print(result)
実行結果(printの出力)は以下のようになります。
なので、きちんと計算できていることがわかります。
TensorBoardの可視化
TensorFlowには、TensorBoardという強力なビジュアライゼーションツールが付いている点も特徴の一つになっています。
WindowsでのTensorBoardですが、TensorFlowのバージョンが「0.12.0rc0」では動作しないので「0.12.0rc1」にしてください。
※2016/12/18以降なら「0.12.0rc1」になっています。
先ほどの続きとして下記ソースを入力します。
import tensorflow as tf
def monitor_calculation(x, b):
title = "b = {0}".format(b)
c = x2_plus_b(float(x), float(b))
s = tf.scalar_summary(title, c)
m = tf.merge_summary([s])
return m
with tf.Session() as sess:
writer = tf.train.SummaryWriter("log", graph_def=sess.graph_def)
xaxis = range(-10, 12)
for b in range(3):
for x in xaxis:
summary_str = sess.run(monitor_calculation(x, b))
writer.add_summary(summary_str, x)
これを実行すると、ログがフォルダ(C:\Anaconda3/envs/tensorenv/log)に出力されます。
TensorBoardの実行
WindowsでのJupyter notebook上ではTerminalがありません。
その為、「Anaconda Prompt」をもう1つ起動します。
logdirには絶対パスを指定します。
(C:\Anaconda3) C:\Users\(ユーザー)>activate tensorenv
(tensorenv) C:\Users\(ユーザー)>cd c:\Anaconda3
(tensorenv) c:\Anaconda3>tensorboard --logdir=c:\anaconda3\envs/tensorenv/log
Starting TensorBoard b'39' on port 6006
(You can navigate to http://192.168.99.1:6006)
Webブラウザにて、下記URLを入力します。
localhost:6006/
最後に
ようやっとWindowsをサポートされることになりましたが、いろいろやっていくと不足しているところが出てくるかも知れませんね。