TensorFlow and Keras

The Keras high-level API for fast model building with fit/evaluate, and the TensorFlow ecosystem for mobile and production deployment.

Intermediate · about 18 hours · difficulty 3/5

Learn first: Debugging and Running Training Experiments

Keras offers a concise, high-level way to build models: stack layers, compile with an optimizer and loss, call fit. It is a valid alternative if your team or target platform uses it. Keras 3 can also run on PyTorch or JAX backends.

What to learn

  • Sequential and Functional APIs for building models; subclassing for custom logic.
  • compile, fit, evaluate, predict, and callbacks (early stopping, checkpoints, learning rate schedules).
  • tf.data pipelines for efficient input loading.
  • GradientTape for custom training loops.
  • Saving in the .keras format; exporting for serving.

Ecosystem

TensorFlow Lite (LiteRT) for mobile and edge devices, TensorFlow Serving and TFX for production pipelines, and TensorFlow.js in the browser.

Trade-offs

Very quick to get started and strong deployment tooling, but most new research code and Hugging Face examples are PyTorch first.

Good enough to move on when you can build, train with callbacks and save a Keras model, and write one custom training step with GradientTape.

Key concepts

  • Sequential and Functional APIs
  • compile and fit
  • Callbacks
  • tf.data pipelines
  • GradientTape custom loops
  • TensorFlow Lite deployment

Other options for this step

  • PyTorch: Tensors, autograd, nn.Module, Dataset/DataLoader and the explicit training loop, the most widely used framework in research and much of industry.
  • JAX: Composable function transformations (grad, jit, vmap) over NumPy-style code, popular for research and large-scale training on TPUs and GPUs.

Mini project

Build a Keras image classifier for Fashion-MNIST with a tf.data pipeline, early stopping and checkpoint callbacks, then convert it to TensorFlow Lite and check the file size and accuracy.

Check yourself

What does model.compile() configure, and why must it happen before fit()?

Free resources