Sandbox

Use this space to play around with keras models and the MNIST training data!


In [1]:
import numpy as np
import keras
from keras.datasets import mnist # load up the training data!
from keras.models import Sequential # our model
from keras.layers import Dense, Dropout, Flatten # layers we've seen
from keras.layers import Conv2D, MaxPooling2D # new layers
from keras import backend as K # see later


Using TensorFlow backend.

Specify some parameters


In [2]:
batch_size = 128
num_classes = 10
epochs = 10

Load in training data. If you want to do a convolutional layer you'll need to reshape data like in 03-MNIST_CNN.ipynb!


In [3]:
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)


(60000, 784)
(60000,)
(10000, 784)
(10000,)

Build Model

This code defines a very simple model -- add more layers!


In [4]:
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax')) # remember y has 10 categories!

Compile Model

Try playing around with different optimizers, loss functions, and more


In [5]:
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])

Model Fit

Try playing with batch size and epochs


In [6]:
history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test))


Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 7s - loss: 0.4134 - acc: 0.8880 - val_loss: 0.2213 - val_acc: 0.9394
Epoch 2/10
60000/60000 [==============================] - 6s - loss: 0.2088 - acc: 0.9409 - val_loss: 0.1717 - val_acc: 0.9505
Epoch 3/10
60000/60000 [==============================] - 6s - loss: 0.1588 - acc: 0.9553 - val_loss: 0.1338 - val_acc: 0.9623
Epoch 4/10
60000/60000 [==============================] - 7s - loss: 0.1302 - acc: 0.9629 - val_loss: 0.1129 - val_acc: 0.9671
Epoch 5/10
60000/60000 [==============================] - 6s - loss: 0.1113 - acc: 0.9690 - val_loss: 0.1041 - val_acc: 0.9696
Epoch 6/10
60000/60000 [==============================] - 7s - loss: 0.0970 - acc: 0.9720 - val_loss: 0.0936 - val_acc: 0.9724
Epoch 7/10
60000/60000 [==============================] - 6s - loss: 0.0859 - acc: 0.9760 - val_loss: 0.0874 - val_acc: 0.9742
Epoch 8/10
60000/60000 [==============================] - 7s - loss: 0.0775 - acc: 0.9778 - val_loss: 0.0836 - val_acc: 0.9753
Epoch 9/10
60000/60000 [==============================] - 7s - loss: 0.0699 - acc: 0.9801 - val_loss: 0.0765 - val_acc: 0.9770
Epoch 10/10
60000/60000 [==============================] - 7s - loss: 0.0636 - acc: 0.9818 - val_loss: 0.0750 - val_acc: 0.9770

Summarize Model


In [7]:
model.summary()


_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 512)               401920    
_________________________________________________________________
dropout_1 (Dropout)          (None, 512)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                5130      
=================================================================
Total params: 407,050
Trainable params: 407,050
Non-trainable params: 0
_________________________________________________________________