Why do i get different accuracies on sparse_categorical_accuracy and val_sparse_categorical_accuracy when i pass in the same data

Viewed 144

I used the same dataset for training and validating my model and yet i get different training and validation accuracy/loss. shouldn't the accuracy/loss be the same since i'm using the same data?

Here is the code:

def create_model(dataset):
    model = tf.keras.models.Sequential([tf.keras.layers.BatchNormalization(),
                                        tf.keras.layers.Conv2D(32, (3, 3), activation='relu', ),
                                        tf.keras.layers.MaxPool2D(2, 2),

                                        tf.keras.layers.Conv2D(64, (3, 3), activation='relu'),
                                        tf.keras.layers.MaxPool2D(2, 2),

                                        tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
                                        tf.keras.layers.MaxPool2D(2, 2),

                                        tf.keras.layers.Conv2D(128, (3, 3), activation='relu'),
                                        tf.keras.layers.MaxPool2D(2, 2),

                                        tf.keras.layers.Dropout(0.5),
                                        tf.keras.layers.Flatten(),
                                        tf.keras.layers.Dense(128, activation='relu'),
                                        tf.keras.layers.Dense(2, activation='softmax')])

    model.compile(
        optimizer='adam',
        loss='sparse_categorical_crossentropy',
        metrics=['sparse_categorical_accuracy'])
    model.fit(dataset, validation_data=dataset)
    return model

I get out of this:

100/100 [==============================] - 178s 2s/step - loss: 0.6487 - sparse_categorical_accuracy: 0.6212 - val_loss: 0.5866 - val_sparse_categorical_accuracy: 0.7001

Note that I only went through one epoch.

1 Answers

This is because Dropout layers doesn't work while validation. Also train accuracy is a mean average of all batch accuracies, while validation one is an accuracy of whole dataset.

Related