KERAS and MNIST#

Let’s start by trying to reproduce our MNIST example using Keras. Because there are a lot more options in Keras/TensorFlow, we can add more layers and different activation functions.

import keras

import matplotlib.pyplot as plt
import numpy as np
2024-12-08 18:25:15.447354: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-12-08 18:25:15.450533: I external/local_xla/xla/tsl/cuda/cudart_stub.cc:32] Could not find cuda drivers on your machine, GPU will not be used.
2024-12-08 18:25:15.459236: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:477] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered
WARNING: All log messages before absl::InitializeLog() is called are written to STDERR
E0000 00:00:1733682315.473328    3753 cuda_dnn.cc:8310] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered
E0000 00:00:1733682315.477536    3753 cuda_blas.cc:1418] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
2024-12-08 18:25:15.494080: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.

This notebook uses keras and the tensorflow backend to do character recognition on the MNIST digits. You need to have the keras and tensorflow packages installed. Also for visualization of the network, you need to have pydot installed.

We follow the example for setting up the network: Vict0rSch/deep_learning

The MNIST data#

The keras library can download the MNIST data directly and provides a function to give us both the training and test images and the corresponding digits. This is already in a format that Keras wants, so we don’t use the classes that we defined earlier.

from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
       0/11490434 ━━━━━━━━━━━━━━━━━━━━ 0s 0s/step

  598016/11490434 ━━━━━━━━━━━━━━━━━━━ 0s 0us/step

 4202496/11490434 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

11490434/11490434 ━━━━━━━━━━━━━━━━━━━━ 0s 0us/step

As before, the training set consists of 60000 digits represented as a 28x28 array (there are no color channels, so this is grayscale data). They are also integer data.

X_train.shape
(60000, 28, 28)
X_train.dtype
dtype('uint8')

Let’s look at the first digit and the “y” value (target) associated with it—that’s the correct answer.

plt.imshow(X_train[0], cmap="gray_r")
print(y_train[0])
5
../_images/144b1562cf1c89fbda547fe55b85a4ed5514928639273293b27827c8fb08a9fc.png

Preparing the Data#

The neural network takes a 1-d vector of input and will return a 1-d vector of output. We need to convert our data to this form.

We’ll scale the image data to fall in [0, 1) and the numerical output to be categorized as an array. Finally, we need the input data to be one-dimensional, so we fill flatten the 28x28 images into a single 784 vector.

X_train = X_train.astype('float32')/255
X_test = X_test.astype('float32')/255

X_train = np.reshape(X_train, (60000, 784))
X_test = np.reshape(X_test, (10000, 784))
X_train[0]
array([0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.01176471, 0.07058824, 0.07058824,
       0.07058824, 0.49411765, 0.53333336, 0.6862745 , 0.10196079,
       0.6509804 , 1.        , 0.96862745, 0.49803922, 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.11764706, 0.14117648, 0.36862746, 0.6039216 ,
       0.6666667 , 0.99215686, 0.99215686, 0.99215686, 0.99215686,
       0.99215686, 0.88235295, 0.6745098 , 0.99215686, 0.9490196 ,
       0.7647059 , 0.2509804 , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.19215687, 0.93333334,
       0.99215686, 0.99215686, 0.99215686, 0.99215686, 0.99215686,
       0.99215686, 0.99215686, 0.99215686, 0.9843137 , 0.3647059 ,
       0.32156864, 0.32156864, 0.21960784, 0.15294118, 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.07058824, 0.85882354, 0.99215686, 0.99215686,
       0.99215686, 0.99215686, 0.99215686, 0.7764706 , 0.7137255 ,
       0.96862745, 0.94509804, 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.3137255 , 0.6117647 , 0.41960785, 0.99215686, 0.99215686,
       0.8039216 , 0.04313726, 0.        , 0.16862746, 0.6039216 ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.05490196,
       0.00392157, 0.6039216 , 0.99215686, 0.3529412 , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.54509807,
       0.99215686, 0.74509805, 0.00784314, 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.04313726, 0.74509805, 0.99215686,
       0.27450982, 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.13725491, 0.94509804, 0.88235295, 0.627451  ,
       0.42352942, 0.00392157, 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.31764707, 0.9411765 , 0.99215686, 0.99215686, 0.46666667,
       0.09803922, 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.1764706 ,
       0.7294118 , 0.99215686, 0.99215686, 0.5882353 , 0.10588235,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.0627451 , 0.3647059 ,
       0.9882353 , 0.99215686, 0.73333335, 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.9764706 , 0.99215686,
       0.9764706 , 0.2509804 , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.18039216, 0.50980395,
       0.7176471 , 0.99215686, 0.99215686, 0.8117647 , 0.00784314,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.15294118,
       0.5803922 , 0.8980392 , 0.99215686, 0.99215686, 0.99215686,
       0.98039216, 0.7137255 , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.09411765, 0.44705883, 0.8666667 , 0.99215686, 0.99215686,
       0.99215686, 0.99215686, 0.7882353 , 0.30588236, 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.09019608, 0.25882354, 0.8352941 , 0.99215686,
       0.99215686, 0.99215686, 0.99215686, 0.7764706 , 0.31764707,
       0.00784314, 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.07058824, 0.67058825, 0.85882354,
       0.99215686, 0.99215686, 0.99215686, 0.99215686, 0.7647059 ,
       0.3137255 , 0.03529412, 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.21568628, 0.6745098 ,
       0.8862745 , 0.99215686, 0.99215686, 0.99215686, 0.99215686,
       0.95686275, 0.52156866, 0.04313726, 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.53333336, 0.99215686, 0.99215686, 0.99215686,
       0.83137256, 0.5294118 , 0.5176471 , 0.0627451 , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        , 0.        ,
       0.        , 0.        , 0.        , 0.        ], dtype=float32)

