image_output_1.png
from keras.layers import Conv2D, MaxPooling2D, Dropout, Activation, Flatten, Dense

from keras.models import Sequential

from keras.optimizers import Adam

from sklearn.preprocessing import LabelBinarizer

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

train = pd.read_csv('../input/train.csv')
test = pd.read_csv('../input/test.csv')

num_training = len(train.values)
num_testing = len(test.values)

y_train = np.array(train.pop('label').values)
x_train = np.array(train.values)
x_test = np.array(test.values)

x_train = x_train.reshape(num_training, 28, 28)
x_test = x_test.reshape(num_testing, 28, 28)

x_train = x_train / 255.0
x_test = x_test / 255.0

from sklearn.preprocessing import LabelBinarizer
y_train_hot = LabelBinarizer().fit_transform(y_train)

model = Sequential()
model.add(Conv2D(6, 5, 5, input_shape=(28, 28, 1), bias=True, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode='same'))
model.add(Dropout(p=0.12))
model.add(Conv2D(16, 5, 5, bias=True, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode='same'))
model.add(Conv2D(35, 5, 5, bias=True, border_mode='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2), border_mode='same'))
model.add(Flatten())
model.add(Dense(120, bias=True))
model.add(Activation('relu'))
model.add(Dropout(p=0.5))
model.add(Dense(84, bias=True))
model.add(Activation('relu'))
model.add(Dense(10, bias=True))
model.add(Activation('softmax'))
model.compile(optimizer=Adam(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'], decay=1)

x_train = np.expand_dims(x_train, axis=4)
x_test = np.expand_dims(x_test, axis=4)

training_hist = model.fit(x_train, y_train_hot, nb_epoch=16, batch_size=64, verbose=2, validation_split=0.23)