[PYTHON] I tried running the TensorFlow tutorial with comments (_TensorFlow_2_0_Introduction for beginners)

Reference URL: https://www.tensorflow.org/tutorials/quickstart/beginner?hl=ja

Target

Do the following --Build a neural network to classify images --Training neural networks --Evaluate the performance of the model

Preparation of package

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#Ver confirmation of tensorflow
print(tf.__version__)
2.3.0

Prepare the dataset

This time I will use MNIST

Other datasets: https://www.tensorflow.org/api_docs/python/tf/keras/datasets?hl=JA

mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

#Convert sample from integer to floating point number (convert 0 to 255 to range 0 to 1)
x_train, x_test = x_train / 255.0, x_test / 255.0
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
#Check the shape of the data
print(x_train.shape)
print(x_test.shape)
(60000, 28, 28)
(10000, 28, 28)

--Training data: 60,000 sheets --Test data: 10000 sheets

Model building

Process flow

  1. 28 ✖️ 28 2D data smoothed to 1D

    tf.keras.layers.Flatten

input_shape = (28, 28) specifies the shape of the input data

  1. Definition of hidden layer

    tf.keras.layers.Dense

128 is the number of units (number of neurons) activation ='relu' specifies the activation function ReLU Other activation functions: https://www.tensorflow.org/api_docs/python/tf/keras/activations?hl=ja

  1. Definition of dropout

Randomly disable some neurons to prevent overfitting 0.2 disables 20%

  1. Definition of fully connected layer

Specify 10 because it will be finally classified into 10 classes.

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10)
])

Pre-learning predictions

Calculate scores called "logit" or "log odds ratio" for each class

Visualize predictive data

Is it the number 5? ??

plt.figure()
plt.imshow(x_train[0] * 255, cmap="gray")
plt.grid(False)
plt.show()

output_14_0.png

Predicted by pre-learning model

predictions = model(x_train[:1]).numpy()
predictions
array([[-0.68039566,  0.24756509,  0.03884459,  0.13278663, -0.09757528,
        -0.41739488, -0.07566899, -0.00817996,  0.17783645,  0.13316259]],
      dtype=float32)

Shape of data to be input at the time of forecast

Note that the shape does not match with x_train [0] The model is designed to make predictions about batches or "gatherings" in a sample.

print(x_train[0].shape)
print(x_train[:1].shape)
(28, 28)
(1, 28, 28)

Convert score to probability

predictions_probability = tf.nn.softmax(predictions).numpy()
print(predictions_probability)
#Get index of maximum element from predicted probability
np.argmax(predictions_probability)
[[0.05172387 0.13082756 0.10618253 0.1166411  0.09264173 0.06728384
  0.09469356 0.10130493 0.12201592 0.11668495]]





1

Definition of loss function

Loss function

Adopts cross entropy

Other loss functions: https://www.tensorflow.org/api_docs/python/tf/keras/losses?hl=ja

loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
loss_fn(y_train[:1], predictions).numpy()
2.6988351

Compiling the model

Defining a model for learning

--optimer: Optimizer algorithm --This time, specify ʻAdam --Other optimization algorithms: https://www.tensorflow.org/api_docs/python/tf/keras/optimizers --loss: loss function --This time, specifycross entropy --metrics: Items quantified during learning and testing --This time, specify ʻaccuracy

model.compile(optimizer='adam',
              loss=loss_fn,
              metrics=['accuracy'])

Model learning

Learn with an epoch of 5 Epoch: How many times one training data is repeatedly trained "

model.fit(x_train, y_train, epochs=5)
Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2981 - accuracy: 0.9127
Epoch 2/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.1435 - accuracy: 0.9572
Epoch 3/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.1079 - accuracy: 0.9671
Epoch 4/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0875 - accuracy: 0.9728
Epoch 5/5
1875/1875 [==============================] - 3s 2ms/step - loss: 0.0750 - accuracy: 0.9768





<tensorflow.python.keras.callbacks.History at 0x7f12f819d5c0>

Model performance evaluation

Use test data to calculate model loss and accuracy verbose is an option to show the progress bar This model shows a 97% accuracy rate in the test data

