LoginSignup
2
2

More than 5 years have passed since last update.

TensorFlowチュートリアルやってみた MNIST 第3回

Last updated at Posted at 2017-08-20

TensorFlowの公式チュートリアルをやってみました

mnist_softmax

手書きの文字を推定するプログラム
60000件の訓練データ
10000件のテストデータ
入力は画像データ784(28*28)

入力X 行784 列1
重みW 行訓練データ数 列784
バイアスb 行10 列1
y = softmax(W・x + b)

行列の掛け算(matrix multipilication)
matmul()

softmax関数

確立を表現するため
分類結果が入力データの関数として求められる
すべての出力の総和が1の活性化関数
y1,y2...y10(それぞれ数字の0~9の確立を表す)

正解は0と1で表される

ラベルデータとsoftmaxの出力をできるだけ近くなるように重み(w)とバイアス(b)を求める

クロスエントロピー

コスト関数(誤差)として
推定値と正解ラベルのズレを計算する

プログラムの流れ

MNISRデータの読み込み

from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

tensorflowのインポート

import tensorflow as tf

入力と重みとバイアスの設定

placeholderで空の入れ物用意
Variable変数

x = tf.placeholder(tf.float32, [None,784])

W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

出力(softmax)

出力は10個の要素、総和すると1となる。

y = tf.nn.softmax(tf.matmul(x,W) + b)

正解ラベル(教師データ)の設定

cross_entropy(交差エントロピー)でyy_の差をどれくらい離れているかを計算する
reduce_mean(平均)
reduce sum(総和)

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)

データの初期化

sess = tf.InteractiveSession()

tf.global_variables_initializer().run()

バッチで訓練

バッチ100個のランダムなデータで訓練する。1000回繰り返す。
trainの中にある100個データを取り出す。
sess.run(train_step, feed_dict={x: batch_xs, y_:batch_ys})
train_step SGD(勾配降下法)で指定している
feed_dict={x: batch_xs, y_:batch_ys} データを与える元、xはバッチのx、yはバッチのy

for _ in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step, feed_dict={x: batch_xs, y_:batch_ys})

評価

equal出力と正解ラベルを比較しTrue、Falseを返す。
argmax(y,1)出力の最大の要素を取り出す。
reduce_mean精度の平均値
castTrue、Falseを数値(割合)に変換
最後にテストデータで評価する

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}))

プログラム

最後にこれまでの流れをまとめたもの記述する。

mnist_softmax.py
# coding: utf-8
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)

sess = tf.InteractiveSession()

tf.global_variables_initializer().run()

for _ 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}))
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2