Created
May 12, 2016 02:31
-
-
Save uramonk/e9625de0711f29cd0a4b57502648dd86 to your computer and use it in GitHub Desktop.
TensorFlow MNIST For ML Beginners チュートリアルのコード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# TensowFlowのインポート | |
import tensorflow as tf | |
# MNISTを読み込むためinput_data.pyを同じディレクトリに置きインポートする | |
# input_data.pyはチュートリアル内にリンクがあるのでそこから取得する | |
# https://tensorflow.googlesource.com/tensorflow/+/master/tensorflow/examples/tutorials/mnist/input_data.py | |
import input_data | |
import time | |
# 開始時刻 | |
start_time = time.time() | |
print "開始時刻: " + str(start_time) | |
# MNISTデータの読み込み | |
# 60000点の訓練データ(mnist.train)と10000点のテストデータ(mnist.test)がある | |
# 訓練データとテストデータにはそれぞれ0-9の画像とそれに対応するラベル(0-9)がある | |
# 画像は28x28px(=784)のサイズ | |
# mnist.train.imagesは[60000, 784]の配列であり、mnist.train.lablesは[60000, 10]の配列 | |
# lablesの配列は、対応するimagesの画像が3の数字であるならば、[0,0,0,1,0,0,0,0,0,0]となっている | |
# mnist.test.imagesは[10000, 784]の配列であり、mnist.test.lablesは[10000, 10]の配列 | |
print "--- MNISTデータの読み込み開始 ---" | |
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) | |
print "--- MNISTデータの読み込み完了 ---" | |
# 訓練画像を入れる変数 | |
# 訓練画像は28x28pxであり、これらを1行784列のベクトルに並び替え格納する | |
# Noneとなっているのは訓練画像がいくつでも入れられるようにするため | |
x = tf.placeholder(tf.float32, [None, 784]) | |
# 重み | |
# 訓練画像のpx数の行、ラベル(0-9の数字の個数)数の列の行列 | |
# 初期値として0を入れておく | |
W = tf.Variable(tf.zeros([784, 10])) | |
# バイアス | |
# ラベル数の列の行列 | |
# 初期値として0を入れておく | |
b = tf.Variable(tf.zeros([10])) | |
# ソフトマックス回帰を実行 | |
# yは入力x(画像)に対しそれがある数字である確率の分布 | |
# matmul関数で行列xとWの掛け算を行った後、bを加算する。 | |
# yは[1, 10]の行列 | |
y = tf.nn.softmax(tf.matmul(x, W) + b) | |
# 交差エントロピー | |
# y_は正解データのラベル | |
y_ = tf.placeholder(tf.float32, [None, 10]) | |
cross_entropy = -tf.reduce_sum(y_*tf.log(y)) | |
# 勾配硬化法を用い交差エントロピーが最小となるようyを最適化する | |
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) | |
# 用意した変数Veriableの初期化を実行する | |
init = tf.initialize_all_variables() | |
# Sessionを開始する | |
# runすることで初めて実行開始される(run(init)しないとinitが実行されない) | |
sess = tf.Session() | |
sess.run(init) | |
# 1000回の訓練(train_step)を実行する | |
# next_batch(100)で100つのランダムな訓練セット(画像と対応するラベル)を選択する | |
# 訓練データは60000点あるので全て使いたいところだが費用つまり時間がかかるのでランダムな100つを使う | |
# 100つでも同じような結果を得ることができる | |
# feed_dictでplaceholderに値を入力することができる | |
print "--- 訓練開始 ---" | |
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}) | |
print "--- 訓練終了 ---" | |
# 正しいかの予測 | |
# 計算された画像がどの数字であるかの予測yと正解ラベルy_を比較する | |
# 同じ値であればTrueが返される | |
# argmaxは配列の中で一番値の大きい箇所のindexが返される | |
# 一番値が大きいindexということは、それがその数字である確率が一番大きいということ | |
# Trueが返ってくるということは訓練した結果と回答が同じということ | |
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) | |
# 精度の計算 | |
# correct_predictionはbooleanなのでfloatにキャストし、平均値を計算する | |
# Trueならば1、Falseならば0に変換される | |
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) | |
# 精度の実行と表示 | |
# テストデータの画像とラベルで精度を確認する | |
# ソフトマックス回帰によってWとbの値が計算されているので、xを入力することでyが計算できる | |
print "精度" | |
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})) | |
# 終了時刻 | |
end_time = time.time() | |
print "終了時刻: " + str(end_time) | |
print "かかった時間: " + str(end_time - start_time) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment