This code shows how an artificial neural network is trained with the MNIST dataset to classify handwritten digits (0-9). The goal is for the model to be able to predict which digit is shown based on the image data.

This is achieved by:
1. Loading and preprocessing of the MNIST image data.
2. Creating a neural network with multiple layers (Layers).
3. Training the network with training data.
4. Evaluating the model’s performance on test data.
5. Testing the model on new sample data.

Classifying digits is a classic machine learning problem that helps beginners understand the basics of neural networks. The MNIST dataset is ideal because it is small enough to train quickly but still complex enough to build meaningful models.

Visualization of the Model

Bash Output

Explanation of the Bash Output

These are the actual digits (labels) of the first ten images from the MNIST training dataset.This serves to verify the labels and ensure that the data has been loaded correctly.

This message means that TensorFlow recognizes and uses the CPU acceleration features AVX and AVX2.

TensorFlow recognizes and uses the NVIDIA GeForce RTX 4070 Ti as the GPU for training. Compute Capability 8.9 means that the GPU supports Tensor Cores and other CUDA-optimized computations.

Epoch 1/5 means that the first of five training epochs is starting.
1875/1875 shows that the model has processed all 60,000 training images in 1875 batches of 32 images each.
Duration: 3 seconds
Loss (error rate on training data): 0.2411
Accuracy (accuracy on training data): 93.05%
val_loss (error rate on validation data): 0.1258
val_accuracy (accuracy on validation data): 96.02%

Already after the first epoch, the model has a very high accuracy of 93% on the training data and 96% on the test data. The model improves with each epoch. Epoch 5: The final accuracy is 98.62% on the training data and 97.93% on the test data.

The model is evaluated with the test data (10,000 images). Accuracy: 97.93% on the test data. This means that in 97.93% of cases the model predicts the correct digit.

A single test image was given to the model. Model says: 7