According to the documentation in TF 2:

       steps_per_epoch: Integer or `None`.
            Total number of steps (batches of samples)
            before declaring one epoch finished and starting the
            next epoch. When training with input tensors such as
            TensorFlow data tensors, the default `None` is equal to
            the number of samples in your dataset divided by
            the batch size, or 1 if that cannot be determined. If x is a
            `tf.data` dataset, and 'steps_per_epoch'
            is None, the epoch will run until the input dataset is exhausted.
            This argument is not supported with array inputs.
        validation_steps: Only relevant if `validation_data` is provided and
            is a `tf.data` dataset. Total number of steps (batches of
            samples) to draw before stopping when performing validation
            at the end of every epoch. If validation_data is a `tf.data` dataset
            and 'validation_steps' is None, validation
            will run until the `validation_data` dataset is exhausted.

I don't understand this part:

Total number of steps (batches of samples)
                before declaring one epoch finished and starting the
                next epoch.

If I have 1000 samples, batch size = 100, then an epoch will take 10 steps to reach. Why is another steps_per_epoch is needed? If both are used, they are conflicting. Isn't it? If the batch size is 100, then 10 steps are needed. If also steps_per_epoch = 20, then it means in one epoch it needs 20 batches, which is conflicting with the '10' steps calculated through the batch size parameter 100.

Where am I wrong?


There is an additional functionality with this method, way to think about it is a chance to use mini-epochs if loss stagnates too much

Usually it is equal to n_samples // batch_size, BUT

steps_per_epoch give you the chance to feeding process of the NN when updating the learning rate using ReduceLROnPlateuau(). This callback checks the drop of the loss once each epoch has finished and 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 n_samples // batch_size without affecting the overall number of training epochs of your model.

  • $\begingroup$ In my example above, if batch size=100, and step_per_epoch=20, then overall epoch is still 10(1000/100), then in each epoch with 100 examples, the learning rate is possibly updated 5 times (100/20)? Similarly, apply this to the validation_steps, in each epoch, the model will be updated 5 times? But the documentation says "performing validation at the end of every epoch". So it seems no validation is performed within an epoch. Please help explain with this example and thanks. $\endgroup$ – ling Dec 21 '19 at 19:15

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.