Kaggle competition: Digit Recognizer
model = Sequential()
model.add(tf.keras.applications.resnet50.ResNet50(input_shape =(75, 75, 3),
pooling = 'avg',
include_top = False,
weights = 'imagenet'))
model.add(L.Flatten())
 model.add(L.Dense(128, activation='relu'))
model.add(L.Dense(10, activation='softmax'))
model.compile(optimizer=RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0), loss='categorical_crossentropy', metrics=['accuracy'])
 learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001)
<load_pretrained>
seed = 42
np.random.seed(seed )
for layer in model.layers[0].layers:
if layer.name == 'conv5_block1_0_conv':
break
layer.trainable=False<train_model>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )
history = model.fit(train_generator, validation_data=valid_generator, epochs=20,
steps_per_epoch=train_generator.n//train_generator.batch_size,
validation_steps=valid_generator.n//valid_generator.batch_size,
callbacks=[learning_rate_reduction] )<feature_engineering>
train.isnull().any().sum()
test = test/255<save_to_csv>
test.isnull().any().sum()
res = model.predict(test[:])
 output = pd.DataFrame({'ImageId':[ i+1 for i in range(len(res)) ],
'Label': [ xi.argmax() for xi in res]})
 output.to_csv('submission_grid.csv', index=False )<set_options>
X = train.iloc[:, 1:]
y = train.iloc[:, 0]
warnings.filterwarnings("ignore")
 %matplotlib inline
np.random.seed(2)
 sns.set(style='white', context='notebook', palette='deep' )<load_from_csv>
X = X.values.reshape(-1, 28, 28, 1)
 test = test.values.reshape(-1, 28, 28, 1 )
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 sub = pd.read_csv('.. /input/digit-recognizer/sample_submission.csv')
 print("Data are Ready!!" )<train_model>
def normalize(arr):
return(arr - np.mean(arr)) / np.std(arr )
print(f"Training data size is {train.shape}
Testing data size is {test.shape}" )<prepare_x_and_y>
X = normalize(X)
 test = normalize(test )
Y_train = train["label"]
X_train = train.drop(labels = ["label"], axis = 1 )<concatenate>
mean, std = np.mean(X), np.std(X)
 print('Mean: %.3f, Standard Deviation: %.3f' %(mean, std))
( x_train1, y_train1),(x_test1, y_test1)= mnist.load_data()
train1 = np.concatenate([x_train1, x_test1], axis=0)
 y_train1 = np.concatenate([y_train1, y_test1], axis=0)
 Y_train1 = y_train1
X_train1 = train1.reshape(-1, 28*28 )<feature_engineering>
datagen = ImageDataGenerator(
 rotation_range=20,
zoom_range = 0.1,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=False,
vertical_flip=False )
X_train = X_train / 255.0
test = test / 255.0
X_train1 = X_train1 / 255.0<concatenate>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15 )
X_train = np.concatenate(( X_train.values, X_train1))
Y_train = np.concatenate(( Y_train, Y_train1))<categorify>
datagen.fit(X_train )
Y_train = to_categorical(Y_train, num_classes = 10 )<split>
y_train = to_categorical(y_train, num_classes=10)
 y_test = to_categorical(y_test, num_classes=10 )
X_train, X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size = 0.1, random_state=2 )<choose_model_class>
model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', strides=1, padding='same', data_format='channels_last',
input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2), strides=1, padding='valid'))
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', strides=1, padding='same', data_format='channels_last'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2), strides=1, padding='valid'))
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', strides=1, padding='same', data_format='channels_last',
input_shape=(28,28,1)))
 model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2), strides=1, padding='valid'))
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', strides=1, padding='same', data_format='channels_last'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2), strides=1, padding='valid'))
model.add(Flatten())
 model.add(Dense(1024, activation='relu'))
model.add(BatchNormalization())
 model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
 model.add(Dense(10, activation='softmax'))
model = Sequential()
model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu', input_shape =(28,28,1)))
 model.add(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(5,5),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 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(BatchNormalization())
 model.add(Conv2D(filters = 64, kernel_size =(3,3),padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))
 model.add(Dropout(0.25))
model.add(Conv2D(filters = 64, kernel_size =(3,3), padding = 'Same', activation ='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Flatten())
 model.add(Dense(256, activation = "relu"))
model.add(BatchNormalization())
 model.add(Dropout(0.25))
model.add(Dense(10, activation = "softmax"))<choose_model_class>
optimizer = Adam(lr=0.001, beta_1=0.9, beta_2=0.999 )
optimizer = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0 )<choose_model_class>
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['categorical_accuracy'] )
model.compile(optimizer = optimizer , loss = "categorical_crossentropy", metrics=["accuracy"] )<choose_model_class>
reduce_lr = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x )
learning_rate_reduction = ReduceLROnPlateau(monitor='val_acc',
patience=3,
verbose=1,
factor=0.5,
min_lr=0.00001 )<define_variables>
batch_size = 64
epochs = 50
epochs = 50
batch_size = 128<define_variables>
history = model.fit_generator(datagen.flow(X_train, y_train, batch_size = batch_size), epochs = epochs,
validation_data =(X_test, y_test), verbose=1,
steps_per_epoch=X_train.shape[0] // batch_size,
callbacks = [reduce_lr] )
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)
 train_gen = datagen.flow(X_train,Y_train, batch_size=batch_size )<train_model>
preds = np.argmax(model.predict(test), axis=1)
 sub_df = {'ImageId':list(range(1, len(test)+ 1)) ,'Label':preds}
submission = pd.DataFrame(sub_df ).astype('int')
 submission.head()
history = model.fit(train_gen,
epochs = epochs,validation_data =(X_val,Y_val),
verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size
, callbacks=[learning_rate_reduction],
validation_steps = X_val.shape[0] // batch_size )<compute_test_metric>
submission.to_csv('submission.csv', index=False )
<compute_train_metric><EOS> 
submission.to_csv('submission.csv', index=False )