0
#Resizing the image to make it suitable for apply convolution
import numpy as np
img_size = 28
X_trainr = np.array(X_train).reshape(-1, img_size, img_size, 1)
X_testr = np.array(X_test).reshape(-1, img_size, img_size, 1)


# Model Compilation
model.compile(loss = "sparse_categorical_crossentropy", optimizer = "adam", metrics = ["accuracy"])
model.fit(X_trainr, y_train, epochs = 5, validation_split = 0.2) #training the model

I loaded the MNIST dataset for digit recognizing code. Then I splited the dataset in training and test set. Then added a new dimension to the 3D training set array and named new array as X_trainr. Then I complited and fitted the model. And after fitting the model, model was not taking whole training set (42000 samples) instead it is taking only 1500 samples. I have tried : set validation_split = 0.3 then it was training on 1313 samples. Why my model is not taking whole training set(42000 samples)?

Output

Epoch 1/5
1500/1500 [==============================] - 102s 63ms/step - loss: 0.2930 - accuracy: 0.9063 - val_loss: 0.1152 - val_accuracy: 0.9649
Epoch 2/5
1500/1500 [==============================] - 84s 56ms/step - loss: 0.0922 - accuracy: 0.9723 - val_loss: 0.0696 - val_accuracy: 0.9780
Epoch 3/5
1500/1500 [==============================] - 80s 53ms/step - loss: 0.0666 - accuracy: 0.9795 - val_loss: 0.0619 - val_accuracy: 0.9818
Epoch 4/5
1500/1500 [==============================] - 79s 52ms/step - loss: 0.0519 - accuracy: 0.9837 - val_loss: 0.0623 - val_accuracy: 0.9831
Epoch 5/5
1500/1500 [==============================] - 84s 56ms/step - loss: 0.0412 - accuracy: 0.9870 - val_loss: 0.0602 - val_accuracy: 0.9818
  • 1
    You are talking about steps per epoch I think. The default batch size is 32, does that tell you something? – Frightera Jul 09 '21 at 18:01
  • As @Frightera said, 1500 is not the number of samples. It is _steps per epoch_. On each step, it takes a batch size samples, which if you don't specify, it considers it as 32. – Kaveh Jul 09 '21 at 18:13
  • Does the size of batch has any effect on underfitting or overfitting? – M Zain-ul-Abideen Jul 11 '21 at 07:34

1 Answers1

0

if X_trainr has 42,000 samples initially and you use a validation split of .2 then you are training on 33600 samples. In model.fit you did not specify a batch size so it defaults to 32. The numbers shown during training are NOT the number of samples. What it is, is Number of training samples/batch_size. Which should be 33600/32=1050. However it shows 1500 so I suspect your X_trainr X .8 size is actually 48000. So Xtrainr= 48000/.8= 60000. Check the dimension of X_trainr.

Gerry P
  • 7,662
  • 3
  • 10
  • 20
  • Thanks @Garry P, but isn't it a problem that the model.fit() is not taking whole X_trainr _(which, after split will be 48000)_ in one epoch? Does this result underfitting? – M Zain-ul-Abideen Jul 11 '21 at 07:32
  • If we set batch_size = 1 in model.fit(), we can get the wanted figures. i.e one epoch will have 48000 steps. The reason is that it will now train using stochastic gradient descent. – M Zain-ul-Abideen Jul 11 '21 at 08:18