I have gone through the TensorFlow documentation.
What is the difference between cache()
vs prefetch()
in TensorFlow?
When should I use the cache()
function and when should I use the prefetch()
function?
The tf.data.Dataset.cache
transformation can cache a dataset, either in memory or on local storage. This will save some operations (like file opening and data reading) from being executed during each epoch. The next epochs will reuse the data cached by the cache transformation.
Prefetch overlaps the preprocessing and model execution of a training step. While the model is executing training step s, the input pipeline is reading the data for step s+1. Doing so reduces the step time to the maximum (as opposed to the sum) of the training and the time it takes to extract the data.