Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<predict_on_test>
%matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep')
 for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
y_pre=model.predict(X_test)
 y_pre=np.round(y_pre ).astype(int ).reshape(1142 )<compute_test_metric>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
print(roc_auc_score(y_pre,y_test))<define_variables>
y_train = train.label
x_train = train.drop('label', 1 )
scores_model = []<compute_test_metric>
train.isnull().values.any()
scores_model.append({'Model': 'SimpleRNN','AUC_Score': roc_auc_score(y_pre,y_test)} )<choose_model_class>
x_train = x_train/255.0
test = test/255.0
model=Sequential()
embedding=Embedding(num_words,100,embeddings_initializer=Constant(embedding_matrix),
input_length=MAX_LEN,trainable=False)
 model.add(embedding)
 model.add(SpatialDropout1D(0.2))
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
optimzer=Adam(learning_rate=1e-5)
 model.compile(loss='binary_crossentropy',optimizer=optimzer,metrics=['accuracy'])
<train_model>
y_train = to_categorical(y_train, num_classes = 10 )
history=model.fit(X_train,y_train,batch_size=4,epochs=10,validation_data=(X_test,y_test),verbose=2 )<predict_on_test>
x_train, x_val, y_train, y_val = train_test_split(x_train, y_train, test_size = 0.1, random_state=2)
y_pre=model.predict(X_test)
 y_pre=np.round(y_pre ).astype(int ).reshape(1142 )<compute_test_metric>
model = Sequential()
model.add(Conv2D(64,kernel_size=5,padding = 'Same',activation='relu',input_shape=(28,28,1)))
 model.add(Conv2D(64,kernel_size=5,padding = 'Same',activation='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.40))
model.add(Conv2D(64,kernel_size=3,padding = 'Same',activation='relu'))
model.add(Conv2D(64,kernel_size=3,padding = 'Same',activation='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.40))
model.add(Flatten())
 model.add(Dense(128, activation = "relu"))
model.add(Dropout(0.40))
model.add(Dense(10, activation = "softmax"))
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] )
print(roc_auc_score(y_pre,y_test))<compute_test_metric>
epochs = 30
batch_size = 100
scores_model.append({'Model': 'LSTM','AUC_Score': roc_auc_score(y_pre,y_test)} )<choose_model_class>
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
rotation_range=10,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False)
 datagen.fit(x_train )
model=Sequential()
embedding=Embedding(num_words,100,embeddings_initializer=Constant(embedding_matrix),
input_length=MAX_LEN,trainable=False)
 model.add(embedding)
 model.add(SpatialDropout1D(0.2))
model.add(GRU(300))
model.add(Dense(1, activation='sigmoid'))
optimzer=Adam(learning_rate=1e-5)
 model.compile(loss='binary_crossentropy',optimizer=optimzer,metrics=['accuracy'])
 model.summary()<train_model>
LR_reduction = ReduceLROnPlateau(monitor='val_accuracy',
patience = 2,
verbose = 1,
factor = 0.5,
min_lr = 0.00001 )
history=model.fit(X_train,y_train,batch_size=8,epochs=10,validation_data=(X_test,y_test),verbose=2 )<predict_on_test>
model.fit_generator(datagen.flow(x_train, y_train, batch_size = batch_size),
epochs = epochs, validation_data =(x_val,y_val), steps_per_epoch=x_train.shape[0] // batch_size, callbacks=[LR_reduction] )
<choose_model_class><EOS> 
results = model.predict(test)
 results = np.argmax(results, axis = 1)
 results = pd.Series(results, name="Label")
 submission = pd.concat([pd.Series(range(1,28001), name = "ImageId"), results], axis = 1)
 submission.to_csv("submission.csv", index=False )