First time studying this topic?

Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.

Back to Deep Learning
Topic 3.8

Introduction to TensorFlow and Keras

Explore Google’s enterprise deep learning framework: static computational graphs, high-level Keras Sequential API, and TensorBoard.

Learning Objectives

  • ✓Understand TensorFlow’s architecture and ecosystem
  • ✓Build models using Keras Sequential and Functional APIs
  • ✓Compile models with optimizers, loss functions, and metrics
  • ✓Monitor training with Callbacks (EarlyStopping, ModelCheckpoint)

📖Let's Begin with a Story

In 2015, Google open-sourced TensorFlow, the successor to their internal DistBelief machine learning engine. TensorFlow was engineered for massive industrial scale: training models across thousands of Google TPU (Tensor Processing Unit) chips and serving predictions to billions of search, YouTube, and Google Photos users with millisecond latency. With the integration of François Chollet's Keras API as TensorFlow’s official high-level interface, building state-of-the-art deep networks became as simple as stacking LEGO bricks.

Why Should You Learn Introduction to TensorFlow and Keras?

🎯 Practical & Engineering Stakes

Enterprise production standard

TensorFlow powers mission-critical deployments across Fortune 500 enterprises, Android mobile runtimes (TFLite), and web browsers (TensorFlow.js).

What Is It?

Simple Explanation

TensorFlow is Google's deep learning powerhouse. Keras is the friendly steering wheel on top of it. With Keras, you build a neural network in 3 lines: 1. model = Sequential() (Create empty network) 2. model.add(Dense(64)) (Add a layer of 64 neurons) 3. model.fit(X, y) (Train on data!)

Technical Explanation

TensorFlow 2.x executes eager tensor execution backed by AutoGraph compilation (@tf.function) targeting XLA (Accelerated Linear Algebra) compilers. The Keras high-level API encapsulates model compilation, optimizer registration, and lifecycle callback hooks for distributed execution.

📚 Key Terms

TensorFlowTensorFlow

Simple: Google’s open-source framework for deep learning.
Technical: An end-to-end open-source machine learning platform with multi-device execution (CPU, GPU, TPU) and graph optimization.

KerasKeras

Simple: The easy, user-friendly API for building neural networks inside TensorFlow.
Technical: High-level deep learning API specification running on top of TensorFlow, emphasizing developer ergonomics and modularity.

EarlyStopping Callback

Simple: Automatically stopping training when the validation loss stops improving to prevent overfitting.
Technical: A training interruption hook monitoring validation metrics with patience parameter PP, restoring best model weights.

⚙️ How Does It Work?

1

Model Definition

Assemble layers sequentially using tf.keras.Sequential([Dense(...)]).

2

Compilation

Register optimizer ("adam"), loss ("sparse_categorical_crossentropy"), and metrics.

3

Fit Execution

model.fit(X, y, epochs=10, validation_split=0.2) runs training loop.

🌍 Where Is It Used?

YouTube Video Recommendation

Problem: Serving sub-millisecond personalized video rankings across 2 billion users.

Solution: Distributed TensorFlow models score billions of candidate video embeddings in parallel.

🎯 Try It Yourself

Which method is used in Keras to specify the optimizer, loss function, and metrics before training?

Interactive activity component

🐍 Python Hands-On: Building an MLP in TensorFlow / Keras

Build, compile, and fit a neural network in TensorFlow.

Open Colab

1Install Required Packages

For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:

Terminal / Notebook Install Commandbash
!pip install tensorflow
Packages:
tensorflow

2Import Libraries

Import Statementspython
1import tensorflow as tf
2from tensorflow.keras import layers, models
3import numpy as np

import tensorflow as tf → Import TensorFlow

from tensorflow.keras import layers, models → Import Keras layers

import numpy as np → NumPy for data

3Demonstration Code & Logic

