How to set steps per epoch with Keras

In this post, we will learn how to set up steps per epochs in Python Keras models. So let’s continue reading this article…

The parameter steps_per_epoch is part of model training only when we use a large size dataset.Steps_per_epoch determines the batches to train in a single dataset to improve the accuracy of the model. The parameter determines the finishing of one epoch and the starting of the next epoch. Steps_per_epoch is a real number only in the dataset with high dimensional features. Normally the parameter has accurate value before the execution of the model.

In Keras model, steps_per_epoch is an argument to the model’s fit function. Steps_per_epoch is the quotient of total training samples by batch size chosen. As the batch size for the dataset increases the steps per epoch reduce simultaneously and vice-versa.The total number of steps before declaring one epoch finished and starting the next epoch. The steps_per_epoch value is NULL while training input tensors like Tensorflow data tensors. This null value is the quotient of total training examples by the batch size, but if the value so produced is deterministic the value 1 is set.

Keras fit() function

The different parameters of the Keras fit function for a model are as shown

fit(object, x = NULL, y = NULL, batch_size = NULL, epochs = 10,
 class_weight = NULL, sample_weight = NULL,
  initial_epoch = 0, steps_per_epoch = NULL, validation_steps = NULL,
  ...)

We can observe that steps_per_epoch value is NULL as default. This value will change according to the size of the dataset. Let’s see how the parameters can be changed during the execution of a model.
The code for setting up steps per epoch in Keras model is:

batch_size=50
trainingsize = 30000 
validate_size = 5000

def calculate_spe(y):
  return int(math.ceil((1. * y) / batch_size)) 


steps_per_epoch = calculate_spe(trainingsize)
validation_steps = calculate_spe(validate_size)

model.fit(x=x_train_batch,
          epochs=50,
          steps_per_epoch=steps_per_epoch,
          validation_steps=validation_steps,
          validation_data=val_batches,           
          callbacks= model_checkpoint      
          )

 

An epoch is completed when the dataset is once passed completely through the model. The number of steps that are requires for the completion of an epoch is ceil(dataset size/batch size). At each step, the network takes in the number of batch size samples, and the weights update constantly on the basis of mean loss. So at each step weights updates on its own. The steps per epoch simply indicate how many times the batch of the dataset has been fed to the network in each epoch.

Also read: The Sequential model in Keras in Python

Leave a Reply

Your email address will not be published. Required fields are marked *