I tried TensorFlow Official Tutorial
mnist_softmax A program that estimates handwritten characters 60000 training data 10000 test data Input is image data 784 (28 * 28)
Input X rows 784 columns 1 Weight W row Training data number Column 784 Bias b row 10 columns 1 y = softmax (W · x + b)
Matrix multiplication matmul()
To express the establishment Classification result is obtained as a function of input data Activation function with the sum of all outputs of 1 y1, y2 ... y10 (representing the probability of numbers 0-9, respectively)
The correct answer is represented by 0 and 1
Find the weight (w) and bias (b) so that the label data and the softmax output are as close as possible.
As a cost function (error) Calculate the difference between the estimated value and the correct label
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf
Prepare an empty container with placeholder
Variable
variable
x = tf.placeholder(tf.float32, [None,784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
The output is 10 elements, which is 1 in total.
y = tf.nn.softmax(tf.matmul(x,W) + b)
Calculate how far the difference between y
and y_
is in cross_entropy
reduce_mean
(mean)
reduce sum
(sum)
y_ = tf.placeholder(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
Optimize and update weights
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
Train with random data for 100 batches. Repeat 1000 times.
Extract 100 data in the train.
sess.run(train_step, feed_dict={x: batch_xs, y_:batch_ys})
train_step
Specified by SGD (gradient descent method)
feed_dict = {x: batch_xs, y_: batch_ys}
Source of data, x is batch x, y is batch 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 Compares the output with the correct label and returns True and False. ʻArgmax (y, 1)
Extract the largest element of the output.
reduce_mean
Mean accuracy
cast
True, convert False to a number (percentage)
Finally evaluate with test data
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}))
Finally, a summary of the flow so far is described.
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}))
Recommended Posts