Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
import nuimport numpy as np
import keras
from keras.models import Sequential
from keras.datasets import mnist
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.layers.core import Dense, Activation, Dropout, Flatten
from keras.utils import np_utils
model = Sequential()
(X_train, Y_train), (X_test, Y_test) = mnist.load_data()
X_train = X_train.reshape(-1, 28, 28, 1).astype('float32')/255
Y_train = np_utils.to_categorical(Y_train, 10)
X_test = X_test.reshape(-1, 28, 28, 1).astype('float32')/255
Y_test = np_utils.to_categorical(Y_test, 10)
model.add(Convolution2D(nb_filter=32, nb_row=5, nb_col=5,
border_mode='same',
input_shape=(28, 28, 1)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(nb_filter=64, nb_row=5, nb_col=5,
border_mode='same'))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(1024))
model.add(Activation("relu"))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation("softmax"))
model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'])
model.fit(X_train, Y_train, nb_epoch=5, batch_size=100)
loss_and_metrics = model.evaluate(X_test, Y_test, batch_size=32)
print(loss_and_metrics)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment