Kaggle competition: Digit Recognizer
history = model.fit(x_train,y_train,batch_size=64,epochs=15,validation_data=(x_val,y_val),shuffle=True )<predict_on_test>
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten,Dropout,Conv2D,MaxPooling2D, BatchNormalization
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import EarlyStopping, Callback
from keras.optimizers import Adam
from sklearn.model_selection import train_test_split
import sklearn.metrics as metrics
import random
import matplotlib.pyplot as plt
import seaborn as sns
val_pred = model.predict(x_val )<prepare_output>
print(tf.config.list_physical_devices('GPU'),'//',tf.test.is_built_with_cuda() )
val_pred1 = np.argmax(val_pred, axis=1 )<predict_on_test>
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv')
 train.head()
predictions = model.predict(test_image )<prepare_output>
print('Nº of missing values in train set: ', train.isnull().any().sum())
 print()
print('Nº of missing values in test set: ', test.isnull().any().sum() )
submission = pd.DataFrame({'ImageId' : range(1,28001), 'Label' : list(subs)})
 submission.head(10)
 submission.shape<save_to_csv>
X = np.array(train.drop('label',axis=1)) / 255.
 X = X.reshape(( -1,28,28,1))
y = np.array(train['label'] )
submission.to_csv("submission1.csv", index = False )<set_options>
train_X, test_X, train_y, test_y = train_test_split(X, y, test_size=0.2)
 print('X_train: ' + str(train_X.shape))
print('Y_train: ' + str(train_y.shape))
print('X_test: ' + str(test_X.shape))
print('Y_test: ' + str(test_y.shape))
!nvidia-smi<set_options>
batch_size = 128
epochs = 110
epochs_to_wait_to_improve = 10
num_classes = max(pd.unique(train['label'])) +1
seed = 7
random.seed(seed )
%matplotlib inline
sns.set(style='white', context='notebook', palette='deep')
 np.random.seed(2 )<load_from_csv>
datagen = ImageDataGenerator(
 rotation_range=12,
width_shift_range=0.11,
height_shift_range=0.11,
shear_range=0.15,
zoom_range = 0.09,
validation_split=0.3,
horizontal_flip=False,
vertical_flip=False
)
train = pd.read_csv('.. /input/digit-recognizer/train.csv')
 test = pd.read_csv('.. /input/digit-recognizer/test.csv' )<prepare_x_and_y>
train_generator = datagen.flow(train_X,
train_y,
batch_size=batch_size,
shuffle=True,
subset='training')
 val_generator = datagen.flow(test_X,
test_y,
batch_size=batch_size,
subset='validation' )
y_train = train["label"]
X_train = train.drop(labels=["label"], axis = 1 )<train_model>
model = Sequential()
model.add(Conv2D(32, kernel_size =(3,3), input_shape=(28, 28, 1), padding = 'Same', activation='relu'))
model.add(Conv2D(64, kernel_size =(3,3), padding = 'Same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2), strides=1, padding='valid'))
model.add(Dropout(0.25))
model.add(Conv2D(64, kernel_size =(3,3), padding = 'Same', activation='relu'))
model.add(BatchNormalization())
 model.add(MaxPooling2D(pool_size=(2, 2), strides=1, padding='valid'))
model.add(Dropout(0.2))
model.add(Flatten())
 model.add(Dense(512, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.20))
model.add(Dense(128, activation='relu'))
model.add(BatchNormalization())
 model.add(Dropout(0.35))
model.add(Dense(num_classes, activation='softmax'))
( X_train1, y_train1),(X_test1, y_test1)= mnist.load_data()
X_train1 = np.concatenate([X_train1, X_test1], axis=0)
 y_train1 = np.concatenate([y_train1, y_test1], axis=0)
 X_train1 = X_train1.reshape(-1, 28*28 )<feature_engineering>
class myCallback(Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy')>0.999):
print("
Reached 99.9% accuracy so cancelling training!")
 self.model.stop_training = True
mycallback = myCallback()
early_stopping_callback = EarlyStopping(monitor='val_loss',
patience=epochs_to_wait_to_improve,
verbose = 2,
restore_best_weights=True)
 optimizer = Adam(lr=0.001, beta_1=0.9)
 model.compile(optimizer=optimizer, loss='sparse_categorical_crossentropy', metrics=['accuracy'] )
X_train = X_train/255.
 X_train1 = X_train1/255.
 test = test/255 .<concatenate>
history = model.fit(train_generator,
epochs=epochs,
validation_data=val_generator,
callbacks=[mycallback,early_stopping_callback] )
X_train = np.concatenate(( X_train.values, X_train1))
y_train = np.concatenate(( y_train, y_train1))<categorify>
test_loss, test_acc = model.evaluate(test_X, test_y, verbose=5)
 print('
Test accuracy:', test_acc)
y_train = to_categorical(y_train, num_classes = 10 )<split>
metrics.classification_report(val_trues, val_preds )
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.1, random_state = 2 )<train_model>
test_pred = np.array(test/255.)
test_pred = test_pred.reshape(( -1,28,28,1))
test_predictions = model.predict_classes(test_pred )
print(f"Training shape {X_train.shape}
Validation shape {X_val.shape}" )<choose_model_class>
sub_df = {'ImageId':list(range(1, len(test_predictions)+ 1)) ,'Label': test_predictions}
submission = pd.DataFrame(sub_df ).astype('int')
 submission.head()
submission.to_csv('submission.csv', index=False )