text_plain_output_2.png application_vnd.jupyter.stderr_output_1.png
from keras.callbacks import ModelCheckpoint

from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense

from keras.layers import Dense, Dropout

from keras.models import Sequential

from keras.utils import np_utils

from sklearn.model_selection import train_test_split

import numpy as np # linear algebra

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

from sklearn.model_selection import train_test_split
seed = 5
np.random.seed(seed)
data_train = pd.read_csv('../input/train.csv')
X = data_train.drop('label', axis=1)
y = data_train['label']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=seed)

X_train_matrix = X_train_matrix.astype('float32') / 255
X_test_matrix = X_test_matrix.astype('float32') / 255

from keras.utils import np_utils
y_train_matrix = np_utils.to_categorical(y_train_matrix, 10)
y_test_matrix = np_utils.to_categorical(y_test_matrix, 10)

from keras.models import Sequential
from keras.layers import Dense, Dropout
model = Sequential()
model.add(Dense(784, input_shape=(len(X_train_matrix[0]),)))
model.add(Dense(392, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(196, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))
model.summary()

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
score = model.evaluate(X_test_matrix, y_test_matrix, verbose=0)
accuracy = score[1] * 100

history = model.fit(X_train_matrix, y_train_matrix, batch_size=100, epochs=6, verbose=1)

from keras.callbacks import ModelCheckpoint
cb_checkpoint = ModelCheckpoint(filepath='best-model.hdf5', verbose=1, save_best_only=True)
history = model.fit(X_train_matrix, y_train_matrix, batch_size=400, epochs=6, validation_split=0.2, shuffle=True, callbacks=[cb_checkpoint], verbose=1)

model.load_weights('best-model.hdf5')
score = model.evaluate(X_test_matrix, y_test_matrix, verbose=0)
accuracy = score[1] * 100

X_train = np.array(list(map(lambda x: np.reshape(x, (28, 28, 1)), X_train_matrix)))
X_test = np.array(list(map(lambda x: np.reshape(x, (28, 28, 1)), X_test_matrix)))

from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=5, padding='same', activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=2))
model.add(Conv2D(filters=64, kernel_size=5, padding='same', activation='relu'))
model.add(MaxPooling2D(pool_size=2))
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(10, activation='softmax'))
model.summary()

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
score = model.evaluate(X_test, y_test_matrix, verbose=0)
accuracy = score[1] * 100

cb_checkpoint = ModelCheckpoint(filepath='best-cnn-model.hdf5', verbose=1, save_best_only=True)
history = model.fit(X_train, y_train_matrix, batch_size=50, epochs=6, validation_split=0.2, shuffle=True, callbacks=[cb_checkpoint], verbose=1)