As we did in our example, we will use categorical data. Keras includes routines to categorize data. In our case, since there are 10 possible digits, we want to put the output into 10 categories (represented by 10 neurons)

from keras.utils import np_utils

y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
Cell In[9], line 1
----> 1 from keras.utils import np_utils
      3 y_train = np_utils.to_categorical(y_train, 10)
      4 y_test = np_utils.to_categorical(y_test, 10)

ImportError: cannot import name 'np_utils' from 'keras.utils' (/opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/site-packages/keras/api/utils/__init__.py)

Now let’s look at the target for the first training digit. We know from above that it was ‘5’. Here we see that there is a 1 in the index corresponding to 5 (remember we start counting at 0 in python).

y_train[0]
array([0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], dtype=float32)

Build the Neural Network#

Now we’ll build the neural network. We will have 2 hidden layers, and the number of neurons will look like:

784 → 500 → 300 → 10

Layers#

Let’s start by setting up the layers. For each layer, we tell keras the number of output neurons. It infers the number of inputs from the previous layer (with the exception of the input layer, where we need to tell it what to expect as input).

Properties on the layers:

from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation

model = Sequential()
model.add(Dense(500, input_dim=784, activation="relu"))
model.add(Dropout(0.4))
model.add(Dense(300, activation="relu"))
model.add(Dropout(0.4))
model.add(Dense(10, activation="softmax"))

Let’s look at the network

from keras.utils.vis_utils import plot_model
plot_model(model, show_shapes=True, show_layer_names=True)
../_images/d88eb4c174b4d597413e6f942e0ed86c25527a27af8cda70877b31ec14e24912.png

Loss function#

We need to specify what we want to optimize and how we are going to do it.

Recall: the loss (or cost) function measures how well our predictions match the expected target. Previously we were using the sum of the squares of the error.

For categorical data, like we have, the “cross-entropy” metric is often used. See here for an explanation: https://jamesmccaffrey.wordpress.com/2013/11/05/why-you-should-use-cross-entropy-error-instead-of-classification-error-or-mean-squared-error-for-neural-network-classifier-training/

Optimizer#

We also need to specify an optimizer. This could be gradient descent, as we used before. Here’s a list of the optimizers supoprted by keras: https://keras.io/api/optimizers/ We’ll use RMPprop, which builds off of gradient descent and includes some momentum.

Finally, we need to specify a metric that is evaluated during training and testing. We’ll use "accuracy" here. This means that we’ll see the accuracy of our model reported as we are training and testing.

More details on these options is here: https://keras.io/api/models/model/

from tensorflow.keras.optimizers import RMSprop