Python Execution Codepython
1# Synthetic dataset: 100 samples, 8 features
2X = np.random.randn(100, 8)
3y = np.random.randint(0, 2, size=(100, 1)) # Binary targets
4
5# Build model with Keras Sequential:
6model = models.Sequential([
7 layers.Dense(32, activation="relu", input_shape=(8,)), # Layer 1
8 layers.Dense(16, activation="relu"), # Layer 2
9 layers.Dense(1, activation="sigmoid") # Output head
10])
11
12# Compile model:
13model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
14
15# Fit for 3 epochs:
16history = model.fit(X, y, epochs=3, batch_size=16, verbose=1)
17print("Training Complete! Final Loss:", round(history.history["loss"][-1], 4))
Step-by-Step Code Breakdown:

# Synthetic dataset: 100 samples, 8 features

Dataset

X = np.random.randn(100, 8)

Features

y = np.random.randint(0, 2, size=(100, 1)) # Binary targets

Labels

# Build model with Keras Sequential:

Architecture

model = models.Sequential([

Container

layers.Dense(32, activation="relu", input_shape=(8,)), # Layer 1

Hidden layer

layers.Dense(16, activation="relu"), # Layer 2

Hidden layer

layers.Dense(1, activation="sigmoid") # Output head

Sigmoid output

])

Close model

# Compile model:

Compilation

model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])

Configure learning

# Fit for 3 epochs:

Training loop

history = model.fit(X, y, epochs=3, batch_size=16, verbose=1)

Run training

print("Training Complete! Final Loss:", round(history.history["loss"][-1], 4))

Print final loss

📋 Complete Script (All-in-One)

Click "Copy for Colab", open Google Colab, paste into a single cell, and run.

Google Colab Notebook Scriptpython
1# =========================================================
2# Building an MLP in TensorFlow / Keras
3# Prepared for Google Colab (Run with Shift + Enter)
4# =========================================================
5
6# Step 1: Install required packages
7!pip install tensorflow
8
9# Step 2: Import libraries
10import tensorflow as tf
11from tensorflow.keras import layers, models
12import numpy as np
13
14# Step 3: Run Demonstration
15# Synthetic dataset: 100 samples, 8 features
16X = np.random.randn(100, 8)
17y = np.random.randint(0, 2, size=(100, 1)) # Binary targets
18
19# Build model with Keras Sequential:
20model = models.Sequential([
21 layers.Dense(32, activation="relu", input_shape=(8,)), # Layer 1
22 layers.Dense(16, activation="relu"), # Layer 2
23 layers.Dense(1, activation="sigmoid") # Output head
24])
25
26# Compile model:
27model.compile(optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"])
28
29# Fit for 3 epochs:
30history = model.fit(X, y, epochs=3, batch_size=16, verbose=1)
31print("Training Complete! Final Loss:", round(history.history["loss"][-1], 4))
⚡ How to Run in Google Colab (3 Quick Steps)
  1. Click "Copy for Google Colab" above.
  2. Click Open New Google Colab Notebook (opens in a new tab).
  3. Click on the first empty code cell, press Ctrl + V (or Cmd + V) to paste, then press Shift + Enter to execute!

🔄 Algorithmic Execution Flow

1
Sequential Assembly

Creates 8 -> 32 -> 16 -> 1 graph.

→
2
Compilation

Attaches Adam optimizer and binary cross-entropy loss.

→
3
Execution

Trains over 3 epochs, reporting loss and accuracy reduction.

📥 Input / Initial State
100 samples with 8 features each
📤 Expected Output
Epoch 1/3: loss: 0.712 - accuracy: 0.52
Epoch 2/3: loss: 0.695 - accuracy: 0.55
Epoch 3/3: loss: 0.681 - accuracy: 0.58
Training Complete!
💡 What Does This Demonstration Show?

Keras abstracts complex training loops into clean, expressive, readable syntax.

Course Learning Resource • Slide DeckLesson 512 Slides

Lesson 5: Classical Deep Learning with PyTorch

CNN, MNIST dataset, backpropagation, loss functions, and optimization.

Author: Dr. Syed Muzamil Basha• FDP Approved Curriculum
Open in New Tab

📝 Learning Check

Test your understanding with these questions. Remember, this is for learning, not examination!

Question 1: What is the purpose of the EarlyStopping callback in TensorFlow / Keras?

Question 2: What is TensorFlow primarily used for?

Question 3: What is Keras in TensorFlow?

Question 4: What does model.compile() do in TensorFlow/Keras?

Question 5: What is TensorBoard?