/home/by-natures/dev*

データ界隈で働くエンジニアとしての技術的なメモと、たまに普通の日記。

Google の機械学習ライブラリ TensorFlow メモ

2016年に知り合いの方が TensorFlow の勉強会を開くということで、運営のサポートをすることになりました。設営だけでなく、Python などの言語的なサポートも行うことになったため、この年末年始に勉強しています。

TensorFlow(Google の公式ページ) とは Google機械学習オープンソースライブラリで、多次元ベクトル(=テンソル)をデータフローによって制御する仕組みから、TensorFlow と名付けたようです。

チュートリアルの時点から数式やデータフローの図が多く出て来るため、このコマンドを打てば何か出て来るといったライブラリではなく、有識者機械学習業務・研究をサポートするツールであるようです。Google がなぜ TensorFlow をオープンソース化したかについて、

Research in this area is global and growing fast, but lacks standard tools. By sharing what we believe to be one of the best machine learning toolboxes in the world, we hope to create an open standard for exchanging research ideas and putting machine learning in products. -- Google の公式ページ より

と書かれています。"for exchanging research ideas and putting machine learning in products." という文で明確に research という言葉が出て来ており、機械学習に詳しい人にとってのライブラリというのが主な位置づけのような気がします。

まだインストールして動作確認しただけですが、この後はチュートリアルをもう少し進めてみたり、実際のデータで使ってみようと思います(余談ですが、最近ブログを書いていなかったので、簡単なエントリーからでも書く癖を戻したいなと思っています…)。しかし色々な会社や団体から機械学習ライブラリが登場してきて、どれが多勢になるのか来年は分かれ目かもしれませんね。

インストール

インストールは Python 2.7 もしくは 3.3 以上であれば、pip から一発で入ります(詳しいインストール手順は "Download and Setup" をどうぞ)

僕の場合は macbrew で Python3.4 を入れ、その後 pip でインストールしました:

sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.6.0-py3-none-any.whl
動作確認1:ライブラリが使えるかどうか

ここはすんなり動きました。

$ python3
Python 3.4.1 (default, Dec 29 2015, 14:51:03) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4
>>> print(sess.run(hello))
b'Hello, TensorFlow!'
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
動作確認2:MNIST のデータの学習

まだよく分かりませんが、動いているようです。結構時間が掛かります。

$ python3 /usr/local/lib/python3.4/site-packages/tensorflow/models/image/mnist/convolutional.py
Succesfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Succesfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Succesfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Succesfully 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
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4
Initialized!
Epoch 0.00
Minibatch loss: 12.054, learning rate: 0.010000
Minibatch error: 90.6%
Validation error: 84.6%
Epoch 0.12
...