Efficient data loading helps your model get data fast so it can learn without waiting. This stops slowdowns during training.
Why efficient data loading prevents bottlenecks in TensorFlow
dataset = tf.data.Dataset.from_tensor_slices(data) dataset = dataset.batch(batch_size).prefetch(buffer_size=tf.data.AUTOTUNE)
tf.data.Dataset helps load and prepare data efficiently.
prefetch() lets the program prepare the next batch while the model trains on the current one.
dataset = tf.data.Dataset.from_tensor_slices(images) dataset = dataset.shuffle(1000).batch(32).prefetch(tf.data.AUTOTUNE)
dataset = tf.data.TFRecordDataset(filenames) dataset = dataset.map(parse_function).batch(64).prefetch(tf.data.AUTOTUNE)
This code creates a dataset with shuffling, batching, and prefetching to load data efficiently. It trains a simple model on dummy data and shows the accuracy.
import tensorflow as tf import numpy as np # Create dummy data x = np.random.random((1000, 28, 28, 1)).astype('float32') y = np.random.randint(0, 10, 1000) # Create dataset with efficient loading batch_size = 64 dataset = tf.data.Dataset.from_tensor_slices((x, y)) dataset = dataset.shuffle(1000).batch(batch_size).prefetch(tf.data.AUTOTUNE) # Simple model model = tf.keras.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28, 1)), tf.keras.layers.Dense(128, activation='relu'), tf.keras.layers.Dense(10, activation='softmax') ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # Train model history = model.fit(dataset, epochs=2) # Print final accuracy print(f"Final accuracy: {history.history['accuracy'][-1]:.4f}")
Using prefetch() overlaps data loading and model training to keep the GPU busy.
Shuffling data helps the model learn better by mixing examples.
Batching groups data to process multiple examples at once, improving speed.
Efficient data loading stops the model from waiting for data, speeding up training.
Use TensorFlow's tf.data API with batching, shuffling, and prefetching for best results.
This helps use hardware fully and improves training performance.