Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between steps and epochs in TensorFlow?

People also ask

What is epoch in TensorFlow?

An epoch is one training iteration, so in one iteration all samples are iterated once. When calling tensorflows train-function and define the value for the parameter epochs, you determine how many times your model should be trained on your sample data (usually at least some hundred times).

What is steps per epoch in TensorFlow?

Steps Per Epoch steps_per_epoch is batches of samples to train. It is used to define how many batches of samples to use in one epoch. It is used to declaring one epoch finished and starting the next epoch. If you have a training set of the fixed size you can ignore it.

What is steps per epoch?

The steps per epoch simply indicate how many times the batch of the dataset has been fed to the network in each epoch.


A training step is one gradient update. In one step batch_size examples are processed.

An epoch consists of one full cycle through the training data. This is usually many steps. As an example, if you have 2,000 images and use a batch size of 10 an epoch consists of:

2,000 images / (10 images / step) = 200 steps.

If you choose your training image randomly (and independently) in each step, you normally do not call it epoch. [This is where my answer differs from the previous one. Also see my comment.]


An epoch usually means one iteration over all of the training data. For instance if you have 20,000 images and a batch size of 100 then the epoch should contain 20,000 / 100 = 200 steps. However I usually just set a fixed number of steps like 1000 per epoch even though I have a much larger data set. At the end of the epoch I check the average cost and if it improved I save a checkpoint. There is no difference between steps from one epoch to another. I just treat them as checkpoints.

People often shuffle around the data set between epochs. I prefer to use the random.sample function to choose the data to process in my epochs. So say I want to do 1000 steps with a batch size of 32. I will just randomly pick 32,000 samples from the pool of training data.


As I am currently experimenting with the tf.estimator API I would like to add my dewy findings here, too. I don't know yet if the usage of steps and epochs parameters is consistent throughout TensorFlow and therefore I am just relating to tf.estimator (specifically tf.estimator.LinearRegressor) for now.

Training steps defined by num_epochs: steps not explicitly defined

estimator = tf.estimator.LinearRegressor(feature_columns=ft_cols)
train_input =  tf.estimator.inputs.numpy_input_fn({'x':x_train},y_train,batch_size=4,num_epochs=1,shuffle=True)
estimator.train(input_fn=train_input)

Comment: I have set num_epochs=1 for the training input and the doc entry for numpy_input_fn tells me "num_epochs: Integer, number of epochs to iterate over data. If None will run forever.". With num_epochs=1 in the above example the training runs exactly x_train.size/batch_size times/steps (in my case this was 175000 steps as x_train had a size of 700000 and batch_size was 4).

Training steps defined by num_epochs: steps explicitly defined higher than number of steps implicitly defined by num_epochs=1

estimator = tf.estimator.LinearRegressor(feature_columns=ft_cols)
train_input =  tf.estimator.inputs.numpy_input_fn({'x':x_train},y_train,batch_size=4,num_epochs=1,shuffle=True)
estimator.train(input_fn=train_input, steps=200000)

Comment: num_epochs=1 in my case would mean 175000 steps (x_train.size/batch_size with x_train.size=700,000 and batch_size=4) and this is exactly the number of steps estimator.train albeit the steps parameter was set to 200,000 estimator.train(input_fn=train_input, steps=200000).

Training steps defined by steps

estimator = tf.estimator.LinearRegressor(feature_columns=ft_cols)
train_input =  tf.estimator.inputs.numpy_input_fn({'x':x_train},y_train,batch_size=4,num_epochs=1,shuffle=True)
estimator.train(input_fn=train_input, steps=1000)

Comment: Although I have set num_epochs=1 when calling numpy_input_fnthe training stops after 1000 steps. This is because steps=1000 in estimator.train(input_fn=train_input, steps=1000) overwrites the num_epochs=1 in tf.estimator.inputs.numpy_input_fn({'x':x_train},y_train,batch_size=4,num_epochs=1,shuffle=True).

Conclusion: Whatever the parameters num_epochs for tf.estimator.inputs.numpy_input_fn and steps for estimator.train define, the lower bound determines the number of steps which will be run through.


In easy words
Epoch: Epoch is considered as number of one pass from entire dataset
Steps: In tensorflow one steps is considered as number of epochs multiplied by examples divided by batch size

steps = (epoch * examples)/batch size
For instance
epoch = 100, examples = 1000 and batch_size = 1000
steps = 100

Epoch: A training epoch represents a complete use of all training data for gradients calculation and optimizations(train the model).

Step: A training step means using one batch size of training data to train the model.

Number of training steps per epoch: total_number_of_training_examples / batch_size.

Total number of training steps: number_of_epochs x Number of training steps per epoch.