TensorFlowのチュートリアルで苦戦してみた

今更感もあるけれど、TensorFlowのチュートリアルを行ってみた。

こちらから。
https://www.tensorflow.org/versions/master/tutorials/mnist/beginners/index.html

まずは、必要なものをインストール

$ python -V
Python 2.7.10

Pythonのバージョンは大丈夫そうなので、必要なモジュールをいれちゃう

$ sudo easy_install pip
$ sudo easy_install --upgrade six

# 仮想の環境変数が使える環境を構築する
# これをしないとMacでTensorFlowを簡単にインストールできない!
$ sudo pip install --upgrade virtualenv
The directory '/Users/aokayama/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 sudo's -H flag.
The directory '/Users/aokayama/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting virtualenv
  Downloading virtualenv-15.0.2-py2.py3-none-any.whl (1.8MB)
    100%  1.8MB 383kB/s 
Installing collected packages: virtualenv
Successfully installed virtualenv-15.0.2

Macではデフォルトの設定が邪魔していてうまく動かないので、
今回のTensorFlowを実行するための環境設定を作成してそこで遊ぶようにする

# 新しいenvを作成
$ virtualenv --system-site-packages ~/tensorflow
New python executable in /Users/aokayama/tensorflow/bin/python
Installing setuptools, pip, wheel...done.

# 入り込む
$ source ~/tensorflow/bin/activate
(tensorflow) $ pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl
Collecting tensorflow==0.8.0 from https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl
  Downloading https://storage.googleapis.com/tensorflow/mac/tensorflow-0.8.0-py2-none-any.whl (19.3MB)
    100% 19.3MB 51kB/s 
Requirement already up-to-date: six>=1.10.0 in /Library/Python/2.7/site-packages/six-1.10.0-py2.7.egg (from tensorflow==0.8.0)
Collecting protobuf==3.0.0b2 (from tensorflow==0.8.0)
  Downloading protobuf-3.0.0b2-py2.py3-none-any.whl (326kB)
    100%  327kB 2.1MB/s 
Requirement already up-to-date: wheel in ./tensorflow/lib/python2.7/site-packages (from tensorflow==0.8.0)
Collecting numpy>=1.10.1 (from tensorflow==0.8.0)
  Downloading numpy-1.11.0-cp27-cp27m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (3.9MB)
    100%  3.9MB 248kB/s 
Requirement already up-to-date: setuptools in ./tensorflow/lib/python2.7/site-packages (from protobuf==3.0.0b2->tensorflow==0.8.0)
Installing collected packages: protobuf, numpy, tensorflow
  Found existing installation: numpy 1.8.0rc1
    Not uninstalling numpy at /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python, outside environment /Users/aokayama/tensorflow
Successfully installed numpy-1.11.0 protobuf-3.0.0b2 tensorflow-0.8.0


# 環境を抜けるときは
(tensorflow)$ deactivate

提供されている教師データをダウンロード

$ wget https://raw.githubusercontent.com/tensorflow/tensorflow/r0.9/tensorflow/examples/tutorials/mnist/input_data.py

後はコードを書くだけ。
Hello Worldを写経

import tensorflow as tf

hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print sess.run(hello)
$ python helloworld.py 
Hello, TensorFlow!

よーしよしよし

肝心のチュートリアルを写経
softmax法

import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

import tensorflow as tf
sess = tf.InteractiveSession()

# create the model
x = tf.placeholder("float", [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y= tf.nn.softmax(tf.matmul(x, W) + b)

# define loss and optimizer
y_ = tf.placeholder("float", [None, 10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

# train
tf.initialize_all_variables().run()
for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    train_step.run({x: batch_xs, y_:batch_ys})

# test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuray = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(accuray.eval({x: mnist.test.images, y_: mnist.test.labels}))
Extracting MNIST_data/train-images-idx3-ubyte.gz
Extracting MNIST_data/train-labels-idx1-ubyte.gz
Extracting MNIST_data/t10k-images-idx3-ubyte.gz
Extracting MNIST_data/t10k-labels-idx1-ubyte.gz
0.9158

なんか出た!!正解率(精度)が出た!!

こんな簡単にできちゃった!!!!すごいよ!TensorFlow!!
とはならず、何をやっているのかが全くわからない。
結局、数学(softmax法)の知識がないと何しているのかがわからない。

全然わからないのだけれど、とりあえずTensorFlowが動く環境ができたということで。
TensorFlowで会話AIを作ってみた」というのに感動をしたので、こちらを試してみたいなぁと思っている。