In [1]:
import tensorflow as tf

# Check if a GPU is available
if tf.test.gpu_device_name():
    print('GPU device found:', tf.test.gpu_device_name())
else:
    print("No GPU found. Please make sure you have enabled GPU acceleration in the notebook settings.")
No GPU found. Please make sure you have enabled GPU acceleration in the notebook settings.
In [12]:
import tensorflow as tf
from tensorflow import keras
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.regularizers import l2
from keras.preprocessing.image import ImageDataGenerator

Data¶

In [13]:
(x_train, y_train), (x_test, y_test)=keras.datasets.mnist.load_data()
In [14]:
x_train, x_test= x_train/255.0, x_test/255.0
In [15]:
x_train.shape, x_test.shape
Out[15]:
((60000, 28, 28), (10000, 28, 28))
In [16]:
x_train[0].shape
Out[16]:
(28, 28)
In [17]:
plt.imshow(x_train[2])
Out[17]:
<matplotlib.image.AxesImage at 0x7ae0a4c6a050>
In [18]:
num_classes = len(set(y_train))
num_classes
Out[18]:
10
In [19]:
num_classes = len(np.unique(y_train))
num_classes
Out[19]:
10

Model¶

For the model, I create an arbritrary deep model with several Conv2D and MaxPooling2D layers as well as Dropout layers

In [20]:
model= keras.models.Sequential([

    keras.layers.Conv2D(50, kernel_size=(3, 3), activation="relu",
                        padding="same", kernel_initializer= "he_normal",kernel_regularizer=l2(0.01),input_shape=[28,28,1]), # grayscale images; stride by defaul=1
    keras.layers.MaxPooling2D(pool_size=(2,2),strides=(2,2)),
    keras.layers.Conv2D(100, 3,activation="relu", padding="same",kernel_initializer= "he_normal",kernel_regularizer=l2(0.01)),
    keras.layers.MaxPooling2D(2),
    keras.layers.Conv2D(50,3,activation="relu",padding="same",kernel_initializer= "he_normal",kernel_regularizer=l2(0.01)),
    keras.layers.MaxPooling2D(2),
    keras.layers.Flatten(),
    keras.layers.Dense(100,activation="relu",kernel_initializer= "he_normal",kernel_regularizer=l2(0.01)),
    keras.layers.Dropout(0.4),
    keras.layers.Dense(50,activation="relu",kernel_initializer= "he_normal",kernel_regularizer=l2(0.01)),
    keras.layers.Dropout(0.4),
    keras.layers.Dense(10,activation="softmax"),

])

compile model¶

In [21]:
model.compile(loss="sparse_categorical_crossentropy",
              optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
              metrics=["accuracy"])

train(fit) model¶

