Kaggle competition: Digit Recognizer
def get_fitted_data_generator(data):
datagen = ImageDataGenerator(
 featurewise_center=False,
samplewise_center=False,
featurewise_std_normalization=False,
samplewise_std_normalization=False,
zca_whitening=False,
zoom_range = 0.1,
height_shift_range=0.1, 
)
 datagen.fit(data)
 return datagen
def fit_model_generator(model, X_train, y_train, epochs=1, batch=32, X_val=None, y_val=None):
image_nbr = np.size(X_train, 0)
 training_data_generator = get_fitted_data_generator(X_train)
 es = callbacks.EarlyStopping(monitor='val_loss', patience=5, verbose=1, restore_best_weights=True)
 rlp = callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=2, min_lr=1e-10, mode='min', verbose=1)
 return model.fit_generator(training_data_generator.flow(X_train, y_train, batch_size=batch), steps_per_epoch=(image_nbr//batch),callbacks=[es, rlp],epochs=epochs, validation_data=(X_val, y_val), verbose=1 )<train_model>
train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')
 test_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv' )
full_data_model = fit_model_generator(model, X_train, y_train, epochs=150,X_val=X_val,y_val=y_val )<predict_on_test>
from matplotlib import pyplot as plt
testing_data = pd.read_csv('.. /input/digit-recognizer/test.csv' ).values
testing_data = prepare_data(testing_data)
 def get_predictions(model, data):
return np.array([np.argmax(prediction)for prediction in model.predict(data)])
 final_predictions = get_predictions(model, testing_data )<save_to_csv>
y_train = train_df.iloc[:,0]
X_train = train_df.iloc[:,1:]
X_test = test_df.copy()
submission_filename = 'submission.csv'
answers = pd.DataFrame({'ImageId':range(1, final_predictions.size + 1),'Label':final_predictions})
 answers.to_csv(submission_filename, index=False )<save_to_csv>
X_train = X_train / 255.0
X_test = X_test/ 255.0
X_train_im = X_train.values.reshape(-1,28,28,1)
 X_test_im = X_test.values.reshape(-1,28,28,1 )
submission_filename = '/kaggle/working/submission.csv'
answers.to_csv(submission_filename, index=False )<load_from_csv>
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.callbacks import EarlyStopping
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten,Dense,Dropout
train_data = pd.read_csv("/kaggle/input/digit-recognizer/train.csv")
 test_data = pd.read_csv("/kaggle/input/digit-recognizer/test.csv")
 print(train_data.head() )<data_type_conversions>
model = Sequential()
model.add(Conv2D(64 , 7 ,activation='relu', padding = "same" ,input_shape=[28,28,1]))
model.add(MaxPooling2D(2))
model.add(Conv2D(128 , 3,activation='relu', padding = "same"))
model.add(Conv2D(128 , 3,activation='relu', padding = "same"))
model.add(MaxPooling2D(2))
model.add(Conv2D(256 , 3,activation='relu', padding = "same"))
model.add(Conv2D(256 , 3,activation='relu', padding = "same"))
model.add(MaxPooling2D(2))
model.add(Flatten())
 model.add(Dense(128,activation='elu',kernel_initializer="he_normal"))
model.add(Dropout(0.5))
model.add(Dense(64,activation='elu',kernel_initializer="he_normal",kernel_regularizer=keras.regularizers.l2(0.01)))
 model.add(Dropout(0.5))
model.add(Dense(64,activation='softmax'))
X_train = train_data.drop('label',axis=1)
 X_train = X_train.values
X_train = X_train.astype('float32')
 y_train = train_data['label']
y_train = y_train.values
y_train = y_train.astype('float32')
 X_test = test_data.astype('float32')
 print("The MNIST dataset has a training set of %d examples." % len(X_train))
print("The MNIST database has a test set of %d examples." % len(X_test))<data_type_conversions>
def exponential_decay(lr0, s):
def exponential_decay_fn(epoch):
return lr0 * 0.1**(epoch / s)
 return exponential_decay_fn
exponential_decay_fn = exponential_decay(lr0=0.01, s=20)
 lr_scheduler = keras.callbacks.LearningRateScheduler(exponential_decay_fn)
 optimizer=keras.optimizers.SGD(lr=0.01,momentum=0.9, nesterov=True)
 model.compile(loss="sparse_categorical_crossentropy",optimizer=optimizer,metrics=['accuracy'] )
X_train = X_train.astype('float32')/255
X_test = X_test.astype('float32')/255<train_model>
checkpoint_cb = keras.callbacks.ModelCheckpoint("my_keras_model.h5",save_best_only=True)
 history = model.fit(X_train_im, y_train, epochs=100,batch_size=32,validation_split=0.1,callbacks=[EarlyStopping(patience=5),lr_scheduler,checkpoint_cb] )
datagen_train = ImageDataGenerator(
 width_shift_range=0.2,
height_shift_range=0.2,
rotation_range = 20,
horizontal_flip=True,
vertical_flip = True,
shear_range = 0.1)
 datagen_train.fit(X_train )<split>
model = keras.models.load_model("my_keras_model.h5")
random_seed = 46
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.25, random_state=random_seed)
 print('Integer-valued labels:')
 print(y_train[:10])
 y_train = np_utils.to_categorical(y_train, 10)
 y_val = np_utils.to_categorical(y_val, 10)
 print('One-hot labels:')
 print(y_train[:10] )<choose_model_class>
y_test = model.predict(X_test_im )
model = Sequential()
model.add(Conv2D(32,(5, 5), padding='same',
input_shape=X_train.shape[1:]))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(Conv2D(32,(5, 5), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.3))
model.add(Conv2D(64,(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(Conv2D(64,(3, 3), padding='same'))
model.add(Activation('relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2)))
 model.add(Dropout(0.3))
model.add(Flatten())
 model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Activation('softmax'))
optimizer = optimizers.Adam(lr=0.001)
 model.compile(loss='categorical_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
 model.summary()
print("input shape ",model.input_shape)
 print("output shape ",model.output_shape )<init_hyperparams>
submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv' )
batch_size = 128
nb_epoch = 20
checkpointer = [
ReduceLROnPlateau(monitor='val_loss',
patience=3,
verbose=1,
factor=0.5,
min_lr = 0.00001,
cooldown=0),
ModelCheckpoint('mnist_model_best.hdf5',
monitor='val_accuracy',
save_best_only=True,
mode='max',
verbose=1)
 ]
hist = model.fit(X_train, y_train, epochs=nb_epoch, batch_size=batch_size, validation_data=(X_val,y_val), callbacks=[checkpointer],verbose=1, shuffle=True )<train_model>
submission.to_csv("cnn_mnist_datagen.csv",index=False)