Model Pipeline - Prefetching for performance
This pipeline shows how prefetching helps speed up training by preparing data while the model trains. It keeps the GPU busy and reduces waiting time.
Jump into concepts and practice - no test required
This pipeline shows how prefetching helps speed up training by preparing data while the model trains. It keeps the GPU busy and reduces waiting time.
Loss
0.8 |****
0.6 |***
0.4 |**
0.2 |*
0.0 +----
1 2 3 4 5 Epochs| Epoch | Loss ↓ | Accuracy ↑ | Observation |
|---|---|---|---|
| 1 | 0.75 | 0.6 | Initial training with prefetching starts smoothly |
| 2 | 0.55 | 0.75 | Loss decreases and accuracy improves as training continues |
| 3 | 0.4 | 0.85 | Model converges faster due to efficient data loading |
| 4 | 0.3 | 0.9 | Training stabilizes with low loss and high accuracy |
| 5 | 0.25 | 0.92 | Final epoch shows best performance with prefetching |
prefetch() in TensorFlow data pipelines?ds?prefetch() uses the parameter buffer_size to set how many batches to prepare ahead.tf.data.AUTOTUNE (all uppercase, no underscore in 'AUTOTUNE').import tensorflow as tf
# Create a dataset
numbers = tf.data.Dataset.range(5)
# Add prefetching
prefetched = numbers.prefetch(buffer_size=tf.data.AUTOTUNE)
for item in prefetched:
print(item.numpy())tf.data.Dataset.range(5) creates numbers 0 to 4. Iterating and printing each item prints one number per line.dataset = tf.data.Dataset.range(10)
dataset = dataset.prefetch(tf.data.AUTOTUNE)
for batch in dataset.batch(2):
print(batch.numpy())dataset = dataset.batch(2).prefetch(tf.data.AUTOTUNE) to avoid error.