Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<create_dataframe>
%matplotlib inline
result = pd.DataFrame(
 {
'feature': model_type,
'train time': model_train_time,
'validation prediction time':model_pred_time,
'rmse train':model_rmse_train,
'rmse validation':model_rmse_val
} 
)
 result.sort_values(by='rmse validation' )<compute_train_metric>
tf.__version__
def make_mi_scores(train_data, target_data):
mi_scores = mutual_info_regression(train_data, target_data)
 mi_scores = pd.Series(mi_scores, name="MI Scores", index=train.columns)
 mi_scores = mi_scores.sort_values(ascending=False)
 return mi_scores
mi_scores = make_mi_scores(train_x, train_y)
 mi_scores<filter>
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' )
mi_features = mi_scores[mi_scores > 0.1].index.values<split>
X = train.drop(['label'], axis=1 ).values
y = train['label'].values
train_x_mi, val_x_mi, train_y_mi, val_y_mi = train_test_split(train[mi_features], target, train_size = 0.8, random_state = RANDOM_STATE)
 logreg_mi = LinearRegression()
start_time = time.time()
logreg_mi.fit(train_x_mi, train_y_mi)
 time_lapse = time.time() - start_time
model_type.append('LogReg - Mutual Information Features')
 model_train_time.append(time_lapse)
 time_lapse<predict_on_test>
train_X, val_X, train_y, val_y = train_test_split(X, y, test_size = 0.2 )
logreg_mi_pred_train = logreg_mi.predict(train_x_mi)
 logreg_mi_train_rmse = evaluation_metric(logreg_mi_pred_train, train_y_mi)
 model_rmse_train.append(logreg_mi_train_rmse)
 logreg_mi_train_rmse<predict_on_test>
train_X = train_X.reshape(-1, 28, 28, 1 ).astype("float32")/255.0
val_X = val_X.reshape(-1, 28, 28, 1 ).astype("float32")/255.0
test = test.values.reshape(-1, 28, 28, 1 ).astype("float32")/255.0
train_y = tf.keras.utils.to_categorical(train_y)
 val_y = tf.keras.utils.to_categorical(val_y )
start_time = time.time()
logreg_mi_pred_val = logreg_mi.predict(val_x_mi)
 time_lapse = time.time() - start_time
model_pred_time.append(time_lapse )<compute_train_metric>
! git clone https://github.com/bckenstler/CLR.git
logreg_mi_val_rmse = evaluation_metric(logreg_mi_pred_val, val_y_mi)
 model_rmse_val.append(logreg_mi_val_rmse)
 logreg_mi_val_rmse<create_dataframe>
