[PYTHON] [Explanation for beginners] TensorFlow tutorial MNIST (for beginners)

TensorFlow beginner tutorial

I ran the TensorFlow Beginners Tutorial MNIST For ML Beginners. I'm a liberal arts graduate and ** TensorFlow for the first time **, and on the contrary ** Deep Learning for the first time **. Furthermore, ** I have hardly done machine learning **. For mathematical knowledge, please refer to the article How to study mathematics for working adults to understand statistics and machine learning. It is explained from the perspective of beginners. There are many parts that lack accuracy, but I give priority to clarity.

Reference link

-Installing TensorFlow on Windows was easy even for Python beginners -[Explanation for beginners] TensorFlow basic syntax and concept -TensorFlow API memo -[Introduction to TensorBoard] Visualize TensorFlow processing to deepen understanding -Visualize TensorFlow tutorial MNIST (for beginners) with TensorBoard -[Introduction to TensorBoard: image] TensorFlow Visualize image processing to deepen understanding -[Introduction to TensorBoard: Projector] Make TensorFlow processing look cool -[Explanation for beginners] TensorFlow Tutorial Deep MNIST -Yuki Kashiwagi's facial features to understand TensorFlow [Part 1]

Overview of MNIST For ML Beginners

The outline diagram is as follows. 00.Overview.JPG

1. Learning from images and correct answer data

MNIST database (Modified National Institute of Standards and Technology database) is a set of handwritten numerical images and correct data indicating which numerical values each image indicates. there is. We will read those data and learn what kind of image should be returned and what kind of numerical value it should be returned.

2. Model generation

As a result of "1. Learning from images and correct answer data", create a model formula. Internally, we will create a model with this image (0 is taken as an example). 00.Overview02.JPG

3. Evaluation of learning results

Calculate how much the correct answer rate will be obtained by the generated model.

MNIST For ML Beginners execution procedure

Execution environment

The execution environment is as shown in the figure below. I'm running from a Jupyter Notebook. TensorFlowEnvironment.JPG

1. Start Jupyter Notebook

Anaconda Navigator is launched from the Windows menu. Then launch Jupyter Notebook from Anaconda Navigator. 10.LaunchJupyterNotebook01.JPG

10.LaunchJupyterNotebook02.JPG

Rename the file in Jupyter Notebook. 10.LaunchJupyterNotebook03.JPG

10.LaunchJupyterNotebook04.JPG

2. Tutorial execution

All you have to do is run the tutorial. Shift + Enter key to execute the command (Enter key alone is just a line break). 20.RunMNIST01.JPG 20.RunMNIST02.JPG The last "0.9178" is the evaluation of the learning result, which means that the correct answer rate is about 92%. 3.Computational Graph When you output Computational Graph (calculation formula graph) with TensorBoard, it looks like this. For details, refer to the article "Visualization of TensorFlow Tutorial MNIST (for beginners) with TensorBoard". 40.TensorBoard01.JPG

Explanation of each command

0. Overview

MNIST interprets handwritten numbers from 0 to 9 as 28-dot square data. MNIST However, that would be complicated to explain in this article, so we will replace the two types of handwritten data, vertical bar (|) and horizontal bar (-), with 5-dot square data. Then, it looks like the figure below. 30.Modeling01.JPG

1. MNIST data import

Importing MINST data. By setting "one_hot = True", it means that if the data already exists locally, it will not be imported.

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

2. Load TensorFlow library

import tensorflow as tf

3. Model definition

Define three variables and a regression model. x: Variable to put an image of handwritten numerical value W: Variable to put the weighting value b: Variable to put the bias value

Explaining the above with a 5-dot square vertical and horizontal bar model, it looks like this (the bias value of b is an adjustment variable and is not included in the figure). The model is defined so that it can be evaluated by the value of $ x * W + b $. Think of "tf.nn.softmax" (softmax function) as a numerical conversion for evaluation. 30.Modeling02.JPG

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)

4. Definition of correct variable

Here, define the storage variable of the correct answer data (image xx is the numerical value xx).

y_ = tf.placeholder(tf.float32, [None, 10])

5. Definition of cross entropy

Definition of cross entropy (cross entropy). Cross entropy is the difference between the predicted and actual values.

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

6. Definition of training method

Train using the "tf.train.Gradient Descent Optimizer".

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

7. Session generation and variable initialization

sess = tf.InteractiveSession()
tf.global_variables_initializer().run()

8. Training execution

Repeat the training 1000 times using 100 random data.

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

9. Evaluation

Here, the predicted value and the correct answer are matched. correct_prediction holds data as True / False. tf.argmax is to extract the largest value in the data (correct numerical value). Use tf.argmax to output the number with the highest probability (evaluation value y).

correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))

10. Evaluation calculation and output

The correct answer rate is calculated by changing the True / False data to 1 or 0 with tf.cast.

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

[Explanation for beginners] TensorFlow tutorial MNIST (for beginners)
[Explanation for beginners] TensorFlow tutorial Deep MNIST
TensorFlow Tutorial MNIST For ML Beginners
TensorFlow Tutorial -MNIST For ML Beginners
Conducting the TensorFlow MNIST For ML Beginners Tutorial
TensorFlow MNIST For ML Beginners Translation
I tried the MNIST tutorial for beginners of tensorflow.
[Roughly translate TensorFlow Tutorial into Japanese] 1. MNIST For ML Beginners
Supplementary notes for TensorFlow MNIST For ML Beginners
[Explanation for beginners] TensorFlow basic syntax and concept
I tried a TensorFlow tutorial (MNIST for beginners) on Cloud9-Classification of handwritten images-
[Explanation for beginners] Introduction to convolution processing (explained in TensorFlow)
[Explanation for beginners] Introduction to pooling processing (explained in TensorFlow)
[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)
Implementation and explanation using XGBoost for beginners
Beginners read "Introduction to TensorFlow 2.0 for Experts"
Django tutorial summary for beginners by beginners ⑤ (test)
Roadmap for beginners
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)
python textbook for beginners
MNIST image generation program creation by DCGAN (tensorflow tutorial)
Enable GPU for tensorflow
Dijkstra algorithm for beginners
OpenCV for Python beginners
[For beginners] Summary of standard input in Python (with explanation)
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
Basic Python grammar for beginners
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)
Linux command memorandum [for beginners]
Try TensorFlow MNIST with RNN
Convenient Linux shortcuts (for beginners)
[Explanation for beginners] OpenCV face detection mechanism and practice (detect MultiScale)