19
$\begingroup$

I am replicating, in Keras, the work of a paper where I know the values of epoch and batch_size. Since the dataset is quite large, I am using fit_generator. I would like to know what to set in steps_per_epoch given epoch value and batch_size. Is there a standard way?

$\endgroup$
14
$\begingroup$

As mentioned in Keras' webpage about fit_generator():

steps_per_epoch: Integer. Total number of steps (batches of samples) to yield from generator before declaring one epoch finished and starting the next epoch. It should typically be equal to ceil(num_samples / batch_size). Optional for Sequence: if unspecified, will use the len(generator) as a number of steps.

You can set it equal to num_samples // batch_size, which is a typical choice.

However, steps_per_epoch give you the chance to "trick" the generator when updating the learning rate using ReduceLROnPlateuau() callback, because this callback checks the drop of the loss once each epoch has finished. If the loss has stagnated for a patience number of consecutive epochs, the callback decreases the learning rate to "slow-cook" the network. If your dataset is huge, as it is usually the case when you need to use generators, you would probably like to decay the learning rate within a single epoch (since it includes a big number of data). This can be achieved by setting steps_per_epoch to a value that is less than num_samples // batch_size without affecting the overall number of training epochs of your model.

Imagine this case as using mini-epochs within your normal epochs to change the learning rate because your loss has stagnated. I have found it very useful in my applications.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I saw the documentation before posting the question, I couldn't understand what it means by num_samples. Does it mean total number of rows in x_train file? $\endgroup$ – yamini goel Mar 16 '19 at 14:29
  • $\begingroup$ it means the size of your dataset, which should be equal to the number of the rows in your file $\endgroup$ – pcko1 Mar 16 '19 at 14:32
  • $\begingroup$ The learning rate is decayed at each step, not at each epoch, according to github.com/keras-team/keras/blob/… $\endgroup$ – Bananach Feb 17 at 18:03
  • $\begingroup$ @Bananach I believe we are saying the same thing, right? $\endgroup$ – pcko1 Feb 17 at 21:54
  • $\begingroup$ I didn't think so. You are saying steps_per_epoch allows for the learning rate to be adjusted during a pass through the samples, by setting steps_per_epoch < num_samples//batch_size. I am saying that that'll happen anyway even with steps_per_epoch=num_samples//batch_size, and it'll happen exactly steps_per_epoch often $\endgroup$ – Bananach Feb 17 at 22:09
2
$\begingroup$

I think it would be nice to have the following relation hold

steps_per_epoch * batch_size = number_of_rows_in_train_data

This will result in usage of all the train data for one epoch. Also, consider using fit() instead of fit_generator() if you need to have fast performance, but take into account that fit() might use more memory.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I think that relation does hold. At least, that's implied here: pyimagesearch.com/2018/12/24/… $\endgroup$ – StatsSorceress Aug 20 '19 at 15:52
  • $\begingroup$ @StatsSorceress: This is only true when the number_of_rows_in_train_data is divisible by batch_size. Otherwise, I assume, the remaining data is unused. $\endgroup$ – Eric McLachlan Feb 6 at 9:05
1
$\begingroup$

For example, if you have 100 training samples, then num_samples = 100, or the number of rows of x_train is 100.

You can specify your own batch size. In this case, say batch_size = 20. As a result, you can set your steps_per_epoch = 100/20 = 5 because in this way you can make use of the complete training data for each epoch.

If you also want to ask the scenario you want to set steps_per_epoch != num_samples/batch_size (for example, when num_samples cannot be fully divided by batch_size), please refer to this post: https://github.com/keras-team/keras/issues/10164

| improve this answer | |
$\endgroup$
0
$\begingroup$

This is the formal definition : It should typically be equal to the number of unique samples of your dataset divided by the batch size.

This is how it works,

When you provide 's' steps per epoch , Each 's' step will have 'x' batches each consisting 'n' samples are sent to fit_generator,

So, if you specify 5 steps per epoch, each epoch computes 'x' batches each consisting of 'n' samples 5 times, then the next epoch is started!

| improve this answer | |
$\endgroup$

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.