Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<split>
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPool2D
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
X_train, X_test, y_train, y_test = train_test_split(padded_sentences,
train_label,
test_size=0.25,
random_state=42,
shuffle=True)
<train_model>
training_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
def training(model, model_name):
checkpoint = ModelCheckpoint(model_name + '.h5', monitor = 'val_loss', verbose = 1, save_best_only = True)
 reduce_lr = ReduceLROnPlateau(monitor = 'val_loss', factor = 0.2, verbose = 1, patience = 5, min_lr = 0.001)
 early_stop = EarlyStopping(monitor='val_loss', patience=1)
 start_time = time.time()
history = model.fit(X_train, y_train,
epochs = 100,
batch_size = 32,
validation_data = [X_test, y_test],
verbose = 1,
callbacks = [reduce_lr, checkpoint, early_stop])
 end_time = time.time()
print("
=>Training time :", round(end_time - start_time, 1), 's')
 model.load_weights(model_name + '.h5')
 start_time = time.time()
preds = model.predict_classes(X_test)
 end_time = time.time()
print('
=>Average Inference Time :', round(( end_time - start_time)/ len(X_test)* 1000, 1), 'ms')
 print('
=>Model Size :', round(os.stat(model_name + '.h5' ).st_size /(1024 ** 2), 1), 'MB')
 metrics(y_test, preds)
 plot(history, [['loss', 'val_loss'],['accuracy', 'val_accuracy']] )<choose_model_class>
X_train = training_data.drop('label', axis=1 ).values
y_train = training_data[['label']].values
X_test = test_data.values
test_data.shape
def CNN() :
model = Sequential()
model.add(Embedding(input_dim=embedding_matrix.shape[0],
output_dim=embedding_matrix.shape[1],
weights=[embedding_matrix],
input_length=length_long_sentence))
model.add(Conv1D(filters=32, kernel_size=8, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
 model.add(Dense(10, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 return model
model = CNN()
model.summary()<train_model>
batch_size = 128
num_classes = 10
epochs = 12
img_rows, img_cols = 28, 28
input_shape =(1, img_rows, img_cols )
training(model, 'model_cnn' )<choose_model_class>
X_train = X_train.reshape(-1,28,28,1)
 X_test = X_test.reshape(-1,28,28,1 )
def RNN() :
model = Sequential()
model.add(Embedding(input_dim=embedding_matrix.shape[0],
output_dim=embedding_matrix.shape[1],
weights=[embedding_matrix],
input_length=length_long_sentence))
model.add(Bidirectional(SimpleRNN(length_long_sentence, return_sequences = True, recurrent_dropout=0.2)))
 model.add(GlobalMaxPool1D())
 model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(length_long_sentence, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(length_long_sentence, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(1, activation = 'sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 return model
model = RNN()
model.summary()<train_model>
X_train = X_train.astype('float32')
 X_test = X_test.astype('float32')
 X_train /= 255
X_test /= 255
print('x_train shape:', X_train.shape)
 print(X_train.shape[0], 'train samples')
 print(X_test.shape[0], 'test samples' )
training(model, 'model_rnn' )<choose_model_class>
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, stratify=y_train,
test_size=0.15, random_state=42 )
def BiGRU() :
model = Sequential()
model.add(Embedding(input_dim=embedding_matrix.shape[0],
output_dim=embedding_matrix.shape[1],
weights=[embedding_matrix],
input_length=length_long_sentence))
model.add(Bidirectional(GRU(length_long_sentence, return_sequences = True, recurrent_dropout=0.2)))
 model.add(GlobalMaxPool1D())
 model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(length_long_sentence, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(length_long_sentence, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(1, activation = 'sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 return model
model = BiGRU()
model.summary()<train_model>
y_train = keras.utils.to_categorical(y_train, num_classes)
 y_val = keras.utils.to_categorical(y_val, num_classes)
 y_train.shape, y_val.shape
training(model, 'model_bigru' )<choose_model_class>
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu', input_shape =(28,28,1)))
 model.add(Conv2D(filters = 32, kernel_size =(5,5),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same',
activation ='relu'))
model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(10, activation = "softmax"))
def BiLSTM() :
model = Sequential()
model.add(Embedding(input_dim=embedding_matrix.shape[0],
output_dim=embedding_matrix.shape[1],
weights=[embedding_matrix],
input_length=length_long_sentence))
model.add(Bidirectional(LSTM(length_long_sentence, return_sequences = True, recurrent_dropout=0.2)))
 model.add(GlobalMaxPool1D())
 model.add(BatchNormalization())
 model.add(Dropout(0.5))
model.add(Dense(length_long_sentence, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(length_long_sentence, activation = "relu"))
model.add(Dropout(0.5))
model.add(Dense(1, activation = 'sigmoid'))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
 return model
model = BiLSTM()
model.summary()<train_model>
optimizer = keras.optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)
 model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'] )
training(model, 'model_bilstm' )<save_to_csv>
model.fit(X_train, y_train,
batch_size=batch_size,
epochs=epochs,
verbose=1,
validation_data=(X_val, y_val))
submission.target = model.predict_classes(test_sentences)
 submission.to_csv("submission.csv", index=False )<import_modules>
score = model.evaluate(X_val, y_val, verbose=0)
 score
import tensorflow as tf
from tensorflow import keras
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt<load_from_csv>
results = model.predict(X_test)
 results = np.argmax(results, axis = 1)
 results = pd.Series(results, name="Label" )
<count_values><EOS> 
submission = pd.concat([pd.Series(range(1,28001),name = "ImageId"), results],axis = 1)
 submission.to_csv("cnn_mnist.csv",index=False )