Kaggle competition: Kannada MNIST
<SOS> metric: CategorizationAccuracy Kaggle data source: kannada-mnist<choose_model_class>
plt.rcParams['figure.figsize'] =(12, 8)
 plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
K.set_image_data_format('channels_last')
 random_state = 42
image_shape =(28, 28, 1 )
def model_gru_srk_atten(embedding_matrix):
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix] )(inp)
 x = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)
 x = Attention(maxlen )(x)
 x = Dense(16, activation="relu" )(x)
 x = Dropout(0.1 )(x)
 x = Dense(1, activation="sigmoid" )(x)
 model = Model(inputs=inp, outputs=x)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model 
<choose_model_class>
source_df = pd.read_csv('.. /input/Kannada-MNIST/train.csv')
 test_df = pd.read_csv('.. /input/Kannada-MNIST/test.csv')
 dig_df = pd.read_csv('.. /input/Kannada-MNIST/Dig-MNIST.csv' )
def model_lstm_du(embedding_matrix):
inp = Input(shape=(maxlen,))
x = Embedding(max_features, embed_size, weights=[embedding_matrix] )(inp)
 x = Bidirectional(CuDNNGRU(64, return_sequences=True))(x)
 avg_pool = GlobalAveragePooling1D()(x)
 max_pool = GlobalMaxPooling1D()(x)
 conc = concatenate([avg_pool, max_pool])
 conc = Dense(64, activation="relu" )(conc)
 conc = Dropout(0.1 )(conc)
 outp = Dense(1, activation="sigmoid" )(conc)
 model = Model(inputs=inp, outputs=outp)
 model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
 return model<find_best_params>
def get_X_Y(df):
X = df.loc[:, 'pixel0':'pixel783'].copy()
Y = df.loc[:, 'label'].copy()
return(X, Y)
 source_X, source_Y = get_X_Y(source_df)
 dig_X, dig_Y = get_X_Y(dig_df)
 print('source_X shape {}, source_Y shape {}'.format(source_X.shape, source_Y.shape))
def train_pred(model, epochs=2):
for e in range(epochs):
model.fit(train_X, train_y, batch_size=512, epochs=1, validation_data=(val_X, val_y))
pred_val_y = model.predict([val_X], batch_size=1024, verbose=0)
 best_thresh = 0.5
best_score = 0.0
for thresh in np.arange(0.1, 0.501, 0.01):
thresh = np.round(thresh, 2)
 score = metrics.f1_score(val_y,(pred_val_y > thresh ).astype(int))
if score > best_score:
best_thresh = thresh
best_score = score
print("Val F1 Score: {:.4f}".format(best_score))
pred_test_y = model.predict([test_X], batch_size=1024, verbose=0)
 return pred_val_y, pred_test_y, best_score<split>
train_X, valid_X, train_Y, valid_Y = train_test_split(
 source_X, source_Y, test_size=0.2, stratify=source_Y, random_state=random_state)
 print('train_X shape {}, train_Y shape {}, valid_X shape {}, valid_Y shape {}'.format(
 train_X.shape, train_Y.shape, valid_X.shape, valid_Y.shape))
train_X, val_X, test_X, train_y, val_y, word_index = load_and_prec()
embedding_matrix_1 = load_glove(word_index)
 embedding_matrix_2 = load_fasttext(word_index)
 embedding_matrix_3 = load_para(word_index )<define_variables>
def build_cnn(input_shape, classes):
input_layer = Input(shape=input_shape)
 conv_layer1 = Conv2D(32, kernel_size=(3,3), strides=1, padding='same', input_shape=(28, 28, 1))(input_layer)
 conv_layer1 = Conv2D(16, kernel_size=(3,3), strides=1, padding='same' )(conv_layer1)
 batch_norm_layer1 = BatchNormalization(momentum=0.5, gamma_initializer='uniform' )(conv_layer1)
 relu_layer1 = LeakyReLU()(batch_norm_layer1)
 max_pool_layer1 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(relu_layer1)
 con_drop_layer1 = Dropout(0.5 )(max_pool_layer1)
 conv_layer2 = Conv2D(64, kernel_size=(3,3), strides=1, padding='same' )(con_drop_layer1)
 conv_layer2 = Conv2D(32, kernel_size=(3,3), strides=1, padding='same' )(conv_layer2)
 batch_norm_layer2 = BatchNormalization(momentum=0.5, gamma_initializer='uniform' )(conv_layer2)
 relu_layer2 = LeakyReLU()(batch_norm_layer2)
 max_pool_layer2 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(relu_layer2)
 con_drop_layer2 = Dropout(0.5 )(max_pool_layer2)
 conv_layer3 = Conv2D(128, kernel_size=(3,3), strides=1, padding='same' )(con_drop_layer2)
 conv_layer3 = Conv2D(64, kernel_size=(3,3), strides=1, padding='same' )(conv_layer3)
 batch_norm_layer3 = BatchNormalization(momentum=0.5, gamma_initializer='uniform' )(conv_layer3)
 relu_layer3 = LeakyReLU()(batch_norm_layer3)
 max_pool_layer3 = MaxPooling2D(pool_size=(2,2), strides=(2,2))(relu_layer3)
 flatten = Flatten()(max_pool_layer3)
 dropout_layer1 = Dropout(0.5 )(flatten)
 dense_layer1 = Dense(256 )(dropout_layer1)
 relu_layer4 = LeakyReLU()(dense_layer1)
 dropout_layer2 = Dropout(0.5 )(relu_layer4)
 dense_layer2 = Dense(256 )(dropout_layer2)
 relu_layer5 = LeakyReLU()(dense_layer2)
 dropout_layer3 = Dropout(0.5 )(relu_layer5)
 output_layer = Dense(10, activation='softmax' )(dropout_layer3)
 model = Model(inputs=[input_layer], outputs=[output_layer])
 model.compile('adam', loss='categorical_crossentropy', metrics=['accuracy'])
 return model
cnn_model = build_cnn(image_shape, 10)
 cnn_model.summary()
<predict_on_test><EOS> 
model_count = 6
epochs = 25
batch_size = 128
models = [build_cnn(image_shape, num_classes)for _ in range(model_count)]
kfold_splitter = StratifiedKFold(n_splits=model_count, shuffle=True, random_state=random_state)
 histories = []
for index,(train_index, valid_index)in enumerate(kfold_splitter.split(source_X, source_Y)) :
print('Start model {} training'.format(index + 1))
k_fold_train_X, k_fold_train_Y = source_X.iloc[train_index], source_Y.iloc[train_index]
k_fold_valid_X, k_fold_valid_Y = source_X.iloc[valid_index], source_Y.iloc[valid_index]
k_fold_train_X_prep, k_fold_valid_X_prep = preproc_images(k_fold_train_X), preproc_images(k_fold_valid_X)
 run_model(
 models[index], k_fold_train_X_prep, to_categorical(k_fold_train_Y), batch_size, number=index,
epochs=epochs, validation_data=(k_fold_valid_X_prep, to_categorical(k_fold_valid_Y)) 
)