1

My dataset is the mnist sign_language_train image dataset with 27000 entries approx. I partitioned it into a train set with 21000 entries approx and a validation set with 6000 entries approx. However when I am fitting the data into the model it only trains on 687 entries per epoch.

X_train, X_validate, y_train, y_validate = train_test_split(X_train, y_train, test_size = 0.2,random_state = 123)
X_train = X_train.reshape(X_train.shape[0], *(28, 28, 1))
X_test = X_test.reshape(X_test.shape[0], *(28, 28, 1))
X_validate = X_validate.reshape(X_validate.shape[0], *(28, 28, 1))

print(X_train.shape) -->(21964, 28, 28, 1)
print(y_train.shape) -->(21964,)
print(X_validate.shape) -->(5491, 28, 28, 1)
print(y_validate.shape) -->(5491,)

model = Sequential()

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

model.add(Conv2D(filters=64, kernel_size=(3, 3), activation='relu', input_shape = (28,28,1)))
model.add(MaxPool2D(pool_size=(2, 2), strides=2))

model.add(Conv2D(filters=128, kernel_size=(3, 3), activation='relu', input_shape = (28,28,1)))
model.add(MaxPool2D(pool_size=(2, 2), strides=2))

model.add(Flatten())

model.add(Dense(64,activation ="relu"))
model.add(Dense(128,activation ="relu"))
#model.add(Dropout(0.2))
model.add(Dense(128,activation ="relu"))
#model.add(Dropout(0.3))
model.add(Dense(25,activation ="softmax"))

And then I fit the above mentioned train and validation sets as shown below and for each epoch it trains on only 687 entries.

model.compile(optimizer=SGD(learning_rate=0.001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
history2=model.fit(X_train, y_train, epochs = 50, validation_data = (X_validate, y_validate))
desertnaut
  • 57,590
  • 26
  • 140
  • 166
Shivam Jha
  • 11
  • 1

1 Answers1

1

I think you are confused with the keras log. The number you see in Epoch log while training is not total number of samples but rather total number of batches. You have 21964 samples in train and the default batch size is 32, so you will see 21964/32=686.375=687 iterations ( last batch being not full). If you want to confirm, set the batch_size to 1 then you should see 21964 iterations, one sample per batch.

Sample

def train():
  mnist = tf.keras.datasets.mnist
  (x_train, y_train), (x_test, y_test) = mnist.load_data()
  print (x_train.shape)

  #nomalize data
  x_train = tf.keras.utils.normalize(x_train, axis=1)
  x_test = tf.keras.utils.normalize(x_test, axis=1)
  #train model
  model = tf.keras.models.Sequential()
  model.add(tf.keras.layers.Flatten())
  model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
  model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
  model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))

  model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', 
          metrics=['accuracy'])
  model.fit(x_train, y_train, epochs=1)
  return model

model = train()

output:

(60000, 28, 28)
1875/1875 [=====================] - 5s 2ms/step - loss: 0.2594 - accuracy: 0.9240

In the above example we have 60000 samples and batch size (defualt) of 32 so we should have 1875 iterations as shown in the epoch log.

mujjiga
  • 16,186
  • 2
  • 33
  • 51