← Master Index
Vol. 03 Module 3.3 Lecture

TensorFlow

AI & Data Libraries

How This Lesson Fits the Module

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/embeddedPrimary stack is Hugging Face transformers
Your platform team runs TFX or Vertex AI pipelinesYou need maximum research-community package support
Keras’s high-level API fits your team’s skill levelCustom training loops and debugging flexibility are critical
Serving models via TensorFlow Serving is already standardYou 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.

import tensorflow as tf model = tf.keras.Sequential([ tf.keras.layers.Input(shape=(784,)), tf.keras.layers.Dense(128, activation="relu"), tf.keras.layers.Dropout(0.2), tf.keras.layers.Dense(10, activation="softmax"), ]) model.compile( optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3), loss="sparse_categorical_crossentropy", metrics=["accuracy"], ) history = model.fit( X_train, y_train, validation_data=(X_val, y_val), epochs=20, batch_size=128, )

Callbacks and Training Discipline

Callbacks automate checkpointing, learning-rate schedules, and early stopping—production hygiene without boilerplate.

callbacks = [ tf.keras.callbacks.ModelCheckpoint( "best_model.keras", save_best_only=True, monitor="val_loss" ), tf.keras.callbacks.EarlyStopping( monitor="val_loss", patience=3, restore_best_weights=True ), tf.keras.callbacks.TensorBoard(log_dir="logs/fit"), ] model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=50, callbacks=callbacks)
ML Example — TensorBoard

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.

text_in = tf.keras.Input(shape=(None,), dtype="int32", name="text") numeric_in = tf.keras.Input(shape=(12,), name="numeric") text_vec = tf.keras.layers.Embedding(10000, 64)(text_in) text_vec = tf.keras.layers.GlobalAveragePooling1D()(text_vec) merged = tf.keras.layers.concatenate([text_vec, numeric_in]) hidden = tf.keras.layers.Dense(64, activation="relu")(merged) out = tf.keras.layers.Dense(1, activation="sigmoid")(hidden) multi_model = tf.keras.Model(inputs=[text_in, numeric_in], outputs=out)

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.

def preprocess(image, label): image = tf.cast(image, tf.float32) / 255.0 return image, label ds = tf.data.Dataset.from_tensor_slices((X_train, y_train)) ds = ds.shuffle(10000).map(preprocess).batch(64).prefetch(tf.data.AUTOTUNE) model.fit(ds, validation_data=val_ds, epochs=10)

Deployment Paths

FormatUse Case
.keras / SavedModelServer-side Python inference, TF Serving
TensorFlow Lite (.tflite)Mobile apps, microcontrollers, edge devices
TensorFlow.jsBrowser-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
Common Misconception: “TensorFlow is obsolete because PyTorch won research.”

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

  1. Short Answer: What three arguments does model.compile() typically set? Answer: optimizer, loss, metrics.
  2. True/False: TensorFlow 2.x executes eagerly by default. Answer: True.
  3. Short Answer: When use Functional API over Sequential? Answer: Multi-input/output or shared layers.
  4. Multiple Choice: Best format for Android on-device inference: (a) .keras, (b) .tflite, (c) .pt. Answer: (b).
  5. Short Answer: Name two Keras callbacks used for training discipline. Answer: e.g. ModelCheckpoint, EarlyStopping, TensorBoard.
  6. True/False: tf.data pipelines can shuffle, map, batch, and prefetch for GPU utilization. Answer: True.
  7. Short Answer: What does @tf.function provide? Answer: Graph compilation for better performance while TF 2.x still defaults to eager execution.
  8. Multiple Choice: Multi-input models should use: (a) Sequential only, (b) Functional API, (c) CSV writer, (d) Markdown. Answer: (b).
  9. True/False: TensorFlow is obsolete because PyTorch dominates research. Answer: False—choice is organizational and deployment-driven.
  10. 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.data scales 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.
Trainer’s Guide

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.