Mnist - Ipynb - Colaboratory (p2)
Mnist - Ipynb - Colaboratory (p2)
ipynb - Colaboratory
import pandas as pd
import numpy as np
import tensorflow as tf
from tensorflow.keras import models, datasets, layers
import matplotlib.pyplot as plt
import matplotlib.image as mp
(train_images,train_labels),(test_images,test_labels)=datasets.mnist.load_data()
account_circle x_tain:
y_tain:
(60000, 28, 28)
(60000,)
x_test: (10000, 28, 28)
y_test: (10000,)
pd.DataFrame(train_images[0])
0 1 2 3 4 5 6 7 8 9 ... 18 19 20 21 22 23 24 25 26 27
0 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
9 0 0 0 0 0 0 0 0 80 156 ... 0 0 0 0 0 0 0 0 0 0
10 0 0 0 0 0 0 0 0 0 14 ... 0 0 0 0 0 0 0 0 0 0
11 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
12 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
13 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
14 0 0 0 0 0 0 0 0 0 0 ... 25 0 0 0 0 0 0 0 0 0
15 0 0 0 0 0 0 0 0 0 0 ... 150 27 0 0 0 0 0 0 0 0
20 0 0 0 0 0 0 0 0 0 0 ... 78 0 0 0 0 0 0 0 0 0
21 0 0 0 0 0 0 0 0 23 66 ... 0 0 0 0 0 0 0 0 0 0
25 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
26 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
27 0 0 0 0 0 0 0 0 0 0 ... 0 0 0 0 0 0 0 0 0 0
28 rows × 28 columns
train_images=train_images/255
test_images=test_images/255
https://colab.research.google.com/drive/1miyOaypjt6SMDJkNztxAd7mihSOPUrcD#printMode=true 1/3
11/1/23, 9:00 AM mnist.ipynb - Colaboratory
model=models.Sequential()
model.add(layers.Flatten(input_shape=(28,28,1)))
model.add(layers.Dense(32,activation='relu'))
model.add(layers.Dense(16,activation='relu'))
model.add(layers.Dense(10,activation='softmax'))
model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
=================================================================
Total params: 25818 (100.85 KB)
Trainable params: 25818 (100.85 KB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________
Epoch 1/10
1875/1875 [==============================] - 13s 4ms/step - loss: 0.4021 - accuracy: 0.8810 - val_loss: 0.2010 - val_accuracy: 0.944
Epoch 2/10
1875/1875 [==============================] - 7s 4ms/step - loss: 0.1852 - accuracy: 0.9459 - val_loss: 0.1558 - val_accuracy: 0.9560
Epoch 3/10
1875/1875 [==============================] - 7s 3ms/step - loss: 0.1459 - accuracy: 0.9569 - val_loss: 0.1463 - val_accuracy: 0.9591
Epoch 4/10
1875/1875 [==============================] - 7s 4ms/step - loss: 0.1245 - accuracy: 0.9632 - val_loss: 0.1365 - val_accuracy: 0.9617
Epoch 5/10
1875/1875 [==============================] - 7s 4ms/step - loss: 0.1102 - accuracy: 0.9675 - val_loss: 0.1336 - val_accuracy: 0.9614
Epoch 6/10
1875/1875 [==============================] - 8s 4ms/step - loss: 0.0989 - accuracy: 0.9704 - val_loss: 0.1166 - val_accuracy: 0.9661
Epoch 7/10
1875/1875 [==============================] - 7s 4ms/step - loss: 0.0892 - accuracy: 0.9735 - val_loss: 0.1166 - val_accuracy: 0.9670
Epoch 8/10
1875/1875 [==============================] - 6s 3ms/step - loss: 0.0809 - accuracy: 0.9758 - val_loss: 0.1137 - val_accuracy: 0.9689
Epoch 9/10
1875/1875 [==============================] - 7s 4ms/step - loss: 0.0751 - accuracy: 0.9769 - val_loss: 0.1240 - val_accuracy: 0.9687
Epoch 10/10
1875/1875 [==============================] - 7s 3ms/step - loss: 0.0705 - accuracy: 0.9785 - val_loss: 0.1128 - val_accuracy: 0.9701
score = model.evaluate(test_images,test_labels)
print("test loss :", score[0])
print("test accuracy :", score[1])
model_name="file.h5"
model.save(model_name,save_format='h5')
loaded_model = tf.keras.models.load_model(model_name)
predictions_one_hot = loaded_model.predict([test_images])
predictions=np.argmax(predictions_one_hot, axis=1)
pd.DataFrame(predictions)
https://colab.research.google.com/drive/1miyOaypjt6SMDJkNztxAd7mihSOPUrcD#printMode=true 2/3
11/1/23, 9:00 AM mnist.ipynb - Colaboratory
0 7
1 2
2 1
3 0
4 4
... ...
9995 2
9996 3
9997 4
9998 5
9999 6
print(predictions[3])
plt.imshow(test_images[3].reshape((28,28)),cmap=plt.cm.binary)
plt.show()
https://colab.research.google.com/drive/1miyOaypjt6SMDJkNztxAd7mihSOPUrcD#printMode=true 3/3