How the Keras steps_per_epoch in fit_generator works

前端 未结 1 1306
遥遥无期
遥遥无期 2020-12-17 07:23

In Keras documentation - steps_per_epoch: Total number of steps (batches of samples) to yield from generator before declaring one epoch finished and startin

相关标签:
1条回答
  • 2020-12-17 07:37

    The steps_per_epoch parameter is the number of batches of samples it will take to complete one full epoch. This is dependent on your batch size. The batch size is set where you initialize your training data. For example, if you're doing this with ImageDataGenerator.flow() or ImageDataGenerator.flow_from_directory(), the batch size is specified with the batch_size parameter in each of these.

    You said you have 3000 samples.

    • If your batch size was 100, then steps_per_epoch would be 30.
    • If your batch size was 10, then steps_per_epoch would be 300.
    • If your batch size was 1, then steps_per_epoch would be 3000.

    This is because steps_per_epoch should be equivalent to the total number of samples divided by the batch size. The process of implementing this in Keras is available in the two videos below.

    The reason why you have to set steps_per_epoch is that the generator is designed to run indefinitely (See the docs:

    "The generator is expected to loop over its data indefinitely."

    ). You implemented this by setting while 1. Since fit_generator() is supposed to run epochs=x times, the method must know when the next epoch begins within this indefinitely loop (and, hence, the data has to be drawn from the beginning again).

    • Image preparation for CNN training with Keras
    • Create and train a CNN in Keras
    0 讨论(0)
提交回复
热议问题