9

What if we specify batch size as 15 and the sample size of 1000 which is not divisible by 15 in Keras model training?.should it still able to train?

also I have looked in to this answer but it's not helping question

please can anybody explain this Thank you.

Dulmina
  • 423
  • 3
  • 15
  • 1
    Did you try? It works. – Dr. Snoopy Jan 12 '19 at 11:43
  • yeh i tried in previous models that i have created its working but in the latest model that i have created gives an error.but if give divisible number then it works fine. – Dulmina Jan 12 '19 at 11:46
  • 1
    If you have errors then you should include it in your question, along with the code that produced it and any tracebacks. – Dr. Snoopy Jan 12 '19 at 11:47
  • yes Matias here is the code but it somewhat lengthy,i have posted it as another question yesterday but i did'nt get any answer.https://stackoverflow.com/questions/54141106/indices-2-is-not-in-0-1 – Dulmina Jan 12 '19 at 11:50

3 Answers3

8

I found the answer for this. If this is the case, it will take it will take the remaining 10 samples to the last step of the epoch.

Eg: 15x66+10=1000 that means it will take 66 batches of size 15 and for the final steps it takes only 10.

Anyways this will only work with input_shape, if we use batch_input_shape it will give us an error because we are specifying the batch shape in the graph level.

peterh
  • 11,875
  • 18
  • 85
  • 108
Dulmina
  • 423
  • 3
  • 15
3

This is no problem for your training and validation data. The generator will take care of this. Hence you can simply use:

STEPS = train_generator.n // train_generator.batch_size
VALID_STEPS = validation_generator.n // train_generator.batch_size

history = model.fit_generator(
    train_generator,
    steps_per_epoch=STEPS,
    epochs=100,
    validation_data=validation_generator,
    validation_steps=VALID_STEPS)

However, for your testset make sure that the batch size fits the data, otherwise you run a risk of your predictions not matching your true labels when comparing both (please check this article which highlights this https://medium.com/difference-engine-ai/keras-a-thing-you-should-know-about-keras-if-you-plan-to-train-a-deep-learning-model-on-a-large-fdd63ce66bd2). You can ensure that the batch size fits your data by using a loop for example:

for i in range(1,160):
    if len(test_data) % i == 0:
        div = i
batch_size = div
AaronDT
  • 3,940
  • 8
  • 31
  • 71
  • Thanks. I was making logic to create 0 padded samples to make remainders from division into a full batch. This is going to help from wasting a lot of time. – Robi Sen Nov 02 '19 at 06:59
0

in tf1.14 you should not care about this issue. It seems that in tf1.15, you need to worry about this.