TensorFlowのチュートリアルを試してみました

概要

TensorFlowは、Googleオープンソースで公開している機械学習環境です。DeepLearning - ディープラーニングの有名ライブラリ5種を最短距離で試す半日コース(TensorFlow, Chainer, Caffe, DeepDream, 画風変換) - Qiitaを参考に、チュートリアルを試してみました。

インストール

TensorFlowのインストールにはvirtualenvを選択するのが安全です。

$ brew python
$ python -V
Python 2.7.10

$ sudo easy_install pip
$ sudo pip install --upgrade virtualenv
$ virtualenv --system-site-packages ~/tensorflow
$ cd ~/tensorflow
(tensorflow)$ pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl

正しくインストールされているかどうか確認します。

(tensorflow)bash-3.2$ python
Python 2.7.10 (default, Oct 23 2015, 18:05:06)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello TensorFlow!')
>>> sess = tf.Session()
>>> print sess.run(hello) Hello TensorFlow!

virtualenvではなくpipインストールにを選択した場合、失敗する可能性があります。その場合には、--ignore-installed sixを付けるとうまくいくかもしれません。

Six issue when installing package #3165

失敗した場合

Installing collected packages: setuptools, protobuf, wheel, numpy, tensorflow
  Found existing installation: setuptools 1.1.6
    Uninstalling setuptools-1.1.6:
Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip-7.1.2-py2.7.egg/pip/basecommand.py", line 211, in main
    status = self.run(options, args)

成功した場合

$ sudo pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.6.0-py2-none-any.whl --ignore-installed six
Collecting setuptools (from protobuf==3.0.0a3->tensorflow==0.6.0)
  Downloading setuptools-19.2-py2.py3-none-any.whl (463kB)
    100% |████████████████████████████████| 466kB 1.1MB/s
Installing collected packages: six, setuptools, protobuf, wheel, numpy, tensorflow
  Running setup.py install for protobuf
Successfully installed numpy-1.8.0rc1 protobuf-3.0.0a3 setuptools-1.1.6 six-1.10.0 tensorflow-0.6.0 wheel-0.26.0

Tutorial

DeepLearning - ディープラーニングの有名ライブラリ5種を最短距離で試す半日コース(TensorFlow, Chainer, Caffe, DeepDream, 画風変換) - Qiitaに書いている手順を実行してみます。

$ cd ~/tensorflow
$ git clone --recurse-submodules https://github.com/tensorflow/tensorflow
$ vi tensorflow/tensorflow/examples/tutorials/mnist/fully_connected_feed.py
30,31行目
#from tensorflow.examples.tutorials.mnist import input_data
#from tensorflow.examples.tutorials.mnist import mnist
import input_data
import mnist
$ python tensorflow/tensorflow/examples/tutorials/mnist/fully_connected_feed.py
Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting data/t10k-labels-idx1-ubyte.gz
can't determine number of CPU cores: assuming 4
I tensorflow/core/common_runtime/local_device.cc:25] Local device intra op parallelism threads: 4
can't determine number of CPU cores: assuming 4
I tensorflow/core/common_runtime/local_session.cc:45] Local session inter op parallelism threads: 4
Step 0: loss = 2.32 (0.036 sec)
Step 100: loss = 2.19 (0.005 sec)
Step 200: loss = 1.98 (0.005 sec)
(略)
Step 700: loss = 0.66 (0.005 sec)
Step 800: loss = 0.71 (0.006 sec)
Step 900: loss = 0.70 (0.006 sec)
Training Data Eval:
  Num examples: 55000  Num correct: 47040  Precision @ 1: 0.8553
Validation Data Eval:
  Num examples: 5000  Num correct: 4303  Precision @ 1: 0.8606
Test Data Eval:
  Num examples: 10000  Num correct: 8653  Precision @ 1: 0.8653
Step 1000: loss = 0.54 (0.016 sec)
Step 1100: loss = 0.66 (0.122 sec)
(略)
Step 1800: loss = 0.43 (0.012 sec)
Step 1900: loss = 0.46 (0.005 sec)
Training Data Eval:
  Num examples: 55000  Num correct: 49235  Precision @ 1: 0.8952
Validation Data Eval:
  Num examples: 5000  Num correct: 4506  Precision @ 1: 0.9012
Test Data Eval:
  Num examples: 10000  Num correct: 9011  Precision @ 1: 0.9011

TensorBoard

計算結果のログを可視化できます。

(tensorflow)$ tensorboard --logdir=/Users/unokun/tensorflow/data

モデル

f:id:unokun3:20160113080122p:plain

平均エントロピー

f:id:unokun3:20160113080108p:plain

今後

Pocket: TensorFlowによるディープラーニングで、アイドルの顔を識別するのような顔認識をしてみたい。

関連情報