Kaggle competition: Digit Recognizer
<SOS> metric: categorizationaccuracy Kaggle data source: digit-recognizer<train_model>
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
epochs = 20
batch_size = 256
history_1 = model.fit(train_x, train_y, batch_size = batch_size, epochs = epochs, validation_data =(val_x, val_y))<compute_train_metric>
mnist = pd.read_csv("/kaggle/input/digit-recognizer/train.csv" )
val_p = np.argmax(model.predict(val_x), axis = 1)
 error = 0
confusion_matrix = np.zeros([10, 10])
 for i in range(val_x.shape[0]):
confusion_matrix[val_y[i], val_p[i]] += 1
if val_y[i] != val_p[i]:
error += 1
print("Confusion Matrix:
", confusion_matrix)
 print("
Errors in validation set: ", error)
 print("
Error Persentage: ",(error * 100)/ val_p.shape[0])
 print("
Accuracy: ", 100 -(error * 100)/ val_p.shape[0])
 print("
Validation set Shape: ", val_p.shape[0] )<train_model>
y = mnist.iloc[:, 0].values
X = mnist.iloc[:, 1:].values
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)
 datagen.fit(train_x )<choose_model_class>
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1
, random_state = 0 )
lrr = ReduceLROnPlateau(monitor = 'val_accuracy', patience = 2, verbose = 1, factor = 0.5, min_lr = 0.00001 )<train_model>
X_train = X_train.reshape(-1, 28, 28, 1 ).astype('float32')/ 255.0
X_test = X_test.reshape(-1, 28, 28, 1 ).astype('float32')/ 255.0
epochs = 30
history_2 = model.fit_generator(datagen.flow(train_x, train_y, batch_size = batch_size), steps_per_epoch = int(train_x.shape[0]/batch_size)+ 1, epochs = epochs, validation_data =(val_x, val_y), callbacks = [lrr])
<compute_train_metric>
y_train = tf.keras.utils.to_categorical(y_train)
 y_test = tf.keras.utils.to_categorical(y_test )
val_p = np.argmax(model.predict(val_x), axis = 1)
 error = 0
confusion_matrix = np.zeros([10, 10])
 for i in range(val_x.shape[0]):
confusion_matrix[val_y[i], val_p[i]] += 1
if val_y[i] != val_p[i]:
error += 1
print("Confusion Matrix:
", confusion_matrix)
 print("
Errors in validation set: ", error)
 print("
Error Persentage: ",(error * 100)/ val_p.shape[0])
 print("
Accuracy: ", 100 -(error * 100)/ val_p.shape[0])
 print("
Validation set Shape: ", val_p.shape[0] )<predict_on_test>
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32,(3, 3), activation = 'relu', padding = 'same', input_shape =(28, 28, 1)) ,
tf.keras.layers.Conv2D(32,(3, 3), activation = 'relu', padding = 'same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(64,(3, 3), activation = 'relu', padding = 'same'),
tf.keras.layers.Conv2D(64,(3, 3), activation = 'relu', padding = 'same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv2D(128,(3, 3), activation = 'relu', padding = 'same'),
tf.keras.layers.Conv2D(128,(3, 3), activation = 'relu', padding = 'same'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(512, activation = 'relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(256, activation = 'relu'),
tf.keras.layers.BatchNormalization() ,
tf.keras.layers.Dropout(0.35),
tf.keras.layers.Dense(10, activation = 'softmax')
 ] )
test_y = np.argmax(model.predict(test_x), axis = 1 )<save_to_csv>
model.compile(optimizer = Adam(lr = 1e-3),
loss = 'categorical_crossentropy',
metrics = ['accuracy'] )
df_submission = pd.DataFrame([df_test.index + 1, test_y], ["ImageId", "Label"] ).transpose()
df_submission.to_csv("MySubmission.csv", index = False )<choose_model_class>
datagen = ImageDataGenerator(
 rotation_range= 10,
zoom_range = 0.1,
width_shift_range = 0.1,
height_shift_range = 0.1 
)
 datagen.fit(X_train )
tpu = tf.distribute.cluster_resolver.TPUClusterResolver.connect()
tpu_strategy = tf.distribute.experimental.TPUStrategy(tpu)
<load_from_csv>
train_generator = datagen.flow(X_train, y_train, batch_size = 64)
 validation_generator = datagen.flow(X_test, y_test, batch_size = 64 )
train_dataframe=pd.read_csv(".. /input/digit-recognizer/train.csv")
 test_dataframe=pd.read_csv(".. /input/digit-recognizer/test.csv" )<count_values>
learning_rate_reduction = ReduceLROnPlateau(monitor = 'val_loss', patience = 3, verbose = 1, factor = 0.5, min_lr = 1e-6)
 model_checkpoint = ModelCheckpoint('./best_model.hdf5',monitor = 'val_loss', mode = "min", verbose = 1, save_best_model = True )
train_dataframe['label'].value_counts()<data_type_conversions>
history = model.fit_generator(
 train_generator,
steps_per_epoch = X_train.shape[0] // 64,
epochs = 50,
validation_data = validation_generator,
validation_steps = X_test.shape[0] // 64,
callbacks = [learning_rate_reduction, model_checkpoint]
)
train_label = train_dataframe.label.to_numpy()
train_image=train_dataframe.to_numpy() [0:,1:].reshape(42000,28,28,1)
 test_image = test_dataframe.to_numpy().reshape(28000,28,28,1 )<data_type_conversions>
model = load_model("./best_model.hdf5" )
train_image = train_image.astype(float)/ 255.0
test_image = test_image.astype(float)/ 255.0<choose_model_class>
model.evaluate(X_test, y_test, verbose = 1 )
with tpu_strategy.scope() :
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64,(3,3), activation='relu',padding = 'Same', input_shape=(28, 28, 1)) ,
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(128,(3,3), activation='relu',padding = 'Same'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Conv2D(256,(3,3), activation='relu',padding = 'Same'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Dropout(0.25),
tf.keras.layers.Flatten() ,
tf.keras.layers.Dense(1024, activation='relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(10, activation='softmax')
 ])
 optimizer = Adam(learning_rate=0.001)
 model.compile(loss=SparseCategoricalCrossentropy(from_logits=True),
optimizer = optimizer,
metrics=['accuracy'])
 epochs = 50
batch_size = 16<split>
test = pd.read_csv("/kaggle/input/digit-recognizer/test.csv" ).values
test = test.reshape(-1, 28, 28, 1 ).astype('float32')/ 255.0
y_pred = model.predict(test ).argmax(axis=1)
 y_pred.shape
x_train,x_val,y_train,y_val=train_test_split(train_image,train_label,test_size=0.2,random_state=42 )<train_model>
submission = pd.DataFrame({'ImageId': np.arange(1, 28001), 'Label': y_pred})
 submission.to_csv("submission.csv", index = False)
 print("Your submission was successfully saved!" )