0
$\begingroup$

I'm having a little bit of trouble understanding what steps per epoch really means. I've read that Number of Steps per Epoch = (Total Number of Training Samples) / (Batch Size), however I don't understand at which point in the training this parameter is actually changing.

I am particularly interested in the question:

What does it mean if I trained my model with more steps per epoch than the total number of training images I have (I have a relatively small training image set).

I am going to re-run the model with fewer steps per epoch, but I am trying to figure out how I might interpret the results I have from a pretrained model trained with an additional 60 training samples and with 100 steps per epoch... the model performs relatively well though has a somewhat erratic training/validation loss curve that nonetheless decreases over time.

$\endgroup$
1
  • 1
    $\begingroup$ Can you please provide the relevant part of the code that allows you to perform 100 steps per epoch, even though you only have 60 training samples? It's possible that you feed the same samples multiple times. For example, you could feed 1 sample at a time, and do this for 60 times, then do again for 40 samples. There are different ways this could be done, but without the specific code, it's difficult to say exactly what's going on. Different libraries may use different terminology. $\endgroup$
    – nbro
    Commented Feb 10 at 16:32

1 Answer 1

1
$\begingroup$

Welcome to AI.stackexchange! To answer your question more precisely, it would be helpful to provide a minimum working example of your code where we can see how you implemented your training loop.

However, I will give you my 2 cents on this topic, since I struggled myself with understanding the difference between epochs, iterations, batches etc. See for example this question. Therein, @nbro gave a good explanaiton that might help you further understand the difference between batches, iterations and epochs.

Im having a little bit of trouble understanding what steps per epoch really means. Ive read that Number of Steps per Epoch = (Total Number of Training Samples) / (Batch Size) [...]

As you have pointed out, steps per epoch is the (total number of training samples)/(batch size), so, how many iterations are needed until one epoch is completed, i.e. until the model has seen all data. Here, I use "steps per epoch" equivalently with "batch iterations". A batch is just a chunk of you full data set, say you have a 1000 data samples, and you want a batch containing 100 data samples, you would get 1000/100 = 10 batches.

I don't understand at which point in the training this parameter is actually changing

It would therefore take you 10 iterations (or steps) until you have fed all your batches to the model, in other words, until your model has seen the entire dataset. The number of iterations is determined by the number of batches you have, so if you have 3 batches, it will be 3 iterations, so it's not about the number of images you have, but the number of batches.

What does it mean if I trained my model with more steps per epoch than the total number of training images I have

Of course, if your dataset is small, you might also have only two batches of size 500 each (in that case it would take you 2 iterations to complete the full dataset), or just one batch that contains all your data (then it just takes one iteration). For simplicity, let's consider 10 batches of 100 data samples.

Instead of feeding the full dataset, we first feed batch_0 through the model, and we compute the loss_batch_0 of that forward pass. Given this loss, we can now compute the gradient of this loss w.r.t. the parameters and update the weights according to the optimisation algorithm, in your case gradient descent. However, since you've used only a small chunk of your entire data, these gradients, as well as the loss, are only approximations of the actual gradients and the actual loss. Now, we have updated the weights of the model once. Using this slightly more accurate weights, you process batch_1, compute the loss again, loss_batch_1, which is now hopefully smaller than loss_batch_0, compute the gradient of this loss w.r.t. the weights, and update again, and so on. You see, in order to process all batches, it would take you 10 iterations, or 10 parameter updates until your model has seen all the data once.

Since we have processed all batches, i.e. the entire data set, we have completed one epoch! Surely the loss won't be small enough, which is why you would usually do more epochs, say you repeat this entire process 50 times. This would result in 50x10 iterations = 500 weight updates. As your dataset is small, you can pack all datasamples into one batch and process all you data in one iteration. Therefore, havin 50 epochs would result in 50 iterations, i.e. 50x weight update. In practice, you will often encounter much larger datasets where packing all data into one batch would either be an overkill for the computer, or from an optimisation point of view, inefficient.

Remember, in traditional gradient descent, the gradient is computed using the entire dataset, which can be computationally expensive for large datasets. SGD, on the other hand, randomly selects a subset of the data (a batch) at each iteration to compute an approximation of the gradient.

Here's the entire process in more detail, this is how your training routine should look like:

  1. Initialization: Initialize the model parameters (weights and biases)

  2. Batch Processing: For each epoch of training:

    • Divide the dataset into batches (and shuffle)

    • For each batch:

      • Feed the batch through the model with the current set of weights to compute predictions.
      • Compute the loss between the predictions and the true labels
      • Compute the gradient of the loss with respect to the model parameters
      • Update the model parameters using the gradient descent update rule (e.g., SGD update rule) based on the current set of weights.
      • Repeat for the next batch, until all your batches are processed
  3. Epoch Completion: Repeat the process for a specified number of epochs until your model has a sufficiently small train & validation loss.

After training is complete, the final weights of the model are those obtained after processing the entire dataset for the last epoch. Based on this algorithm above, you might understand what you are missing here:

but I am trying to figure out how I might interpret the results I have from a pretrained model trained with an additional 60 training samples and with 100 steps per epoch

If you pack all these 60 training samples in one batch, then you do one iteration per epoch, doing 100 iterations per epoch is consequently 100 epochs. If you'd divide these 60 samples into two batches of 30 each, you'd have 2 iterations per epoch, doing 100 epochs would result in 200 iterations, i.e. 200x updating the weights. To interpret your loss, track your training loss, and track your validation loss. If the validation loss starts to deviate from the train loss, you are most likely overfitting the dataset, and you should stop training (reduce the nr. of epochs).

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .