C.W.K.
Stream
Lesson 03 of 06 · published

Data Pipeline Options

~9 min · data

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

One fit(), many input types

One of Keras 3's quiet superpowers: model.fit() doesn't care how your data is packaged. The same training call accepts NumPy arrays, a tf.data.Dataset, a PyTorch DataLoader, a keras.utils.PyDataset, or a Pandas DataFrame — on any backend. That decoupling is what lets you keep your model code stable while you swap the data layer underneath it.

FormatBackendBest For
NumPy arraysAllSmall datasets that fit in memory
tf.data.DatasetAllLarge datasets, complex pipelines
PyTorch DataLoaderAllPyTorch ecosystem integration
keras.utils.PyDatasetAllCustom data generators
Pandas DataFrameAllTabular data

How to choose

NumPy is the right answer when everything fits in RAM — it's the simplest path and there's no reason to overbuild. The moment your data outgrows memory, or you need streaming, on-the-fly transforms, shuffling, and prefetch, reach for a real pipeline. tf.data is the most mature and feature-complete; torch.DataLoader is the idiomatic choice once you're on the PyTorch backend (with num_workers for multi-process loading); keras.utils.PyDataset is the framework-neutral escape hatch for custom generators you control end to end.

The one gotcha that bites everyone

When you pass a batched dataset (anything iterable — tf.data, DataLoader, PyDataset), the batch_size argument to fit() is ignored. The dataset already decides its own batch size. People wire up a DataLoader(batch_size=64), then pass fit(..., batch_size=32), and get confused why batches are 64 wide. The dataset wins — set the batch size where the batching actually happens.

Code

The same fit() accepts every input format·python
# All of these work with model.fit():
model.fit(numpy_x, numpy_y)              # NumPy arrays
model.fit(tf_dataset)                     # tf.data.Dataset
model.fit(torch_dataloader)               # PyTorch DataLoader
model.fit(keras_pydataset)                # keras.utils.PyDataset

External links

Exercise

Wrap CIFAR-10 in tf.data.Dataset with batch + shuffle + prefetch. Train. Then redo it with torch.utils.data.DataLoader (set KERAS_BACKEND=torch). Compare epoch times.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.