Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<normalization>
import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import os
std_scaler_base = StandardScaler()
std_scaler_base.fit(train_x_base)
 train_x_base = std_scaler_base.transform(train_x_base)
 val_x_base = std_scaler_base.transform(val_x_base )<train_model>
train_input = ".. /input/digit-recognizer/train.csv"
test_input = ".. /input/digit-recognizer/test.csv"
train_dataset = pd.read_csv(train_input)
 test_dataset = pd.read_csv(test_input )
logreg = LinearRegression()
start_time = time.time()
logreg.fit(train_x_base, train_y_base)
 time_lapse = time.time() - start_time
model_type.append('LogReg Baseline')
 model_train_time.append(time_lapse)
 time_lapse 
<predict_on_test>
train_labels = tf.keras.utils.to_categorical(train_dataset.pop("label"))
logreg_pred_train = logreg.predict(train_x_base)
 logreg_train_rmse = evaluation_metric(logreg_pred_train, train_y_base)
 model_rmse_train.append(logreg_train_rmse)
 logreg_train_rmse<predict_on_test>
train_dataset = np.array(train_dataset.values.reshape(-1, 28, 28, 1))
test_dataset = np.array(test_dataset.values.reshape(-1, 28, 28, 1))
start_time = time.time()
logreg_pred_val = logreg.predict(val_x_base)
 time_lapse = time.time() - start_time
model_pred_time.append(time_lapse )<compute_test_metric>
train_dataset = train_dataset / 255.0
test_dataset = test_dataset / 255.0
%time logreg_val_rmse = evaluation_metric(logreg_pred_val, val_y_base )<concatenate>
checkpoint_path = "logs/checkpoints/"
model_rmse_val.append(logreg_val_rmse)
 logreg_val_rmse<create_dataframe>
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3, 3), input_shape=(28, 28, 1), activation=tf.nn.relu, padding="SAME"),
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.Conv2D(64,(3, 3), activation=tf.nn.relu, padding="SAME"),
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Conv2D(128,(3, 3), activation=tf.nn.relu, padding="SAME"),
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Conv2D(128,(3, 3), activation=tf.nn.relu, padding="SAME"),
tf.keras.layers.MaxPooling2D() ,
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation="softmax")
 ])
 epochs=50
callbacks = [
tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_path,
monitor="accuracy",
save_best_only=True,
save_weights_only=True),
tf.keras.callbacks.EarlyStopping(monitor="accuracy", patience=15)
 ]
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
 history = model.fit(train_dataset, train_labels, epochs=epochs, callbacks=callbacks, batch_size=64 )
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' )<split>
model.load_weights(checkpoint_path )
train_x_corr, val_x_corr, train_y_corr, val_y_corr = train_test_split(train[correlation_features], target, train_size = 0.8, random_state = RANDOM_STATE)
 logreg_corr = LinearRegression()
start_time = time.time()
logreg_corr.fit(train_x_corr, train_y_corr)
 time_lapse = time.time() - start_time
model_type.append('LogReg - Correlation Features')
 model_train_time.append(time_lapse)
 time_lapse<predict_on_test>
labels = [np.argmax(predict)for predict in model.predict(test_dataset)]
df = pd.DataFrame({
"ImageId": list(range(1, len(test_dataset)+1)) ,
"Label": labels,
} )
logreg_corr_pred_train = logreg_corr.predict(train_x_corr)
 logreg_corr_train_rmse = evaluation_metric(logreg_corr_pred_train, train_y_corr)
 model_rmse_train.append(logreg_corr_train_rmse)
 logreg_corr_train_rmse<predict_on_test>
df.to_csv("submission.csv", index=False )
<compute_train_metric><EOS> 
model.save("model.h5" )