PyTorch dominates research and LLM workflows today, but TensorFlow remains entrenched in enterprise ML platforms, mobile deployment (TensorFlow Lite), and Google Cloud pipelines. A professional AI engineer understands both ecosystems and picks based on team standards and deployment targets.
TensorFlow’s high-level Keras API offers rapid prototyping; lower-level APIs and TensorFlow Serving support production at scale.
Learning Objectives
By the end of this lesson, students should be able to:
- Build and train models with the Keras Sequential and Functional APIs.
- Explain TensorFlow tensors, graphs, and eager execution.
- Compile models with appropriate loss functions, optimizers, and metrics.
- Use callbacks for checkpointing and early stopping.
- Identify TensorFlow’s strengths in deployment and mobile/edge inference.
- Compare TensorFlow and PyTorch trade-offs for real projects.
What TensorFlow Is—and When to Use It
TensorFlow is Google’s end-to-end platform for building, training, and deploying ML models. Modern TensorFlow 2.x runs eagerly by default (like PyTorch) while retaining graph compilation (@tf.function) for performance.
| Use TensorFlow when… | Prefer PyTorch when… |
|---|---|
| Deploying with TensorFlow Lite on Android/iOS/embedded | Primary stack is Hugging Face transformers |
| Your platform team runs TFX or Vertex AI pipelines | You need maximum research-community package support |
| Keras’s high-level API fits your team’s skill level | Custom training loops and debugging flexibility are critical |
| Serving models via TensorFlow Serving is already standard | You are starting a greenfield LLM project in 2025+ |
Keras Sequential API
For feedforward architectures, the Sequential API is concise and readable—ideal for baselines and teaching.
Callbacks and Training Discipline
Callbacks automate checkpointing, learning-rate schedules, and early stopping—production hygiene without boilerplate.
TensorBoard visualizes scalars, histograms, and graphs during training. Even PyTorch projects often export metrics to TensorBoard-compatible formats. Learning to read learning curves is framework-agnostic engineering skill.
Functional API for Complex Architectures
Multi-input, multi-output, and shared-layer models need the Functional API—the Keras equivalent of flexible graph construction.
tf.data for Scalable Input Pipelines
Large datasets do not fit in RAM. tf.data pipelines prefetch, shuffle, and map transformations in parallel—critical for GPU utilization.
Deployment Paths
| Format | Use Case |
|---|---|
.keras / SavedModel | Server-side Python inference, TF Serving |
TensorFlow Lite (.tflite) | Mobile apps, microcontrollers, edge devices |
| TensorFlow.js | Browser-based inference |
| ONNX (via converters) | Cross-framework deployment |
TensorFlow Strengths
- Mature deployment toolchain (TFLite, Serving)
- Keras lowers the barrier for new practitioners
- Integrated with Google Cloud ML services
- Strong tf.data input pipelines at scale
TensorFlow Trade-offs
- Smaller share of cutting-edge LLM examples vs PyTorch
- API surface spans Keras, TF core, and legacy patterns
- Debugging compiled graphs can be harder than eager PyTorch
- Version migration occasionally breaks older tutorials
Reality: Framework choice is organizational and deployment-driven. Many production systems still train or serve with TensorFlow—especially on mobile and in Google-centric stacks. Learn both concepts; specialize per job.
Knowledge Check
- Short Answer: What three arguments does
model.compile()typically set? Answer: optimizer, loss, metrics. - True/False: TensorFlow 2.x executes eagerly by default. Answer: True.
- Short Answer: When use Functional API over Sequential? Answer: Multi-input/output or shared layers.
- Multiple Choice: Best format for Android on-device inference: (a) .keras, (b) .tflite, (c) .pt. Answer: (b).
- Short Answer: Name two Keras callbacks used for training discipline. Answer: e.g. ModelCheckpoint, EarlyStopping, TensorBoard.
- True/False:
tf.datapipelines can shuffle, map, batch, and prefetch for GPU utilization. Answer: True. - Short Answer: What does
@tf.functionprovide? Answer: Graph compilation for better performance while TF 2.x still defaults to eager execution. - Multiple Choice: Multi-input models should use: (a) Sequential only, (b) Functional API, (c) CSV writer, (d) Markdown. Answer: (b).
- True/False: TensorFlow is obsolete because PyTorch dominates research. Answer: False—choice is organizational and deployment-driven.
- Short Answer: Name one TensorFlow deployment path besides SavedModel. Answer: TensorFlow Lite, TensorFlow.js, or ONNX via converters.
Key Takeaways
- TensorFlow + Keras provides a full stack from prototype to mobile deployment.
- Use callbacks, validation splits, and TensorBoard as engineering defaults.
tf.datascales input pipelines; SavedModel/TFLite scales deployment.- Choose TensorFlow or PyTorch based on team, cloud, and deployment constraints.
- Next: Jupyter for the notebook environment where most of this code runs.
Compare exercise: Implement the same MNIST classifier in PyTorch and TensorFlow/Keras. Students document lines of code, training time, and which API felt clearer for debugging.
Recap: TensorFlow + Keras covers prototype-to-mobile deployment; next, run this code in Jupyter.