[PYTHON] I tried the TensorFlow tutorial MNIST 3rd

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

softmax function

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.

Cross entropy

As a cost function (error) Calculate the difference between the estimated value and the correct label

Program flow

Read MNISR data

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

Import tensorflow

import tensorflow as tf

Input, weight and bias settings

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

Output (softmax)

The output is 10 elements, which is 1 in total.

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

Setting of correct label (teacher data)

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

training

Optimize and update weights

train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

Data initialization

sess = tf.InteractiveSession()

tf.global_variables_initializer().run()

Training in batch

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

Evaluation

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

program

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

I tried the TensorFlow tutorial MNIST 3rd
I tried the TensorFlow tutorial 1st
I tried the TensorFlow tutorial 2nd
I tried the MNIST tutorial for beginners of tensorflow.
I tried TensorFlow tutorial CNN 4th
I tried tensorflow for the first time
I tried running the TensorFlow tutorial with comments (_TensorFlow_2_0_Introduction for beginners)
I tried running TensorFlow
Conducting the TensorFlow MNIST For ML Beginners Tutorial
I tried a TensorFlow tutorial (MNIST for beginners) on Cloud9-Classification of handwritten images-
I tried the changefinder library!
I tried running the TensorFlow tutorial with comments (text classification of movie reviews)
63rd day I installed tensorflow.
[MNIST] I tried Fine Tuning using the ImageNet model.
I tried using magenta / TensorFlow
I tried porting the code written for TensorFlow to Theano
I tried to find the average of the sequence with TensorFlow
I tried refactoring the CNN model of TensorFlow using TF-Slim
[For beginners] I tried using the Tensorflow Object Detection API
[Explanation for beginners] TensorFlow tutorial MNIST (for beginners)
I tried the Naro novel API 2
TensorFlow Tutorial MNIST For ML Beginners
I tried the Naruro novel API
I tried to move the ball
I tried using the checkio API
TensorFlow Tutorial -MNIST For ML Beginners
I tried to estimate the interval.
TensorFlow tutorial tutorial
[Explanation for beginners] TensorFlow tutorial Deep MNIST
I tried the asynchronous server of Django 3.0
I tried to implement Autoencoder with TensorFlow
I tried to summarize the umask command
I tried to visualize AutoEncoder with TensorFlow
I tried to recognize the wake word
I tried playing a ○ ✕ game using TensorFlow
I tried the OSS visualization tool, superset
I tried to classify text using TensorFlow
I tried to summarize the graphical modeling.
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
Python: I tried the traveling salesman problem
I tried playing with the image with Pillow
I tried the Python Tornado Testing Framework
I tried using the BigQuery Storage API
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I tried "smoothing" the image with Python + OpenCV
I tried web scraping to analyze the lyrics.
I tried using scrapy for the first time
I tried the pivot table function of pandas
[Python] I tried substituting the function name for the function name
I tried cluster analysis of the weather map
I tried hitting the Qiita API from go
vprof --I tried using the profiler for Python
I tried to move GAN (mnist) with keras
I tried "differentiating" the image with Python + OpenCV
I tried scraping
I tried to optimize while drying the laundry
I tried to save the data with discord
I tried PyQ
I tried simulating the "birthday paradox" in Python
I tried the least squares method in Python