最近機械学習にちょっと興味持ち始めて、
世の中でどう使われてるかー、とか5, 6冊本読んでみましたが、
やっぱ使ってみないと実感わかないので、まあとりあえず使ってみよう、と思いました。
今回はTensorflowを使ってます。
Googleが公開してる、pythonの機械学習プログラミング用のライブラリですね。
Tensorflowの利用はMac OSかUbuntu、というちょっと変わった制約がある。
普段Windows ServerとかRedhat Enterprise Linux派な私としてはちょっとやりづらいけど、
まあRHELと大して変わらんだろう、と高をくくってやり始める(フラグではない)。
条件
Amazon EC2インスタンスでの構築。 Ubuntu 14.04.3 LTS (GNU/Linux 3.13.0-74-generic x86_64)
インストール
まずはインストール。
インストールに使うpipコマンドから導入。
# apt-get install python-pip
ログの垂れ流しをみてると、、、あれ?
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
しかたねえ
# apt-get update # apt-get install python-pip
上手くいった。
ここで本でなくマニュアルみたら、
https://www.tensorflow.org/versions/r0.10/get_started/os_setup.html#pip-installation
$ sudo apt-get install python-pip python-dev
と書いてあったので、python-devもインストール。
# apt-get install python-pip python-dev
pythonのバージョンは
# python --version Python 2.7.6
で2.7だったので、
マニュアルから下記コマンドを選択。
# export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.10.0rc0-cp27-none-linux_x86_64.whl # pip install --upgrade $TF_BINARY_URL
インストール完了。
動作確認
マニュアルに沿って動作確認。
まずはソフトマックスで動確。
CNN(畳み込みニューラルネットワーク)とかは理論についていけてないので、まずはこれから。
こんなファイルを作ってみる(ファイル名:softmax_1000try_py)。
2016/9/5 追記:
このスクリプトは、TensorFlowのチュートリアルページから引っ張ってきたものです。
スクリプトの解説らしきものをこちらのエントリに書いたので、
気になる方はどうぞ。
from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) import tensorflow as tf x = tf.placeholder(tf.float32, [None, 784]) W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) y = tf.nn.softmax(tf.matmul(x, W) + b) y_ = tf.placeholder(tf.float32, [None, 10]) cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) init = tf.initialize_all_variables() sess = tf.Session() sess.run(init) for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
実行
# python softmax_1000try.py Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes. Extracting MNIST_data/train-images-idx3-ubyte.gz Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes. Extracting MNIST_data/train-labels-idx1-ubyte.gz Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes. Extracting MNIST_data/t10k-images-idx3-ubyte.gz Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes. Extracting MNIST_data/t10k-labels-idx1-ubyte.gz 0.9194
91.9%の精度で判定できてる、ということですね。
マニュアルにも92%程度の精度になるはず、とあるし、
動作確認に問題はなし。
こんどはコレ使ってCNN試してみるか・・・。
理論が追いついてないからちゃんと勉強しないとな、、、
では。