why keras wont train on my entire image set

0

Issue

I am training a convolutional model in keras.
The size of my training data is

(60000, 28, 28, 1)

I build a simple model here

model = Sequential()

model.add(
    Conv2D(
        filters=32,
        kernel_size=(4, 4), 
        input_shape=(28, 28, 1),
        activation='relu'
    )
)
model.add(MaxPool2D(pool_size=(2, 2)))       

model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))

model.compile(
    loss='categorical_crossentropy', 
    optimizer='adam',
    metrics=['accuracy']
)

When I tried to fit this model to my data in the following line

model.fit(
    x_train,
    y_train_hot_encode,
    epochs=20,
    validation_data=(x_test, y_test_hot_encode),
)

I noticed something weird in the logs

Epoch 1/20
1875/1875 [==============================] - 18s 9ms/step - loss: 0.5311 - accuracy: 0.8109 - val_loss: 0.3381 - val_accuracy: 0.8780
Epoch 2/20
1875/1875 [==============================] - 19s 10ms/step - loss: 0.2858 - accuracy: 0.8948 - val_loss: 0.2820 - val_accuracy: 0.8973
Epoch 3/20
1875/1875 [==============================] - 18s 9ms/step - loss: 0.2345 - accuracy: 0.9150 - val_loss: 0.2732 - val_accuracy: 0.9001
Epoch 4/20
1875/1875 [==============================] - 18s 9ms/step - loss: 0.2016 - accuracy: 0.9247 - val_loss: 0.2549 - val_accuracy: 0.9077
Epoch 5/20
1875/1875 [==============================] - 17s 9ms/step - loss: 0.1644 - accuracy: 0.9393 - val_loss: 0.2570 - val_accuracy: 0.9077
Epoch 6/20
1875/1875 [==============================] - 17s 9ms/step - loss: 0.1434 - accuracy: 0.9466 - val_loss: 0.2652 - val_accuracy: 0.9119
Epoch 7/20
1875/1875 [==============================] - 17s 9ms/step - loss: 0.1225 - accuracy: 0.9553 - val_loss: 0.2638 - val_accuracy: 0.9135

As you can see, each epoch was trained on 1875 images and not on the entire 60K images, why is that? or am I reading the log the wrong way?

Solution

It is because the number shown there is number of steps instead of number of examples trained. As you didn’t supply batch_size into model.fit(), it used the default batch size 32.

The expected number of steps per epoch is ceil(60000/32) = 1875, consistent with what is shown in the log.

Answered By – Laplace Ricky

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More