How to properly set steps_per_epoch and validation_steps in Keras?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
In training neural networks with Keras, one frequently encounters the parameters steps_per_epoch
and validation_steps
when working with models that utilize data generators or when dealing with large datasets that cannot fit into memory. Setting these parameters correctly is crucial for efficient training and for obtaining meaningful evaluation metrics. Here, we’ll explore how to properly determine these values, backed with technical insights and examples.
Understanding steps_per_epoch
and validation_steps
steps_per_epoch
steps_per_epoch
defines the number of batches of samples to be processed before declaring one epoch finished during the training phase. It is a critical parameter when using methods like fit_generator
, timeseries_generator
, or when a custom generator is utilized.
- Formula:
- If you're shuffling your dataset on each epoch and you have a dataset size of
Nand a batch size ofB, generally,steps_per_epoch = N / B. - If data augmentation or random transformations are applied which increase the dataset size virtually, adjust
steps_per_epochaccordingly to balance computational load.
validation_steps
This parameter informs how many batches from the validation generator should be used for validation after each epoch. It is essential when validation data is provided as a generator.
- Formula:
- For a validation set of size
Vwith a batch sizeB,validation_steps = V / B.
Practical Considerations
- Data Size and Memory Constraints:
- For very large datasets, utilizing a generator with
steps_per_epochallows batching data. This is especially critical when the entire dataset cannot fit into memory.
- Data Distribution:
- When data is imbalanced, each epoch should still cover a representative sample by ensuring
steps_per_epochis set correctly to incorporate class balance.
- Performance and Convergence:
- A too-small
steps_per_epochmay result in overfitting during training as weights are updated more frequently with less data per epoch. - Conversely, a large
steps_per_epochcan lead to increased training time without significant accuracy improvement.
- Epoch Definition:
- The concept of an epoch traditionally means passing through the complete dataset once. Adjust
steps_per_epochandvalidation_stepsto honor this principle whenever feasible.
Example Implementation
Consider a CIFAR-10 dataset used in a small CNN model. Assume the training set has 50,000 samples and validation set has 10,000 samples. If using a batch size of 32, here’s how we calculate:
- Dynamic Adjustments:
- During training, monitor accuracy and loss. Adjust
steps_per_epochandvalidation_stepsif the network appears overfitting or underfitting.
- Custom Generators:
- For bespoke data processing pipelines, custom Python generators may be used. These should yield tuples
(inputs, targets), or(inputs, targets, sample_weights), withsteps_per_epochandvalidation_stepsstill applicable.
- Environmental Constraints:
- computational resources (e.g., CPU vs GPU) might also affect the choice of
steps_per_epochsignificantly in terms of processing speed limitations.
Related reading
- How to properly use tf.metrics.accuracy?
- How to prune neurons in neural network
- How to read weights saved in tensorflow checkpoint file?
- How to record val_loss and loss per batch in keras
- How to properly set steps_per_epoch and validation_steps in Keras?
- How to properly use from_logits in keras loss function for binary classification?
- How to properly use tf.function while subclassing keras Layer/Model?
- How to prune a tree in R?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.