TensorFlow & Keras
TensorFlow is Google's deep learning framework, and Keras is its high-level API for defining and training models. Most teams work almost entirely through Keras for its productivity and clarity — a few lines to build, compile, and train a network — dropping to lower-level TensorFlow only when they need custom training logic or operations.
TensorFlow's enduring strength is its deployment ecosystem: TF Serving for production APIs, TF Lite for mobile/edge, and TF.js for the browser. If your model needs to run somewhere other than a Python server, that ecosystem is a major draw.
TL;DR
- Use Keras for most model definition and training.
- Build
tf.datainput pipelines deliberately — for performance and correctness. - Follow the compile → fit → evaluate workflow.
- Track experiments and keep training reproducible.
Quick Example
Keras Sequential models read top-to-bottom; compile then fit does the rest:
Core Concepts
- Tensors and operations — the numerical substrate (with GPU/TPU support).
- Keras models — built two ways: Sequential (a simple stack of layers) or Functional (arbitrary graphs with multiple inputs/outputs).
- Optimizers, losses, metrics — configured in
compile. tf.data— the input pipeline API for efficient, correct data loading.
Sequential vs Functional
Use Sequential for simple layer stacks; reach for the Functional API when you need shared layers, multiple inputs/outputs, or non-linear topology.
Input Pipelines (tf.data)
Build pipelines deliberately — shuffle, batch, and prefetch so the GPU isn't starved:
Training Workflow & Callbacks
- Prepare data (often with
tf.data). - Define the architecture.
- Compile (loss, optimizer, metrics).
- Fit (train), then evaluate.
Callbacks add early stopping, checkpointing, and TensorBoard logging:
Best Practices
- Use a validation set and guard against overfitting (early stopping, dropout, regularization).
- Save checkpoints with
ModelCheckpoint(save_best_only=True). - Monitor metrics (TensorBoard) during training.
- Prefetch and batch in
tf.dataso input isn't the bottleneck. - Keep preprocessing identical for training and inference — bake it into the model or a shared pipeline (see MLOps).
Common Mistakes
Inconsistent preprocessing at inference
No validation monitoring → silent overfitting
FAQ
TensorFlow or PyTorch?
Both are mature and capable. TensorFlow + Keras offers a clean high-level API and an exceptional deployment ecosystem — TF Serving, TF Lite (mobile/edge), TF.js (browser) — making it attractive when you need to ship beyond a Python server. PyTorch has a more Pythonic, eager feel and leads in research. Many new projects pick PyTorch for ergonomics; choose TensorFlow if its deployment targets or your team's experience favor it.
When should I use the Functional API over Sequential?
Use Sequential for straightforward, single-input single-output stacks of layers — it's the simplest and most readable. Use the Functional API when the architecture isn't a simple line: multiple inputs or outputs, shared layers, residual/skip connections, or branching. For highly dynamic or custom logic, subclass keras.Model. Most everyday models are Sequential or Functional.
Why use tf.data instead of plain NumPy arrays?
tf.data builds an efficient input pipeline that can shuffle, batch, prefetch, and parallelize data loading so the accelerator isn't waiting on the CPU — often the real training bottleneck. It also handles datasets too large to fit in memory by streaming from disk. For small in-memory datasets, NumPy arrays to fit() are fine; for performance or scale, tf.data with prefetch(AUTOTUNE) matters.
How do I deploy a trained TensorFlow model?
Save it (model.save("path.keras") or the SavedModel format) and choose a target: TF Serving for scalable server-side inference over gRPC/REST, TF Lite for mobile and embedded/edge devices, or TF.js to run in the browser. Bundle preprocessing into the model where possible so serving matches training. See MLOps for the surrounding lifecycle (versioning, monitoring, rollout).
Related Topics
- PyTorch — The main alternative
- Python for Data Science — The surrounding toolkit
- Model Evaluation — Validating models
- MLOps — Deploying and monitoring
- Computer Vision · NLP — Application domains