Reference URL: https://www.tensorflow.org/tutorials/quickstart/beginner?hl=ja
Do the following --Build a neural network to classify images --Training neural networks --Evaluate the performance of the model
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
#Ver confirmation of tensorflow
print(tf.__version__)
2.3.0
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
tf.keras.layers.Flatten
input_shape = (28, 28) specifies the shape of the input data
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
Randomly disable some neurons to prevent overfitting 0.2 disables 20%
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)
])
Calculate scores called "logit" or "log odds ratio" for each class
Is it the number 5? ??
plt.figure()
plt.imshow(x_train[0] * 255, cmap="gray")
plt.grid(False)
plt.show()
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)
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)
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
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
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, specify
cross entropy --metrics: Items quantified during learning and testing --This time, specify ʻaccuracy
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])
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>
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]
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
Prediction: 2 Correct answer: 2
Prediction: 1 Correct answer: 1
Prediction: 0 Correct answer: 0
Prediction: 4 Correct answer: 4
Recommended Posts