application_vnd.jupyter.stderr_output_1.png
from keras.layers import Dense,Dropout, Activation,Lambda,Flatten

from keras.models import Sequential

from keras.optimizers import Adam , RMSprop

from keras.preprocessing import image

from keras.utils.np_utils import to_categorical

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)

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

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

x_train = train[:, 1:].values.astype('float32')
y_train = train[:, 0].values.astype('int32')
x_test = test.values.astype('float32')

x_train.shape

y_train.shape

x_train = x_train.reshape(x_train.shape[0], 28, 28)

x_train.shape

x_train = x_train.reshape(x_train.shape[0], 28, 28, 1)
x_test = x_test.reshape(x_test.shape[0], 28, 28, 1)

(x_train.shape, x_test.shape)

from keras.utils.np_utils import to_categorical
y_train = to_categorical(y_train, num_classes=10)

y_train.shape

mean_px = x_train.mean().astype(np.float32)
std_px = x_train.std().astype(np.float32)
def standardize(x):
    return (x - mean_px) / std_px

x_train.reshape

model = Sequential()
model.add(Lambda(standardize, input_shape=(28, 28, 1)))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))

model.summary()

model.compile(optimizer=RMSprop(lr=0.001), loss='categorical_crossentropy', metrics=['accuracy'])

from keras.preprocessing import image
gen = image.ImageDataGenerator()

X_train, X_val, Y_train, Y_val = train_test_split(x_train, y_train, test_size=0.1, random_state=34)
batches = gen.flow(X_train, Y_train, batch_size=64)
val_batches = gen.flow(X_val, Y_val, batch_size=64)

cache = model.fit_generator(batches, batches.n, nb_epoch=1, validation_data=val_batches, nb_val_samples=val_batches.n)

model.optimizer.lr = 0.01
gen = image.ImageDataGenerator()
batches = gen.flow(X_train, Y_train, batch_size=64)
history = model.fit_generator(batches, batches.n, nb_epoch=1)

preds = model.predict_classes(x_test, verbose=0)

subs = pd.DataFrame({'ImageId': list(range(1, len(preds) + 1)), 'Label': preds})
subs.to_csv('sub1.csv', index=False, header=True)