<jupyter_start><jupyter_text>Classifying grayscale images of handwritten digits (28 pixels by 28 pixels), into their 10 categories (0 to 9). MNIST dataset is a set of 60,000 training images, plus 10,000 test images, assembled by the National Institute of Standards and Technology (the NIST in MNIST) in the 1980s. The MNIST dataset comes pre-loaded in Keras, in the form of a set of four Numpy arrays:<jupyter_code>from keras.datasets import mnist

(train_images, train_labels), (test_images, test_labels) = mnist.load_data()<jupyter_output>Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 2s 0us/step<jupyter_text>`train_images` and `train_labels` form the "training set", the data that the model will learn from. The model will then be tested on the "test set", `test_images` and `test_labels`. Our images are encoded as Numpy arrays, and the labels are simply an array of digits, ranging from 0 to 9. There is a one-to-one correspondence between the images and the labels.Let's have a look at the training data:<jupyter_code>train_images.shape
len(train_labels)
train_labels<jupyter_output><empty_output><jupyter_text>Let's have a look at the test data:<jupyter_code>test_images.shape
len(test_labels)
test_labels<jupyter_output><empty_output><jupyter_text>1. train with `train_images` and `train_labels`. 2. learn how to associate images and labels 3. predictions for `test_images`4. verify if the predictions match the labels from `test_labels`Let's build our network<jupyter_code>from keras import models
from keras import layers

network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
network.add(layers.Dense(10, activation='softmax'))<jupyter_output><empty_output><jupyter_text>* The Layers Extract `Representations` out of data* The Representations that are more meaningful to the problem at hand * Deep Learning models are succession of increasingly refined data **Filters/Layers*** Here the network consists of a sequence of two `Dense` layers,  * `Dense` layers are densely-connected (also called "fully-connected") neural layers. * The last layer is a 10-way "softmax" layer,  * Returning an array of 10 probability scores (summing to 1).  * Each score be the probability that the current digit image belongs to one of our 10 digit classes.To make model ready for training* A loss function:  * how the network be able to measure how good a job it is doing on its training data, and thus how it will be able to steer itself in the right direction.* An optimizer:  * the mechanism through which the network update itself based on the data it sees and its loss function.* Metrics to monitor during training and testing.  * Accuracy = the fraction of the images that were correctly classified<jupyter_code>network.compile(optimizer='rmsprop',
                loss='categorical_crossentropy',
                metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Before training, we will preprocess our data by reshaping it into the shape that the network expects, and scaling it so that all values are in the `[0, 1]` interval. Previously, our training images for instance were stored in an array of shape `(60000, 28, 28)` of type `uint8` with values in the `[0, 255]` interval. We transform it into a `float32` array of shape `(60000, 28 * 28)` with values between 0 and 1.<jupyter_code>train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255<jupyter_output><empty_output><jupyter_text>We also need to categorically encode the labels, a step which we explain in chapter 3:<jupyter_code>from keras.utils import to_categorical

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)<jupyter_output><empty_output><jupyter_text>We are now ready to train our network, which in Keras is done via a call to the `fit` method of the network: we "fit" the model to its training data.<jupyter_code>network.fit(train_images, train_labels, epochs=5, batch_size=128)<jupyter_output>Epoch 1/5
60000/60000 [==============================] - 2s - loss: 0.2577 - acc: 0.9245     
Epoch 2/5
60000/60000 [==============================] - 1s - loss: 0.1042 - acc: 0.9690     
Epoch 3/5
60000/60000 [==============================] - 1s - loss: 0.0687 - acc: 0.9793     
Epoch 4/5
60000/60000 [==============================] - 1s - loss: 0.0508 - acc: 0.9848     
Epoch 5/5
60000/60000 [==============================] - 1s - loss: 0.0382 - acc: 0.9890<jupyter_text>Two quantities are being displayed during training: the "loss" of the network over the training data, and the accuracy of the network over the training data.We quickly reach an accuracy of 0.989 (i.e. 98.9%) on the training data. Now let's check that our model performs well on the test set too:<jupyter_code>test_loss, test_acc = network.evaluate(test_images, test_labels)
print('test_acc:', test_acc)<jupyter_output>test_acc: 0.9777
{'fraction_non_alphanumeric': 0.11730319563522992, 'fraction_numerical': 0.04890880748246298, 'mean_word_length': 5.352722772277228, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '109406', 'license': 'MIT', 'n_tokens_mistral': 1718, 'n_tokens_neox': 1506, 'n_words': 598, 'path': '.ipynb_checkpoints/2.1-a-first-look-at-a-neural-network-Copy1-checkpoint.ipynb', 'repo_name': 'memari-majid/deep-learning-with-python-notebooks', 'size': '5132'}