Cannot get accuracy to 99% for handwritten digits problem

Cannot get accuracy to 99% for handwritten digits problem. What must I change in my code?

My code is:

mnist = tf.keras.datasets.mnist

(x_train, y_train),(x_test, y_test) = mnist.load_data(path=path)
x_train, x_test = x_train / 255.0, x_test / 255.0
# GRADED FUNCTION: train_mnist
def train_mnist():
    # Please write your code only where you are indicated.
    # please do not remove # model fitting inline comments.

    # YOUR CODE SHOULD START HERE
    class myCallback(tf.keras.callbacks.Callback):
      def on_epoch_end(self, epoch, logs={}):
        if(logs.get('acc')>0.99):
          print("\nReached 99% accuracy so cancelling training!")
          self.model.stop_training = True
    # YOUR CODE SHOULD END HERE

    mnist = tf.keras.datasets.mnist

    (x_train, y_train),(x_test, y_test) = mnist.load_data(path=path)
    
    callbacks = myCallback()
    model = tf.keras.models.Sequential([
        # YOUR CODE SHOULD START HERE
        tf.keras.layers.Flatten(input_shape=(28, 28)),
        tf.keras.layers.Dense(2048, activation=tf.nn.relu),
        tf.keras.layers.Dense(10, activation=tf.nn.softmax)
        # YOUR CODE SHOULD END HERE
    ])
    

    model.compile(optimizer='adam',
                  loss='sparse_categorical_crossentropy',
                  metrics=['accuracy'])
    
    # model fitting
    history = model.fit(    
        # YOUR CODE SHOULD START HERE 
        x_train, y_train, epochs=10, callbacks=[callbacks]    
        # YOUR CODE SHOULD END HERE
    )
    
    # model fitting
    return history.epoch, history.history['acc'][-1]

My results are:

Epoch 1/10
60000/60000 [==============================] - 10s 164us/sample - loss: 3.0371 - acc: 0.9159
Epoch 2/10
60000/60000 [==============================] - 9s 147us/sample - loss: 0.5314 - acc: 0.9372s - loss: 0.5336 - acc: 0.
Epoch 3/10
60000/60000 [==============================] - 9s 147us/sample - loss: 0.4604 - acc: 0.9451
Epoch 4/10
60000/60000 [==============================] - 9s 150us/sample - loss: 0.3976 - acc: 0.9506
Epoch 5/10
60000/60000 [==============================] - 9s 150us/sample - loss: 0.3249 - acc: 0.9552
Epoch 6/10
60000/60000 [==============================] - 9s 148us/sample - loss: 0.3109 - acc: 0.9555
Epoch 7/10
60000/60000 [==============================] - 9s 147us/sample - loss: 0.2911 - acc: 0.9592
Epoch 8/10
60000/60000 [==============================] - 9s 147us/sample - loss: 0.2679 - acc: 0.9626
Epoch 9/10
60000/60000 [==============================] - 9s 147us/sample - loss: 0.2717 - acc: 0.9625
Epoch 10/10
60000/60000 [==============================] - 9s 150us/sample - loss: 0.2477 - acc: 0.9657
([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 0.96573335)

Hi @Jaime_Gonzalez,
you forgot to normalize x_train and x_test.

Best

Hi @maurizioscibilia,

You’re right! I thought I was normalising the data but I was doing so BEFORE calling the train_mnist() function.

Silly mistake!

Thanks for your time

1 Like

Hi, I am getting the wrong accuracy and it isnt changing, even if we increase neurons . The call back function works on loss but not on accuracy as the loss is decreasing but strangely the accuracy is stuck at 0.0987 . May be its stuck into a local minima? Please can anyone help me



Look at my code at the top of this thread and look at your code and try to make them as similar as possible (you callbacks function is fine, don’t worry about that).

In addition to that, you must normalise your data, so add this line right below where it says #START CODE HERE:
`x_train = x_train / 255.0

@jamie_Gonzalez Thanks the problem was resolved, I was normalizing the labels as well