Skip to content

17: Deep Learning with TensorFlow and Keras

hero

Chapter 17: Deep Learning with TensorFlow and Keras

Section titled “Chapter 17: Deep Learning with TensorFlow and Keras”

In the previous chapter, you mastered traditional machine learning with scikit-learn. You learned how to handle tabular data and build models like Random Forests and SVMs. However, some problems are too complex for traditional algorithms—problems involving unstructured data like images, audio, and large-scale natural language. This is where Deep Learning shines.

Deep Learning is a subset of machine learning inspired by the structure and function of the human brain, using Artificial Neural Networks (ANNs). While traditional machine learning often requires manual “feature engineering,” deep learning models can learn features directly from the raw data.

In this chapter, we will use TensorFlow (Google’s open-source library) and Keras (the high-level API for TensorFlow) to build and train neural networks. For a PHP developer, deep learning might seem like “magic,” but we will deconstruct it into familiar concepts: layers, weights, and optimization. You’ll learn how to build models that can recognize objects in photos and predict the sentiment of customer reviews.

Before starting this chapter, you should have:

  • Completed Chapter 16: scikit-learn ML
  • Python 3.10+ with tensorflow and numpy installed
  • Basic understanding of multi-dimensional arrays (NumPy)
  • Estimated Time: ~2.5 hours

Verify your setup:

Terminal window
# Install TensorFlow (this may take a few minutes)
pip install tensorflow numpy matplotlib
# Verify installation
python3 -c "import tensorflow as tf; print(f'TensorFlow version: {tf.__version__}')"

By the end of this chapter, you will have created:

  • Multi-layer Perceptron (MLP): A basic neural network for digit classification (MNIST).
  • Convolutional Neural Network (CNN): A specialized network for identifying objects in images.
  • Transfer Learning Pipeline: A method to adapt a massive Google-trained model (MobileNet) to your specific business tasks.
  • Python-Flask Inference API: A microservice that serves deep learning predictions to your PHP application via JSON/HTTP.
  • Understand the architecture of a Neuron and Dense Layer.
  • Master the Keras Sequential API for building deep networks.
  • Learn the basics of Convolutions and Pooling for computer vision.
  • Implement Early Stopping to prevent overfitting.
  • Serve heavy deep learning models to lightweight PHP clients.

Step 1: Neural Network Foundations (~20 min)

Section titled “Step 1: Neural Network Foundations (~20 min)”

Understand the anatomy of a neural network: inputs, weights, biases, and activation functions.

A neural network is essentially a collection of mathematical functions layered on top of each other. Each connection has a weight that determines its importance. During training, the network adjusts these weights to minimize errors—a process called Backpropagation.

1. Create a basic MLP visualization script:

examples/neural_net_foundations.py
import tensorflow as tf
from tensorflow.keras import layers, models
import numpy as np
# 1. Define a simple Sequential model
# Sequential means layers are stacked one after another
model = models.Sequential([
# Input Layer (Flatten 28x28 images into a 784-length vector)
layers.Input(shape=(28, 28)),
layers.Flatten(),
# Hidden Layer (Dense means every neuron connects to every input)
# ReLU (Rectified Linear Unit) is the standard "activation" function
layers.Dense(128, activation='relu'),
# Output Layer (10 neurons for digits 0-9)
# Softmax turns outputs into probabilities (sum to 1.0)
layers.Dense(10, activation='softmax')
])
# 2. Compile the model
# Optimizer (Adam) adjusts weights
# Loss (Crossentropy) measures how "wrong" the model is
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 3. View the architecture
model.summary()

2. Run the script:

Terminal window
python3 examples/neural_net_foundations.py
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
flatten (Flatten) (None, 784) 0
dense (Dense) (None, 128) 100480
dense_1 (Dense) (None, 10) 1290
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
  • layers.Dense: This is the basic building block. The 128 neurons in the hidden layer each have 784 weights (from the input) plus a bias, resulting in 784 * 128 + 128 = 100,480 parameters.
  • Activation Functions: Without them, the network would just be a simple linear regression. relu allows the network to learn non-linear patterns.
  • model.summary(): This is your roadmap. It tells you exactly how many “knobs” (parameters) the network will be turning during training.

Step 2: Training your first MLP (MNIST) (~30 min)

Section titled “Step 2: Training your first MLP (MNIST) (~30 min)”

Train a model to recognize handwritten digits using the famous MNIST dataset.

1. Create the training script:

