From 9b63cad586ad86e4f5d2f54629db88c9869bc6b4 Mon Sep 17 00:00:00 2001 From: Shivam-316 <56474719+Shivam-316@users.noreply.github.com> Date: Fri, 2 Oct 2020 09:27:59 +0530 Subject: [PATCH] Give 99.28% accuracy on MNIST --- CNN.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/CNN.py b/CNN.py index 71bf972..f8e1909 100644 --- a/CNN.py +++ b/CNN.py @@ -12,22 +12,36 @@ def __init__(self): def build_and_compile_model(self): if self.modelbuilt: return - # Add a Convolutional layer - self.model.add(tf.keras.layers.Conv2D(32, (3, 3), input_shape=(28, 28, 1), activation='relu')) - # Add a Max pooling layer + # Add a Convolutional layers and Max pooling layers + self.model.add(tf.keras.layers.Conv2D(32, 5, input_shape=(28, 28, 1), activation='relu')) self.model.add(tf.keras.layers.MaxPool2D()) - # Add the flattened layer + + self.model.add(tf.keras.layers.Conv2D(64, 3,activation='relu')) + self.model.add(tf.keras.layers.MaxPool2D()) + + self.model.add(tf.keras.layers.Conv2D(128, 3,activation='relu')) + self.model.add(tf.keras.layers.MaxPool2D()) + + self.model.add(tf.keras.layers.Conv2D(256, 2,activation='relu')) + self.model.add(tf.keras.layers.MaxPool2D()) + self.model.add(tf.keras.layers.Flatten()) - # Add the hidden layer - self.model.add(tf.keras.layers.Dense(512, activation='relu')) - # Adding a dropout layer + + # Add the hidden layers and Dropout + self.model.add(tf.keras.layers.Dense(50, activation='relu')) self.model.add(tf.keras.layers.Dropout(0.2)) + + self.model.add(tf.keras.layers.Dense(25, activation='relu')) + self.model.add(tf.keras.layers.Dropout(0.2)) + # Add the output layer self.model.add(tf.keras.layers.Dense(10, activation='softmax')) # Compiling the model - self.model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) + self.model.compile(optimizer="nadam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]) self.modelbuilt = True + + '''This function loads the Train/Test dataset, trains the model and evaluates it. It prints the accuracy attained on the test set in the end''' def train_and_evaluate_model(self):