Chapter 2.1 - A first look at a neural network

Loading the dataset


In [1]:
from keras.datasets import mnist


Using TensorFlow backend.

Loading training and testing datasets.


In [2]:
(train_images, train_labels),(test_images, test_labels) = mnist.load_data()

In [3]:
train_images.shape


Out[3]:
(60000, 28, 28)

In [4]:
train_labels.shape


Out[4]:
(60000,)

In [5]:
train_labels


Out[5]:
array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)

In [6]:
test_images.shape


Out[6]:
(10000, 28, 28)

In [7]:
test_labels.shape


Out[7]:
(10000,)

In [8]:
test_labels


Out[8]:
array([7, 2, 1, ..., 4, 5, 6], dtype=uint8)

Creating a network


In [9]:
# Importing models like Sequential or Functional Model
from keras import models

In [10]:
# Imporing layers like Dense
from keras import layers

In [11]:
# Initializing a sequential network
network = models.Sequential()

In [12]:
# Adding the input layer
network.add(layers.Dense(units = 512, 
                         activation = 'relu', 
                         input_shape = (28 * 28,)))

In [13]:
# Adding the outpul layer
network.add(layers.Dense(units = 10, 
                         activation = 'softmax'))

In [14]:
# Compiling the network
network.compile(optimizer='rmsprop',
                loss='categorical_crossentropy',
                metrics = ['accuracy'])

Preparing the image data


In [15]:
# Reshaping the data from a 3-D array (number of 2-d examples) to a 2-D array (number of 1-D examples)
train_images = train_images.reshape((60000, 28 * 28))

In [16]:
# Changing type of the data to float32
train_images = train_images.astype('float32')

In [17]:
# Rescaling the data from 0-255 to 0-1
train_images = train_images / 255.0

In [18]:
test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32')
test_images = test_images / 255.0

Preparing the labels


In [19]:
# Importing one-hot encoding tool to_categorical from keras.utils
from keras.utils import to_categorical

In [20]:
# One-hot encoding the labels
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

Training the network


In [21]:
network.fit(train_images, 
            train_labels, 
            epochs = 5,
            batch_size = 128)


Epoch 1/5
60000/60000 [==============================] - 3s - loss: 0.2591 - acc: 0.9242     
Epoch 2/5
60000/60000 [==============================] - 2s - loss: 0.1031 - acc: 0.9693     
Epoch 3/5
60000/60000 [==============================] - 2s - loss: 0.0683 - acc: 0.9799     
Epoch 4/5
60000/60000 [==============================] - 2s - loss: 0.0502 - acc: 0.9851     
Epoch 5/5
60000/60000 [==============================] - 2s - loss: 0.0381 - acc: 0.9883     
Out[21]:
<keras.callbacks.History at 0x27a3bbd5c18>

Evaluating the network by testing it on the test examples


In [22]:
test_loss, test_acc = network.evaluate(test_images, test_labels)


 9472/10000 [===========================>..] - ETA: 0s

In [23]:
print('Test acc:', test_acc)


Test acc: 0.9789