In [23]:
history=model.fit(x_train,y_train,epochs=15,validation_split=0.15)
Epoch 1/15
1594/1594 [==============================] - 154s 96ms/step - loss: 1.1832 - accuracy: 0.9015 - val_loss: 0.5030 - val_accuracy: 0.9686
Epoch 2/15
1594/1594 [==============================] - 148s 93ms/step - loss: 0.5887 - accuracy: 0.9359 - val_loss: 0.4317 - val_accuracy: 0.9678
Epoch 3/15
1594/1594 [==============================] - 148s 93ms/step - loss: 0.5333 - accuracy: 0.9402 - val_loss: 0.4164 - val_accuracy: 0.9646
Epoch 4/15
1594/1594 [==============================] - 143s 90ms/step - loss: 0.4999 - accuracy: 0.9442 - val_loss: 0.3722 - val_accuracy: 0.9731
Epoch 5/15
1594/1594 [==============================] - 147s 92ms/step - loss: 0.4753 - accuracy: 0.9458 - val_loss: 0.3581 - val_accuracy: 0.9757
Epoch 6/15
1594/1594 [==============================] - 150s 94ms/step - loss: 0.4626 - accuracy: 0.9483 - val_loss: 0.3517 - val_accuracy: 0.9727
Epoch 7/15
1594/1594 [==============================] - 149s 94ms/step - loss: 0.4506 - accuracy: 0.9489 - val_loss: 0.3495 - val_accuracy: 0.9721
Epoch 8/15
1594/1594 [==============================] - 151s 95ms/step - loss: 0.4401 - accuracy: 0.9499 - val_loss: 0.3337 - val_accuracy: 0.9760
Epoch 9/15
1594/1594 [==============================] - 149s 93ms/step - loss: 0.4300 - accuracy: 0.9518 - val_loss: 0.3328 - val_accuracy: 0.9737
Epoch 10/15
1594/1594 [==============================] - 147s 92ms/step - loss: 0.4248 - accuracy: 0.9517 - val_loss: 0.3240 - val_accuracy: 0.9760
Epoch 11/15
1594/1594 [==============================] - 146s 91ms/step - loss: 0.4145 - accuracy: 0.9531 - val_loss: 0.3260 - val_accuracy: 0.9721
Epoch 12/15
1594/1594 [==============================] - 150s 94ms/step - loss: 0.4087 - accuracy: 0.9531 - val_loss: 0.3196 - val_accuracy: 0.9744
Epoch 13/15
1594/1594 [==============================] - 146s 92ms/step - loss: 0.4053 - accuracy: 0.9534 - val_loss: 0.3125 - val_accuracy: 0.9743
Epoch 14/15
1594/1594 [==============================] - 149s 93ms/step - loss: 0.3958 - accuracy: 0.9556 - val_loss: 0.3190 - val_accuracy: 0.9709
Epoch 15/15
1594/1594 [==============================] - 145s 91ms/step - loss: 0.3959 - accuracy: 0.9551 - val_loss: 0.3067 - val_accuracy: 0.9739
In [24]:
print(history.history)
{'loss': [1.1831835508346558, 0.5886949896812439, 0.5333249568939209, 0.4998887777328491, 0.47531718015670776, 0.4626152813434601, 0.45060884952545166, 0.44007253646850586, 0.43000471591949463, 0.4248494505882263, 0.41450634598731995, 0.40870776772499084, 0.4053203761577606, 0.39576956629753113, 0.395870178937912], 'accuracy': [0.9015098214149475, 0.9359215497970581, 0.940156877040863, 0.9441764950752258, 0.9458431601524353, 0.9483137130737305, 0.9488823413848877, 0.9499411582946777, 0.9517843127250671, 0.9517058730125427, 0.9531176686286926, 0.9530980587005615, 0.9533921480178833, 0.9555882215499878, 0.9551176428794861], 'val_loss': [0.5030266642570496, 0.4316800534725189, 0.41640254855155945, 0.3722226917743683, 0.35805702209472656, 0.3517487347126007, 0.349468857049942, 0.33373522758483887, 0.3328009843826294, 0.3240131437778473, 0.32600101828575134, 0.31959614157676697, 0.3124983310699463, 0.31900644302368164, 0.3067024052143097], 'val_accuracy': [0.9685555696487427, 0.9677777886390686, 0.964555561542511, 0.973111093044281, 0.9756666421890259, 0.9726666808128357, 0.9721111059188843, 0.9760000109672546, 0.9736666679382324, 0.9760000109672546, 0.9721111059188843, 0.9744444489479065, 0.9743333458900452, 0.9708889126777649, 0.9738888740539551]}
In [25]:
fig, ax =  plt.subplots(figsize=(12,5))
ax.plot(history.history["loss"],label="train_loss")
ax.plot(history.history["accuracy"],label="train_accuracy")
ax.plot(history.history["val_loss"],label = "val_loss")
ax.plot(history.history["val_accuracy"],label = 'val_accuracy')
ax.legend()
Out[25]:
<matplotlib.legend.Legend at 0x7ae09277ebf0>

evaluate the performance of the trained model on a test dataset¶

In [26]:
model.evaluate(x_test,y_test)
313/313 [==============================] - 7s 24ms/step - loss: 0.2963 - accuracy: 0.9788
Out[26]:
[0.2963279187679291, 0.9787999987602234]

prediction¶

In [ ]:
x_test.shape
Out[ ]:
(10000, 28, 28)
In [ ]:
x1=x_test[0]
x1.shape
Out[ ]:
(28, 28)
In [ ]:
plt.imshow(x1)
Out[ ]:
<matplotlib.image.AxesImage at 0x7e6192d68160>
In [ ]:
x1 = x1.reshape((1, 28, 28, 1))  # Assuming  model expects input shape (batch_size, height, width, channels)
prediction = model.predict(x1)
1/1 [==============================] - 1s 682ms/step
In [ ]:
print(prediction)
[[1.06617015e-35 1.94560221e-18 3.18099171e-21 3.64374880e-18
  1.89552256e-25 1.07535842e-24 0.00000000e+00 1.00000000e+00
  8.91905492e-31 2.51174452e-17]]
In [ ]:
predicted_class = np.argmax(prediction)
print(predicted_class)
7

I evaluate now with another test data

In [ ]:
x2=x_test[900]
plt.imshow(x2)
Out[ ]:
<matplotlib.image.AxesImage at 0x7e6192daaec0>
In [ ]:
x2=x2.reshape((1, 28, 28, 1))
prediction = model.predict(x2)
print(prediction)
1/1 [==============================] - 0s 29ms/step
[[7.4877670e-16 9.9999988e-01 5.8919492e-10 6.8755689e-08 1.2284365e-14
  2.7449447e-09 9.8647583e-12 1.3068326e-08 6.6703011e-11 3.7738847e-13]]
In [ ]:
predicted_class = np.argmax(prediction)
print(predicted_class)
1
In [ ]: