This maybe a very popular beginner projects that worth trying. The MNIST dataset is a very popular dataset containing handwritten numbers from 0 - 9 from different people.
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()Since it's super popular, it's already comes with the TensorFlow library, and it's relatively easy to just load it.
x_train = tf.keras.utils.normalize(x_train, axis = 1)
x_test = tf.keras.utils.normalize(x_test, axis = 1)After loading the data, the next step is to normalize the data. This will make the data easier to process. Then, training the data. Here I use Neural Networks with several layers, with ReLU activation functions and SoftMax at the end. ReLU is quite popular as an activation function. That's why I chose it. If the model doesn't work or need more optimization, then I could try to adjust the activation function as well.
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape = (28, 28)))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))After training the model, it grew from the accuracy of 86.76% to 97.84% on the training data. Then I evaluate using the test data that the model hasn't seen before. I got the accuracy of 96.88%.
Afterwards, I tried to test the model using my own handwriting. However, I ran into small problem that the input size of my handwriting is not compatible, since I just screenshot it freely. (I don't even know the dimension of the screenshots). So I need to resize the image to match the MNIST image size which is 28 x 28.
In the end, I insert my handwriting for the model to predict and it got 72.22%, with 5 wrong guesses from 18 pictures. It's relatively low compare to the test dataset. This happened because some of the resized images don't really resamble the actual numbers.