examples/train_mnist.py
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
# 1. Load and Normalize Data
# MNIST is the "Hello World" of Deep Learning
(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data()
# Scale pixels (0-255) to range (0-1) for better math performance
train_images, test_images = train_images / 255.0, test_images / 255.0
# 2. Build Model
model = models.Sequential([
layers.Flatten(input_shape=(28, 28)),
layers.Dense(128, activation='relu'),
layers.Dropout(0.2), # Randomly shut off neurons to prevent overfitting
layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# 3. Train the model (Epochs = passes through the whole dataset)
print("Training started...")
model.fit(train_images, train_labels, epochs=5)
# 4. Evaluate
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose=2)
print(f'\nTest Accuracy: {test_acc:.4f}')
# 5. Save Model
model.save('models/mnist_model.h5')
print("Model saved to models/mnist_model.h5")

2. Run the script:

Terminal window
python3 examples/train_mnist.py
  • Normalization: Deep learning models prefer small numbers. Dividing pixel values by 255 is a mandatory step.
  • Dropout: This is a regularization technique. By “dropping” 20% of neurons during each training step, we force the network to become more robust and less reliant on specific pixels.
  • Epochs: Training for multiple passes allows the network to refine its weights gradually.

Problem: Accuracy is very low (~10%) Cause: Usually means the data wasn’t normalized or the labels are mismatched. Solution: Ensure you divided by 255.0 and your output layer has exactly 10 neurons.

Step 3: Computer Vision with CNNs (~40 min)

Section titled “Step 3: Computer Vision with CNNs (~40 min)”

Build a Convolutional Neural Network (CNN)—the gold standard for image recognition.

A standard “Dense” network treats an image as a flat vector, losing spatial relationships (e.g., “this eye is above this nose”). CNNs use Convolutions to scan for local patterns like edges, shapes, and textures.

1. Create a CNN script:

examples/cnn_example.py
import tensorflow as tf
from tensorflow.keras import datasets, layers, models
# 1. Load CIFAR-10 (10 classes of color images)
(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()
train_images, test_images = train_images / 255.0, test_images / 255.0
# 2. Build CNN Architecture
model = models.Sequential([
# Convolutional layers "scan" the image for features
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)),
layers.MaxPooling2D((2, 2)), # Reduce size, keeping most important info
layers.Conv2D(64, (3, 3), activation='relu'),
layers.MaxPooling2D((2, 2)),
# Transition to Dense layers for classification
layers.Flatten(),
layers.Dense(64, activation='relu'),
layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.summary()
# 3. Train (Note: CNNs are slower to train than MLPs)
model.fit(train_images, train_labels, epochs=3, validation_data=(test_images, test_labels))
  • Conv2D: This layer applies filters (kernels) to the image. Early layers might find lines, while later layers find complex shapes like “ears” or “wheels.”
  • MaxPooling2D: This halves the resolution of the feature maps, reducing the computational load and helping the network focus on “where” features are rather than their exact pixel location.
  • Spatial Awareness: Because filters slide across the image, CNNs can recognize a “cat” regardless of whether it’s in the top-left or bottom-right corner.

Use a pre-trained model from Google (MobileNet) and adapt it to your own small dataset.

Training a deep network from scratch requires millions of images and weeks of GPU time. Transfer Learning allows you to leverage the “knowledge” of a massive model and just train a small custom “head” for your specific task.

1. Create a Transfer Learning script:

examples/transfer_learning.py
import tensorflow as tf
from tensorflow.keras import layers, models, applications
# 1. Load Pre-trained Base Model (MobileNetV2)
# include_top=False means we throw away Google's classification layer
base_model = applications.MobileNetV2(input_shape=(160, 160, 3),
include_top=False,
weights='imagenet')
# 2. Freeze the base model (Don't change Google's weights!)
base_model.trainable = False
# 3. Add our custom "Head"
model = models.Sequential([
base_model,
layers.GlobalAveragePooling2D(),
layers.Dense(1, activation='sigmoid') # Binary classification (e.g., Cat vs Dog)
])
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
model.summary()

The base_model has already learned how to “see” millions of objects. By freezing its weights, we keep those generic visual features (edges, textures) and only teach the final layer how to combine those features for our specific problem.

Step 5: Serving Deep Learning Models to PHP (~30 min)

Section titled “Step 5: Serving Deep Learning Models to PHP (~30 min)”

Serve your TensorFlow model via a Flask API so your PHP application can consume it.

Deep learning libraries are massive and memory-hungry. You shouldn’t try to run them inside a standard PHP request. Instead, run a dedicated Python service that handles the model and returns JSON.

1. Create the Python API (Flask):

services/ml_api.py
from flask import Flask, request, jsonify
import tensorflow as tf
import numpy as np
from PIL import Image
import io
app = Flask(__name__)
# Load the model once at startup
model = tf.keras.models.load_model('models/mnist_model.h5')
@app.route('/predict', methods=['POST'])
def predict():
# 1. Get image from PHP
file = request.files['image']
img = Image.open(file.stream).convert('L').resize((28, 28))
# 2. Preprocess (Convert to NumPy and Normalize)
img_array = np.array(img) / 255.0
img_array = img_array.reshape(1, 28, 28)
# 3. Predict
prediction = model.predict(img_array)
digit = int(np.argmax(prediction))
confidence = float(np.max(prediction))
return jsonify({
"digit": digit,
"confidence": round(confidence, 4)
})
if __name__ == "__main__":
app.run(port=5000)

2. Call from PHP:

examples/consume_dl_api.php
<?php
declare(strict_types=1);
$imageUrl = 'path/to/digit_image.png';
$ch = curl_init('http://localhost:5000/predict');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, [
'image' => new CURLFile($imageUrl)
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$data = json_decode($response, true);
echo "Prediction: " . $data['digit'] . "\n";
echo "Confidence: " . ($data['confidence'] * 100) . "%\n";

This architecture separates concerns. PHP handles the user session, database, and file uploads, while Python handles the heavy matrix multiplications required by deep learning. This is how production AI systems (like Google or Netflix) are built.

Goal: Build a simple Dense network for text classification.

  1. Use the IMDB dataset (datasets.imdb.load_data).
  2. Build a Sequential model with an Embedding layer, a GlobalAveragePooling1D layer, and a Dense output.
  3. Train it to recognize “Positive” vs “Negative” reviews.

Goal: Improve your CNN’s robustness.

  1. Add layers.RandomFlip("horizontal") and layers.RandomRotation(0.1) at the beginning of your CNN model.
  2. Observe how it affects the training accuracy vs validation accuracy.
  3. Validation: Does it reduce overfitting?

Goal: Create a PHP form that uploads an image and displays the AI’s prediction.

  1. Create a simple HTML form with a file input.
  2. In the PHP handler, forward the image to the Flask API.
  3. Display the result and the “confidence” score to the user.

In this chapter, you moved beyond traditional machine learning into the cutting edge of AI:

  1. Neural Architecture: Understanding neurons, layers, and how they stack.
  2. Sequential API: Mastering the clean, declarative way to build models in Keras.
  3. Computer Vision: Using CNNs to solve problems that are impossible for traditional code.
  4. Efficiency: Leveraging Transfer Learning to get “Superhuman” results with minimal data.
  5. Architectural Separation: Using the API pattern to bridge the gap between heavy Python AI and agile PHP web apps.
  1. MNIST Digit Classifier: A foundation in image recognition.
  2. CIFAR CNN: A multi-class visual model.
  3. MobileNet Adaptor: A professional transfer learning pipeline.
  4. PHP-Python AI Service: A real-world production architecture.

1. Don’t Reinvent the Wheel If you’re working with images or text, always check for a pre-trained model (Transfer Learning) before trying to build your own from scratch.

2. Data is Fuel Deep learning models are “data hungry.” If you only have 100 examples, traditional ML (scikit-learn) will often outperform a neural network.

3. Monitor Your Validation Loss If your training accuracy is 99% but your validation accuracy is 70%, your model is overfitting (memorizing). Use Dropout and Early Stopping.

4. GPU is a Game Changer Training deep networks on a CPU is slow. For production models, use a GPU (via Google Colab or AWS) to speed up training by 10-50x.

You are now approaching the finish line of the technical specialization:

  1. Chapter 1-12: Built data systems in PHP.
  2. Chapter 13-16: Mastered Python, Stats, and ML.
  3. Chapter 17: Mastered Deep Learning with Neural Networks ← You are here
  4. ➡️ Chapter 18: Moving into Advanced Data Visualization to tell stories with your data.

Immediate Practice:

  1. Try the TensorFlow Playground to visualize how neurons learn in real-time.
  2. Explore Hugging Face for thousands of pre-trained models you can use with TensorFlow.
  3. Set up a Google Colab notebook to experiment with free GPU access.

Chapter 18 Preview:

In the next chapter, we’ll master the art of Advanced Data Visualization. You’ll learn:

  • Building interactive charts with Plotly.
  • Creating statistical heatmaps with Seaborn.
  • Using Matplotlib for custom, publication-quality figures.
  • Bridging Python visualizations back into your PHP dashboards.

You’ll move from “calculating numbers” to “convincing stakeholders”!

::: tip Next Chapter Continue to Chapter 18: Data Visualization Mastery to create publication-quality visualizations! :::