model.evaluate(x_test,  y_test, verbose=1)
313/313 [==============================] - 0s 1ms/step - loss: 0.0742 - accuracy: 0.9772





[0.07416515052318573, 0.9771999716758728]

Change model output

When outputting probabilities instead of numbers

probability_model = tf.keras.Sequential([
  model,
  tf.keras.layers.Softmax()
])
predictions = probability_model(x_test[:5])

#Visualize and compare predictions and correct answers
for index, prediction in enumerate(predictions):
    print(f'Prediction:{np.argmax(prediction)}Correct answer:{y_test[index]}')
    ax = plt.subplot(3, 3, index + 1)
    plt.imshow(x_test[index] * 255, cmap="gray")
    plt.show()

Prediction: 7 Correct answer: 7

output_33_1.png

Prediction: 2 Correct answer: 2

output_33_3.png

Prediction: 1 Correct answer: 1

output_33_5.png

Prediction: 0 Correct answer: 0

output_33_7.png

Prediction: 4 Correct answer: 4

output_33_9.png

Recommended Posts

I tried running the TensorFlow tutorial with comments (_TensorFlow_2_0_Introduction for beginners)
I tried the MNIST tutorial for beginners of tensorflow.
I tried the TensorFlow tutorial 1st
I tried the TensorFlow tutorial 2nd
[For beginners] I tried using the Tensorflow Object Detection API
I tried the TensorFlow tutorial MNIST 3rd
I tried tensorflow for the first time
I tried running TensorFlow
Conducting the TensorFlow MNIST For ML Beginners Tutorial
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
I tried a TensorFlow tutorial (MNIST for beginners) on Cloud9-Classification of handwritten images-
I ran the TensorFlow tutorial with comments (first neural network: the beginning of the classification problem)
I tried porting the code written for TensorFlow to Theano
[Explanation for beginners] TensorFlow tutorial MNIST (for beginners)
I tried TensorFlow tutorial CNN 4th
TensorFlow Tutorial MNIST For ML Beginners
TensorFlow Tutorial -MNIST For ML Beginners
I tried using the frequently used seaborn method with as few arguments as possible [for beginners]
I tried to solve the ant book beginner's edition with python
[Pandas] I tried to analyze sales data with Python [For beginners]
I tried running the DNN part of OpenPose with Chainer CPU
I tried to implement Autoencoder with TensorFlow
I tried playing with the image with Pillow
I tried "smoothing" the image with Python + OpenCV
[Python] I tried substituting the function name for the function name
I tried to save the data with discord
I tried non-negative matrix factorization (NMF) with TensorFlow
I played with Floydhub for the time being
I tried python programming for the first time.
I tried "binarizing" the image with Python + OpenCV
I tried running faiss with python, Go, Rust
I tried Mind Meld for the first time
I tried running Deep Floor Plan with Python 3.6.10.
I tried playing with the calculator on tkinter
I tried running an object detection tutorial using the latest deep learning algorithm
I tried object detection with YOLO v3 (TensorFlow 2.1) on the GPU of windows!
I tried searching for files under the folder with Python by file name
I tried to learn the sin function with chainer
[Translation] NumPy Official Tutorial "NumPy: the absolute basics for beginners"
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
I tried running pymc
I tried Python on Mac for the first time.
I tried running the app on the IoT platform "Rimotte"
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
[For beginners in competition professionals] I tried to solve 40 AOJ "ITP I" questions with python
I tried python on heroku for the first time
I tried running TensorFlow in AWS Lambda environment: Preparation
I tried to visualize the running data of the racing game (Assetto Corsa) with Plotly
I tried to solve the problem with Python Vol.1
I tried a simple RPA for login with selenium
I tried to implement Grad-CAM with keras and tensorflow
[For beginners] Quantify the similarity of sentences with TF-IDF
AI Gaming I tried it for the first time
I tried to find an alternating series with tensorflow
I tried hitting the API with echonest's python client
I tried running the sample code of the Ansible module
[For those who want to use TPU] I tried using the Tensorflow Object Detection API 2
I tried running the offline speech recognition system Julius with python in the Docker virtual environment
I tried to find the entropy of the image with python
[TensorFlow] I want to master the indexing for Ragged Tensor