rms = RMSprop()
model.compile(loss='categorical_crossentropy',
              optimizer=rms, metrics=['accuracy'])

Train#

For training, we pass in the inputs and target and the number of epochs to run and it will optimize the network by adjusting the weights between the nodes in the layers.

The number of epochs is the number of times the entire data set is passed forward and backward through the network. The batch size is the number of training pairs you pass through the network at a given time. You update the parameter in your model (the weights) once for each batch. This makes things more efficient and less noisy.

epochs = 20
batch_size = 256
model.fit(X_train, y_train, epochs=epochs, batch_size=batch_size,
          validation_data=(X_test, y_test), verbose=2)
Epoch 1/20
235/235 - 2s - loss: 0.3749 - accuracy: 0.8850 - val_loss: 0.1474 - val_accuracy: 0.9546 - 2s/epoch - 7ms/step
Epoch 2/20
235/235 - 1s - loss: 0.1597 - accuracy: 0.9515 - val_loss: 0.1051 - val_accuracy: 0.9678 - 1s/epoch - 6ms/step
Epoch 3/20
235/235 - 1s - loss: 0.1184 - accuracy: 0.9650 - val_loss: 0.0835 - val_accuracy: 0.9733 - 1s/epoch - 6ms/step
Epoch 4/20
235/235 - 1s - loss: 0.0976 - accuracy: 0.9699 - val_loss: 0.0717 - val_accuracy: 0.9783 - 1s/epoch - 6ms/step
Epoch 5/20
235/235 - 1s - loss: 0.0833 - accuracy: 0.9744 - val_loss: 0.0667 - val_accuracy: 0.9802 - 1s/epoch - 6ms/step
Epoch 6/20
235/235 - 1s - loss: 0.0735 - accuracy: 0.9774 - val_loss: 0.0672 - val_accuracy: 0.9803 - 1s/epoch - 6ms/step
Epoch 7/20
235/235 - 1s - loss: 0.0625 - accuracy: 0.9804 - val_loss: 0.0625 - val_accuracy: 0.9808 - 1s/epoch - 6ms/step
Epoch 8/20
235/235 - 1s - loss: 0.0592 - accuracy: 0.9818 - val_loss: 0.0573 - val_accuracy: 0.9824 - 1s/epoch - 6ms/step
Epoch 9/20
235/235 - 1s - loss: 0.0526 - accuracy: 0.9823 - val_loss: 0.0615 - val_accuracy: 0.9819 - 1s/epoch - 6ms/step
Epoch 10/20
235/235 - 1s - loss: 0.0483 - accuracy: 0.9844 - val_loss: 0.0619 - val_accuracy: 0.9823 - 1s/epoch - 6ms/step
Epoch 11/20
235/235 - 1s - loss: 0.0461 - accuracy: 0.9854 - val_loss: 0.0646 - val_accuracy: 0.9821 - 1s/epoch - 6ms/step
Epoch 12/20
235/235 - 1s - loss: 0.0420 - accuracy: 0.9863 - val_loss: 0.0606 - val_accuracy: 0.9834 - 1s/epoch - 6ms/step
Epoch 13/20
235/235 - 1s - loss: 0.0397 - accuracy: 0.9871 - val_loss: 0.0611 - val_accuracy: 0.9827 - 1s/epoch - 6ms/step
Epoch 14/20
235/235 - 1s - loss: 0.0378 - accuracy: 0.9877 - val_loss: 0.0598 - val_accuracy: 0.9836 - 1s/epoch - 6ms/step
Epoch 15/20
235/235 - 1s - loss: 0.0343 - accuracy: 0.9892 - val_loss: 0.0646 - val_accuracy: 0.9828 - 1s/epoch - 6ms/step
Epoch 16/20
235/235 - 1s - loss: 0.0330 - accuracy: 0.9898 - val_loss: 0.0646 - val_accuracy: 0.9830 - 1s/epoch - 6ms/step
Epoch 17/20
235/235 - 1s - loss: 0.0319 - accuracy: 0.9895 - val_loss: 0.0639 - val_accuracy: 0.9832 - 1s/epoch - 6ms/step
Epoch 18/20
235/235 - 1s - loss: 0.0290 - accuracy: 0.9906 - val_loss: 0.0603 - val_accuracy: 0.9848 - 1s/epoch - 6ms/step
Epoch 19/20
235/235 - 1s - loss: 0.0295 - accuracy: 0.9904 - val_loss: 0.0603 - val_accuracy: 0.9855 - 1s/epoch - 6ms/step
Epoch 20/20
235/235 - 1s - loss: 0.0282 - accuracy: 0.9909 - val_loss: 0.0589 - val_accuracy: 0.9851 - 1s/epoch - 6ms/step
<keras.callbacks.History at 0x7faa75320090>

Test#

keras has a routine, evaluate() that can take the inputs and targets of a test data set and return the loss value and accuracy (or other defined metrics) on this data.

Here we see we are > 98% accurate on the test data—this is the data that the model has never seen before (and was not trained with).

loss_value, accuracy = model.evaluate(X_test, y_test, batch_size=16)
print(accuracy)
625/625 [==============================] - 1s 813us/step - loss: 0.0589 - accuracy: 0.9851
0.9850999712944031

Predicting#

Suppose we simply want to ask our neural network to predict the target for an input. We can use the predict() method to return the category array with the predictions. We can then use np.argmax() to select the most probable.

np.argmax(model.predict(np.array([X_test[0]])))
1/1 [==============================] - 0s 46ms/step
7
y_test[0]
array([0., 0., 0., 0., 0., 0., 0., 1., 0., 0.], dtype=float32)

Now let’s loop over the test set and print out what we predict vs. the true answer for those we get wrong. We can also plot the image of the digit.

wrong = 0
max_wrong = 10

for n, (x, y) in enumerate(zip(X_test, y_test)):
    try:
        res = model.predict(np.array([x]), verbose=0)
        if np.argmax(res) != np.argmax(y):
            print("test {}: prediction = {}, truth is {}".format(n, np.argmax(res), np.argmax(y)))
            plt.imshow(x.reshape(28, 28), cmap="gray_r")
            plt.show()
            wrong += 1
            if (wrong > max_wrong-1):
                break
    except KeyboardInterrupt:
        print("stopping")
        break
test 247: prediction = 2, truth is 4
../_images/b8338bd91f2ebecd32b5ee414f607df96f44ee99b561586e42fda17d81aff5da.png
test 321: prediction = 7, truth is 2
../_images/59d6b610ee513c26edb35979dc886e6f99a1833a7a3c160d380ce1ef496bff39.png
test 340: prediction = 3, truth is 5
../_images/4db2c44ef2c4c779e54b5132410e7ef116fa091efa027787d5ebce1861ad0768.png
test 445: prediction = 0, truth is 6
../_images/dcd2acda1d02219e3b5142372c12b6c42081c340ef8af88cff33c283f0374546.png
test 495: prediction = 2, truth is 8
../_images/83ed3971013179e83f880b6876ee469bb40676385b63d2a969bb30c352aa0a82.png
test 582: prediction = 2, truth is 8
../_images/a375459f2b8493988ea6081fcb3e559e569787decbedc07633f5767fb083869c.png
test 619: prediction = 8, truth is 1
../_images/356bf4ef64e3abbea21c4de12c65c12229c6387b696046c7e0ae003b23c545ec.png
test 646: prediction = 6, truth is 2
../_images/f2175865716bd472bca3309a685f878683e665c3054b602f788246a1475a1cd9.png
test 674: prediction = 3, truth is 5
../_images/9375b66cd0e3e0fcb4ee606d0dc781c9e10ae1a44bd969f6e0ae0af06ac1134a.png
test 684: prediction = 3, truth is 7
../_images/a111fd6eed6c262a1840060a62444466781bb0ec08e842f220287b8fbc21f8f5.png

Experimenting#

There are a number of things we can play with to see how the network performance changes:

  • batch size

  • adding or removing hidden layers

  • changing the dropout

  • changing the activation function

Callbacks#

Keras allows for callbacks each epoch to store some information. These can allow you to, for example, plot of the accuracy vs. epoch by adding a callback. Take a look here for some inspiration:

https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/History

Going Further#

Convolutional neural networks are often used for image recognition, especially with larger images. They use filter to try to recognize patterns in portions of images (A tile). See this for a keras example:

https://www.tensorflow.org/tutorials/images/cnn