Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between steps_per_epoch and batch size in tensorlfow's model.fit() method? [duplicate]

As per the definition from documentation :

Batch size : Number of samples per gradient update.

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

How are they any different and how are they dependent on each other, if they are?

like image 536
Sadaf Shafi Avatar asked Dec 13 '25 03:12

Sadaf Shafi


1 Answers

here is a simple example. Assume that you have 1,000 training samples and you set the batch size to 50. In that case you will need to run 1000/50 =20 batches of data if you want to go through all of your training data once for each epoch. So to do that you set steps_per_epoch= 20. Many people set steps_per_epoch=number of train samples//batch_size. This is a good approximation to go through all your training examples in an epoch but it only works EXACTLY once if batch_size is a factor of the number of train samples. Below if a piece of code I wrote that determines the batch_size and steps_per_epoch to go through the samples EXACTLY once per epoch. In the code length is equal to the number of samples and b_max is the maximum batch size you will allow based on memory constraints.

batch_size=sorted([int(length/n) for n in range(1,length+1) if length % n ==0 and length/n<=b_max],reverse=True)[0]  
steps=int(length/batch_size)

For training going through the training set exactly once isn't that important if you shuffle your data.. However for validation and test going through the validation set once or the test set once is important to get a precisely true result.

like image 93
Gerry P Avatar answered Dec 16 '25 01:12

Gerry P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!