clr_triangular = CyclicLR(mode='triangular' )
result = pd.DataFrame(
 {
'feature': model_type,
'train time': model_train_time,
'validation prediction time':model_pred_time,
'rmse train':model_rmse_train,
'rmse validation':model_rmse_val
} 
)
 result.sort_values(by='rmse validation' )<train_model>
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy')>0.9999):
print("
Reached 99.99% accuracy so cancelling training!")
 self.model.stop_training = True
callbacks = myCallback()
learning_rate_reduction = tf.keras.callbacks.ReduceLROnPlateau(monitor = 'val_loss', patience = 3, verbose = 1, factor = 0.5, min_lr = 1e-6 )
lreg = Lasso(random_state = RANDOM_STATE)
 start_time = time.time()
lreg.fit(train_x, train_y)
 time_lapse = time.time() - start_time
model_type.append('Lasso')
 model_train_time.append(time_lapse)
 time_lapse<predict_on_test>
IMG_SIZE = 28
BATCH_SIZE = 64
AUTOTUNE = tf.data.experimental.AUTOTUNE
num_classes = 10
def augment(image_label, seed):
image, label = image_label
image = tf.image.resize_with_crop_or_pad(
 image,
IMG_SIZE + 6,
IMG_SIZE + 6 
)
 new_seed = tf.random.experimental.stateless_split(seed, num=1)[0,:]
image = tf.image.stateless_random_crop(
 image,
size=(BATCH_SIZE, IMG_SIZE, IMG_SIZE, 1),
seed=seed 
)
 image = tf.image.stateless_random_brightness(
 image,
max_delta=0.5,
seed=new_seed 
)
 image = tf.clip_by_value(
 image,
0,
1)
 return image, label
def create_train_val(train_ds, val_ds):
train_ds =(
 train_ds 
.shuffle(1000)
.map(f, num_parallel_calls=AUTOTUNE)
.prefetch(AUTOTUNE)
)
 val_ds =(
 val_ds 
.prefetch(AUTOTUNE)
)
 return train_ds, val_ds
def create_model(train_ds, val_ds, epochs):
model = tf.keras.Sequential([
layers.Conv2D(32,(3, 3), activation = 'relu', padding = 'same', input_shape=(28,28,1)) ,
layers.Conv2D(32,(3, 3), activation = 'relu', padding = 'same'),
layers.BatchNormalization() ,
layers.MaxPooling2D(2, 2),
layers.Dropout(0.2),
layers.Conv2D(64,(3, 3), activation = 'relu', padding = 'same'),
layers.Conv2D(64,(3, 3), activation = 'relu', padding = 'same'),
layers.BatchNormalization() ,
layers.MaxPooling2D(2, 2),
layers.Dropout(0.2),
layers.Conv2D(128,(3, 3), activation = 'relu', padding = 'same'),
layers.Conv2D(128,(3, 3), activation = 'relu', padding = 'same'),
layers.BatchNormalization() ,
layers.MaxPooling2D(2, 2),
layers.Dropout(0.2),
layers.Flatten() ,
layers.Dense(512, activation = 'relu'),
layers.BatchNormalization() ,
layers.Dropout(0.2),
layers.Dense(256, activation = 'relu'),
layers.BatchNormalization() ,
layers.Dropout(0.35),
layers.Dense(10, activation = 'softmax')
 ])
 model.compile(optimizer=Adam(0.1),
loss=tf.keras.losses.CategoricalCrossentropy() ,
metrics=['accuracy'])
 print(model.summary())
 return model
rng = tf.random.Generator.from_seed(123, alg='philox')
 def f(x, y):
seed = rng.make_seeds(2)[0]
image, label = augment(( x, y), seed)
 return image, label
train_ds = tf.data.Dataset.from_tensor_slices(( train_X, train_y)).batch(BATCH_SIZE)
 val_ds = tf.data.Dataset.from_tensor_slices(( val_X, val_y)).batch(BATCH_SIZE)
 test_ds = tf.data.Dataset.from_tensor_slices(( test)).batch(BATCH_SIZE)
 train_ds, val_ds = create_train_val(train_ds, val_ds)
 cnn_model = create_model(train_ds, val_ds, 100 )
lreg_pred_train = lreg.predict(train_x)
 lreg_pred_train_rmse = evaluation_metric(lreg_pred_train, train_y)
 model_rmse_train.append(lreg_pred_train_rmse)
 lreg_pred_train_rmse<predict_on_test>
startTime = timeit.default_timer()
history = cnn_model.fit(
 train_ds,
steps_per_epoch = train_X.shape[0] // BATCH_SIZE,
epochs = 50,
validation_data = val_ds,
validation_steps = val_X.shape[0] // BATCH_SIZE,
callbacks = [callbacks, learning_rate_reduction] 
)
 elapsedTime = timeit.default_timer() - startTime
print("Time taken for the Network to train : ",elapsedTime )
start_time = time.time()
lreg_pred = lreg.predict(val_x)
 time_lapse = time.time() - start_time
model_pred_time.append(time_lapse )<compute_test_metric>
startTime = timeit.default_timer()
history = cnn_model.fit(
 train_ds,
steps_per_epoch = train_X.shape[0] // BATCH_SIZE,
epochs = 50,
validation_data = val_ds,
validation_steps = val_X.shape[0] // BATCH_SIZE,
callbacks = [clr_triangular] 
)
 elapsedTime = timeit.default_timer() - startTime
print("Time taken for the Network to train : ",elapsedTime )
lreg_rmse = evaluation_metric(lreg_pred, val_y)
 model_rmse_val.append(lreg_rmse)
 lreg_rmse<create_dataframe>
prediction = cnn_model.predict(test ).argmax(axis=1)
 sub['Label'] = prediction
sub.to_csv("MNIST_sub_cnn1.csv", index=False)
 sub.head()