TensorFlow Tutorial -MNIST For ML Beginners

What is MNIST

First, I will briefly explain the MNIST that we will use this time. MNIST is the name of an image dataset of handwritten numbers (0-9). The contents of the MNIST dataset used this time are ・ Train: 55,000 sheets ・ Validationm: 5000 sheets ・ Test: 10000 sheets It is composed of a total of 70,000 sheets, and is a set of handwritten number images and labels for correct answers.

train is training, validation is hyperparameter training, test is used to check the accuracy of the created model, and this code does not use validation data.

Image information

One image is a 28 * 28 gray image (= 1 channel) Untitled Diagram (1).png

Creation model

The model created this time is as follows

The layer depth is a simple model with only two layers, an input layer and an output layer. The number of neurons in the input layer is 784 in length x width x number of channels (28 * 28 * 1) when the MNIST image is converted into a one-dimensional array, and the output is 0 to 9, for a total of 10 neurons. The method of converting to this one-dimensional array ignores the relationship and shape of adjacent pixel information, but this time we will use this method. Next time, I would like to make an estimation using the method using CNN.

Source code

mnist_for_ml_beginners


#Import TensorFlow
import tensorflow as tf
#Import of Numpu of library for numerical calculation
import numpy as np
#tensorflow.contrib.learn.python.learn.datasets.mnist.py function read_data_Import sets
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets

#Read MNIST data
mnist = read_data_sets("MNIST_data/", one_hot=True)

#Input variables(The number of data:Nne input data:784 pieces)
x = tf.placeholder("float", [None, 784])

#Variable for weight(Initialize with 0)
W = tf.Variable(tf.zeros([784, 10]))
#Bias variable(Initialize with 0)

b = tf.Variable(tf.zeros([10]))

#output(Perform softmax regression after calculating the inner product of x and W and adding bias)
#By using the softmax function, the total value of the output becomes 1.(=Can be expressed as a probability)
y = tf.nn.softmax(tf.matmul(x, W) + b)

#Variables for correct labels(1 out of 10 values is 1 and the rest are 0)
y_ = tf.placeholder("float", [None, 10])

#Calculate cross entropy error(The larger the value of y corresponding to the correct label, the closer to 0)
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))

#Minimize cross entropy error using gradient descent
#Learning rate is 0.001(Hyperparameters)
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

#Start the model created in the session
sess = tf.Session()
#Initialization
#tf.initialize_all_variables() ->  tf.global_variables_initializer()
sess.run(tf.global_variables_initializer())

#Start learning 1000 times
for i in range(1000):
    #Batch processing(Randomly get 100 from train)
    batch_xs, batch_ys = mnist.train.next_batch(100)
    #feet_In dict x and y_Replace value with placeholder in
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})


#Evaluate the model
#argmax(y,1) :Calculate the index of the largest value in y
#tf.equal(,) :True if the values match, False otherwise
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))

#tf.cast() :Cast bool type to float type(True =1, False =0)
#tf.reduce_mean() :Calculate the average value
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))

#Precision calculation(=Use Mnist Test)
print (sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

The accuracy of the execution result is about 92%

in conclusion

What I learned this time ・ Input x and correct label y_ are placeholders ・ Weight W and bias b are Variable To use.

And if you implement this code, it will also calculate using GPU, so I found that it is a library that you can easily try machine learning, so I would like to continue trying various things using TensorFlow in the future.

The site that I used as a reference

  1. TensorFlow MNIST For ML Beginners Tutorial
  2. TensorFlow: MNIST for ML beginners (code explanation)

Recommended Posts

TensorFlow Tutorial MNIST For ML Beginners
TensorFlow Tutorial -MNIST For ML Beginners
Conducting the TensorFlow MNIST For ML Beginners Tutorial
[Explanation for beginners] TensorFlow tutorial MNIST (for beginners)
TensorFlow MNIST For ML Beginners Translation
[Explanation for beginners] TensorFlow tutorial Deep MNIST
Supplementary notes for TensorFlow MNIST For ML Beginners
[Roughly translate TensorFlow Tutorial into Japanese] 1. MNIST For ML Beginners
I tried the MNIST tutorial for beginners of tensorflow.
TensorFlow MNIST For ML Beginners Translation
TensorFlow Tutorial MNIST For ML Beginners
TensorFlow Tutorial -MNIST For ML Beginners
Supplementary notes for TensorFlow MNIST For ML Beginners
Conducting the TensorFlow MNIST For ML Beginners Tutorial
I tried a TensorFlow tutorial (MNIST for beginners) on Cloud9-Classification of handwritten images-
[Roughly translate TensorFlow Tutorial into Japanese] 2. Deep MNIST For Experts
TensorFlow tutorial tutorial
[Deprecated] Chainer v1.24.0 Tutorial for beginners
TensorFlow Deep MNIST for Experts Translation
I tried running the TensorFlow tutorial with comments (_TensorFlow_2_0_Introduction for beginners)
I tried the TensorFlow tutorial MNIST 3rd
Django tutorial summary for beginners by beginners ③ (View)
Beginners read "Introduction to TensorFlow 2.0 for Experts"
Django tutorial summary for beginners by beginners ⑤ (test)
Roadmap for beginners
[Explanation for beginners] TensorFlow basic syntax and concept
Django tutorial summary for beginners by beginners ⑦ (Customize Admin)
Django tutorial summary for beginners by beginners ⑥ (static file)
Django Tutorial Summary for Beginners by Beginners (Model, Admin)
Django tutorial summary for beginners by beginners ① (project creation ~)
Django tutorial summary for beginners by beginners ④ (Generic View)
Installing TensorFlow on Windows Easy for Python beginners
Code for TensorFlow MNIST Begginer / Expert with Japanese comments
[Translation] NumPy Official Tutorial "NumPy: the absolute basics for beginners"
Spacemacs settings (for beginners)
Enable GPU for tensorflow
Dijkstra algorithm for beginners
OpenCV for Python beginners
[Explanation for beginners] Introduction to convolution processing (explained in TensorFlow)
[Explanation for beginners] Introduction to pooling processing (explained in TensorFlow)
How to learn TensorFlow for liberal arts and Python beginners
[For beginners] I tried using the Tensorflow Object Detection API
Installation notes for TensorFlow for Windows
Learning flow for Python beginners
[For beginners] kaggle exercise (merucari)
Linux distribution recommended for beginners
CNN (1) for image classification (for beginners)
Python3 environment construction (for beginners)
Overview of Docker (for beginners)
Python #function 2 for super beginners
Seaborn basics for beginners ④ pairplot
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Python #list for super beginners
~ Tips for beginners to Python ③ ~
[For Kaggle beginners] Titanic (LightGBM)
Reference resource summary (for beginners)
Try TensorFlow MNIST with RNN
Convenient Linux shortcuts (for beginners)