Cainvas
In [16]:
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
Using TensorFlow backend.
In [17]:
(x_train, y_train), (x_test, y_test) = mnist.load_data()
Downloading data from https://s3.amazonaws.com/img-datasets/mnist.npz
11493376/11490434 [==============================] - 2s 0us/step
In [18]:
print(x_train.shape, y_train.shape)
(60000, 28, 28) (60000,)
In [19]:
x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)
input_shape = (28, 28, 1)
In [20]:
y_train = keras.utils.to_categorical(y_train, 10)
y_test = keras.utils.to_categorical(y_test, 10)
In [21]:
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
x_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
In [22]:
batch_size = 128
num_classes = 10
epochs = 10
In [23]:
model = Sequential()
model.add(Conv2D(32, kernel_size=(5, 5),activation='relu',input_shape=input_shape))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))
In [24]:
model.compile(loss=keras.losses.categorical_crossentropy,optimizer=keras.optimizers.Adadelta(),metrics=['accuracy'])
In [25]:
hist = model.fit(x_train, y_train,batch_size=batch_size,epochs=epochs,verbose=1,validation_data=(x_test, y_test))
print("The model has successfully trained")
Train on 60000 samples, validate on 10000 samples
Epoch 1/10
60000/60000 [==============================] - 20s 338us/step - loss: 0.3839 - accuracy: 0.8817 - val_loss: 0.0699 - val_accuracy: 0.9801
Epoch 2/10
60000/60000 [==============================] - 20s 332us/step - loss: 0.1094 - accuracy: 0.9710 - val_loss: 0.0412 - val_accuracy: 0.9879
Epoch 3/10
60000/60000 [==============================] - 20s 333us/step - loss: 0.0804 - accuracy: 0.9792 - val_loss: 0.0302 - val_accuracy: 0.9909
Epoch 4/10
60000/60000 [==============================] - 20s 331us/step - loss: 0.0659 - accuracy: 0.9833 - val_loss: 0.0334 - val_accuracy: 0.9900
Epoch 5/10
60000/60000 [==============================] - 20s 331us/step - loss: 0.0557 - accuracy: 0.9861 - val_loss: 0.0277 - val_accuracy: 0.9919
Epoch 6/10
60000/60000 [==============================] - 20s 332us/step - loss: 0.0455 - accuracy: 0.9886 - val_loss: 0.0278 - val_accuracy: 0.9919
Epoch 7/10
60000/60000 [==============================] - 20s 330us/step - loss: 0.0401 - accuracy: 0.9898 - val_loss: 0.0302 - val_accuracy: 0.9915
Epoch 8/10
60000/60000 [==============================] - 20s 331us/step - loss: 0.0370 - accuracy: 0.9908 - val_loss: 0.0370 - val_accuracy: 0.9909
Epoch 9/10
60000/60000 [==============================] - 20s 331us/step - loss: 0.0340 - accuracy: 0.9914 - val_loss: 0.0240 - val_accuracy: 0.9927
Epoch 10/10
60000/60000 [==============================] - 20s 331us/step - loss: 0.0287 - accuracy: 0.9929 - val_loss: 0.0253 - val_accuracy: 0.9930
The model has successfully trained
In [26]:
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
Test loss: 0.02530691795379312
Test accuracy: 0.9929999709129333
In [27]:
model.save('mnist.h5')
print("Saving the model as mnist.h5")
Saving the model as mnist.h5