value of steps per epoch passed to keras fit generator function
Asked Answered
E

3

13

What is the need for setting steps_per_epoch value when calling the function fit_generator() when ideally it should be number of total samples/ batch size?

Easing answered 21/12, 2017 at 15:46 Comment(0)
P
20

Keras' generators are infinite.

Because of this, Keras cannot know by itself how many batches the generators should yield to complete one epoch.

When you have a static number of samples, it makes perfect sense to use samples//batch_size for one epoch. But you may want to use a generator that performs random data augmentation for instance. And because of the random process, you will never have two identical training epochs. There isn't then a clear limit.

So, these parameters in fit_generator allow you to control the yields per epoch as you wish, although in standard cases you'll probably keep to the most obvious option: samples//batch_size.

Priscella answered 21/12, 2017 at 16:3 Comment(2)
should be samples // batch_size i believeExocarp
This answer created more questions in my mind :'|, first time to use generatorsEndurable
C
3

Without data augmentation, the number of samples is static as Daniel mentioned. Then, the number of samples for training is steps_per_epoch * batch size.

By using ImageDataGenerator in Keras, we make additional training data for data augmentation. Therefore, the number of samples for training can be set by yourself. If you want two times training data, just set steps_per_epoch as (original sample size *2)/batch_size.

Counterproductive answered 18/1, 2019 at 4:3 Comment(0)
I
0

This is the safest one.

steps_per_epoch = len(train_generator). 

If you have 1300 images and batch size of 64, if you set steps_per_epoch = 1300//64, it will result in 20 steps per epoch, ignoring some image. By setting steps_per_epoch = len(train_generator), you ensure all 1300 images are used during each epoch, including handling the case where the last batch might be smaller than the specified batch size due to division remainder.

Immateriality answered 2/2 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.