it happens quite a lot for me that I declared batch_size=64 or any number and end up watching Keras training my net on whole data instead of the batch. then I edit it many times and finally finished with the very first code working. for example this simple code:

dt=data_train.drop("label", axis=1)
inputs = keras.Input(shape=(784,), name="digits")
x = layers.Dense(500, activation="relu", name="dense_1")(inputs)
x = layers.Dense(500, activation="relu", name="dense_2")(x)
x = layers.Dense(300, activation="relu", name="dense_3")(x)
x = layers.Dense(64, activation="relu", name="dense_4")(x)
outputs = layers.Dense(10, activation="softmax", name="predictions")(x)

model = keras.Model(inputs=inputs, outputs=outputs)
opt = keras.optimizers.Adam(learning_rate=0.01)
model.compile(optimizer=opt, loss="sparse_categorical_crossentropy", metrics=["accuracy"])
X_train, X_test, y_train, y_test = train_test_split(dt, y, test_size=0.2)
X_train, Xvalid, y_train, yvalid = train_test_split(X_train, y_train, test_size=0.2)
history=model.fit(X_train,y_train, batch_size=64 ,epochs=30,validation_data=(Xvalid,yvalid))

and it train itself with : 26880/26880 [==============================] - 3s 108us/sample - loss: 0.4717 - acc: 0.8718 - val_loss: 0.4149 - val_acc: 0.8847 how I can fix it? Thanks


Using model.fit() will always train on the whole dataset for n epochs. The batch_size argument denotes how many samples are used to calculate the gradient and updates the parameters. If you want to train the model on just a batch of data (i.e. a subset of your total dataset) simply pass the subset of data to model.fit().


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.