In 04-07 we built an MLP — a multilayer perceptron with one or two hidden layers — and left pending the acronyms that dominate the headlines: CNN, RNN, transformers. This lesson settles that debt. Deep learning is, in essence, the same neural network from 04-07 taken into depth: many stacked layers that learn hierarchical representations of the data. You'll understand exactly what depth contributes, why it only became practical about fifteen years ago, which concrete techniques make it trainable, which architecture fits each kind of data, and you'll train a deep network with Keras on MercaFresh's churn — to discover, honestly, when deep learning pays off and when the gradient boosting from the previous lesson remains the better choice.

Contents

  1. From the MLP to the deep network: what depth contributes
  2. Why it's possible now: GPUs, data and techniques
  3. The techniques that make it trainable
  4. Architectures and their domains: CNN, RNN/LSTM, transformers
  5. Example with Keras: MercaFresh churn
  6. Deep learning vs. gradient boosting on tabular data
  7. What comes next

From the MLP to the deep network: what depth contributes

Recall the MLP of 04-07: layers of neurons where each one computes a linear combination of its inputs followed by a non-linear activation, trained with backpropagation. In theory, a single sufficiently wide hidden layer can approximate any function (the universal approximation theorem). So why stack more layers?

Because depth doesn't provide more capacity in the abstract, but a better organized capacity: a hierarchy of representations. Each layer builds concepts on top of the previous one's:

  • In an image: layer 1 detects edges; layer 2 combines edges into textures and corners; layer 3, into parts (an eye, a wheel); layer 4, into whole objects.
  • In text: characters → words → syntax → meaning.
  • In audio: waveforms → phonemes → words.

This composition is exponentially more efficient than the brute force of one wide layer: it reuses intermediate concepts just as software reuses functions. And it has an enormous practical consequence that connects with 03-06: the network learns the feature engineering on its own. Where we hand-designed the RFM features for churn, a deep network on images discovers its own "visual RFM" layer by layer. That's why deep learning crushes on unstructured data (images, audio, text), where designing features by hand is nearly impossible.

flowchart LR
    P[Pixels] --> B[Layer 1: edges] --> T[Layer 2: textures] --> O[Layer 3: parts] --> C[Layer 4: object - cat]

Why it's possible now: GPUs, data and techniques

In 01-02 we recounted the AI winters: neural networks have existed since the 1950s-80s, but for decades deep networks were untrainable in practice. Their resurrection (~2012, with AlexNet winning ImageNet) rested on three simultaneous legs:

Leg What changed Why it matters
Hardware (GPUs) Graphics cards turned out to be perfect for multiplying matrices in parallel Training runs of months became days or hours
Data The internet produced massive labeled datasets (ImageNet: 14M images) Deep networks have millions of parameters: without massive data, they only memorize
Techniques ReLU, dropout, batch normalization, better initializations and optimizers They solved the mathematical problems that jammed training

No leg suffices alone: it's their historical coincidence that opened the current era. The first two are context; the third is engineering you need to know, and we unpack it next.

The techniques that make it trainable

Training 50 layers with the "naive" backpropagation of 04-07 fails on two fronts: the gradients vanish as they traverse so many layers (the correction signal arrives at the first layers as nothing) and the millions of parameters overfit greedily. These are the tools that solved it:

Technique What it does Problem it attacks
ReLU (max(0, x)) as activation Replaces sigmoid/tanh in hidden layers; its gradient is 1 for positive inputs Vanishing gradients: the signal no longer fades layer by layer
Dropout At each training step, randomly "switches off" a percentage of neurons (e.g. 30%) Overfitting: no neuron can depend on a specific other one; deep down it trains an implicit ensemble of subnetworks (an echo of the bagging in 07-02!)
Batch normalization Normalizes each layer's activations over the mini-batch (mean 0, deviation 1) Stabilizes and speeds up training: each layer receives inputs on a controlled scale — the idea of 03-05 applied inside the network
Early stopping Halts training when the validation loss stops improving Overfitting from too many epochs — the same medicine as in the boosting of 07-03

Notice the pattern: none of these ideas is really foreign to you. Dropout is regularization (07-01) with an ensemble flavor (07-02); batch normalization is standardization (03-05); early stopping you already used in 07-03. Deep learning recombines the whole course's arsenal at another scale.

Architectures and their domains: CNN, RNN/LSTM, transformers

A "dense" network (every neuron connected to every other, like our MLP) ignores the structure of the data. The great architectures of deep learning are ways of building that structure into the network's design:

CNN: convolutional networks (images)

A 1000×1000-pixel image has 3 million inputs; a dense layer would need billions of weights. The CNN solves it with the convolution: instead of connecting each neuron to the whole image, a small filter (e.g. 3×3 weights) slides across the entire image, computing at each position the product with the local patch. The same filter — the same 9 weights — is reused at every position.

flowchart LR
    I[6x6 image] -->|"3x3 filter slides"| M[4x4 activation map]
    M -->|"pooling: keep the local maximum"| R[2x2 summary]
    R --> S[Next layers: filters over filters]

Two intuitions justify it: (1) a vertical-edge detector is useful in any corner of the image — sharing weights encodes that invariance and slashes the parameter count —; (2) stacking convolutions builds exactly the edges → textures → parts → objects hierarchy from section 1. Between convolutions comes pooling (keeping the maximum of each zone), which summarizes and grants tolerance to small shifts. CNNs dominate computer vision: image classification, object detection, medical imaging. At MercaFresh: automatically classifying the photos suppliers upload to the catalog — and it's the architecture we'll use in project 09-02.

RNN and LSTM: sequences

For sequential data (time series, text), the recurrent network processes the elements in order while maintaining an internal state — a "memory" updated at every step. LSTMs (Long Short-Term Memory) add gates that decide what to remember and what to forget, fixing the inability of plain RNNs to retain long-range dependencies. Natural domain: forecasting daily demand for MercaFresh products, text (before 2018), audio, sensors.

Transformers and attention: language (and by now almost everything)

The transformer (2017, "Attention Is All You Need") replaced recurrence with the attention mechanism: each element in the sequence decides, with learned weights, which other elements to "look at" to build its representation — in "the order that Ana canceled yesterday", the word "canceled" attends strongly to "order" and to "Ana". By eliminating step-by-step processing, training parallelizes massively, which made it possible to scale to models with billions of parameters trained on a good chunk of the internet. They are the foundation of the large language models and today's generative AI we mentioned when closing the history in 01-02 — GPT, Claude, Gemini are giant transformers. Studying them in detail is beyond this course; what matters is that you can place them.

Architecture Structure it exploits Typical domain
Dense (MLP, 04-07) Nothing in particular Tabular data
CNN Spatial locality, translation invariance Images, video, signal
RNN / LSTM Temporal order, sequential dependencies Time series, audio
Transformer Relations between any positions (attention) Language, and increasingly images/audio/…

Example with Keras: MercaFresh churn

So far we've used sklearn's MLPClassifier (04-07). For real deep networks, the standard is dedicated frameworks; we'll use Keras (TensorFlow's high-level API) for its clarity. The full framework catalog — TensorFlow, PyTorch and company — is covered in 08-01; here one is enough to get our hands on this lesson's techniques.

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# --- MercaFresh churn dataset (the same as 07-02/07-03) ---
rng = np.random.default_rng(42)
n = 1000
recency = rng.gamma(2, 15, n)
frequency = rng.poisson(5, n) + 1
monetary = rng.gamma(3, 40, n)
tenure = rng.uniform(1, 60, n)
incidents = rng.poisson(0.5, n)
logits = 0.05 * recency - 0.4 * frequency + 0.6 * incidents - 0.01 * tenure - 0.5
y = (rng.random(n) < 1 / (1 + np.exp(-logits))).astype(int)
X = pd.DataFrame({"recency": recency, "frequency": frequency,
                  "monetary": monetary, "tenure": tenure,
                  "incidents": incidents})

# Split and scale with the discipline of 06-01: the scaler is fitted on train only
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, stratify=y, random_state=42)
scaler = StandardScaler().fit(X_train)
X_train_s, X_test_s = scaler.transform(X_train), scaler.transform(X_test)

# --- Deep network: 3 hidden layers with ReLU and dropout ---
tf.random.set_seed(42)
model = keras.Sequential([
    layers.Input(shape=(5,)),                 # 5 RFM features
    layers.Dense(64, activation="relu"),      # hidden layer 1
    layers.Dropout(0.3),                      # switches off 30% of neurons while training
    layers.Dense(32, activation="relu"),      # hidden layer 2
    layers.Dropout(0.3),
    layers.Dense(16, activation="relu"),      # hidden layer 3
    layers.Dense(1, activation="sigmoid"),    # output: churn probability
])

model.compile(
    optimizer="adam",                 # adaptive gradient descent (evolution of 04-01's)
    loss="binary_crossentropy",       # the log-loss of logistic regression (04-02)
    metrics=["accuracy"],
)

early_stop = keras.callbacks.EarlyStopping(
    monitor="val_loss", patience=20,          # patience: 20 epochs without improving
    restore_best_weights=True)                # recovers the weights from the best point

history = model.fit(
    X_train_s, y_train,
    validation_split=0.2,             # 20% of train as internal validation
    epochs=300, batch_size=32,
    callbacks=[early_stop], verbose=0,
)

print(f"Epochs run: {len(history.history['loss'])}")
loss, acc = model.evaluate(X_test_s, y_test, verbose=0)
print(f"Test accuracy: {acc:.3f}")

Reading the code, piece by piece:

  • Sequential stacks layers in order; Dense is the fully connected layer of the MLP from 04-07. Three hidden layers (64→32→16) already make a modest "deep" network.
  • compile sets the optimizer (Adam: the gradient descent of 04-01 with adaptive per-parameter steps) and the loss (binary cross-entropy is exactly the cost function of the logistic regression of 04-02: the sigmoid output of the last layer is a logistic regression over the learned representations).
  • fit trains by epochs (full passes over the train set) in mini-batches of 32 examples, setting aside 20% for validation; the early-stopping callback watches that validation and restores the best weights, just as we did with boosting in 07-03.
  • history.history stores the train/validation loss curves per epoch: plotting them with matplotlib reproduces the learning curves of 06-05 and is the first diagnostic for any network.

If you compare the result with the HistGradientBoosting of 07-03 on these same data, the network normally won't beat it (and takes longer). It's not a bug in the code: it's the lesson of the next section.

Deep learning vs. gradient boosting on tabular data

Time to be honest, because the marketing isn't. On tabular data — tables of customers, orders, RFM features: the daily bread of MercaFresh and of most companies — the empirical evidence is consistent: gradient boosting wins or ties almost always, with less effort.

Criterion Gradient boosting (07-03) Deep learning
Small/medium tabular data (thousands-millions of rows) Wins almost always Rarely pays off
Images, audio, text, video Not competitive Unrivaled
Data size needed Works from hundreds of rows Needs abundant data
Data preparation Minimal (doesn't even need scaling) Scaling, plus sensitivity to everything
Training and tuning cost Seconds-minutes, CPU Minutes-hours-weeks, ideally GPU
Feature engineering Manual (03-06) still adds value Learns it on its own (its superpower)
Interpretability Medium (importances, SHAP) Low

The practical rule for MercaFresh and for your career:

  • Does your data fit naturally in a pandas DataFrame? Start with gradient boosting (with the Random Forest of 07-02 as the baseline). Consider a network only if you have vast amounts of data and boosting falls short.
  • Is your data images, audio or free text? Deep learning, no debate — and today, almost always, starting from a pretrained model rather than training from scratch (a technique, transfer learning, we'll touch on in the image project 09-02, where we'll finally build a CNN end to end).

What comes next

With this lesson you have the complete map of deep learning at the informed-user level: you know what depth contributes, which techniques make it possible and which architecture fits each kind of data. Going deep into each architecture fills entire courses; in this course, the framework ecosystem (TensorFlow, PyTorch and their world) is cataloged in 08-01, and real hands-on practice with CNNs arrives in 09-02.

Common Mistakes and Tips

  • Using deep learning by default on tabular data. The fashionable mistake. For MercaFresh's churn, a deep network takes longer, demands more tuning and will probably perform the same as or worse than LightGBM. Choose the tool for the data, not for the headline.
  • Forgetting to scale the inputs. Unlike trees, networks are very sensitive to scale (gradient descent struggles with features of wildly different magnitudes). StandardScaler fitted on train only, always — the discipline of 03-05 and 06-01.
  • Training with no validation set and no early stopping. A network with thousands of parameters memorizes the train set with relish; validation_split + EarlyStopping is the minimum seatbelt.
  • Reading the training accuracy as performance. With dropout active, the train metric during fit is distorted (part of the network is switched off); always look at the val_loss/val_accuracy curves.
  • Huge networks for tiny datasets. With 1,000 customers, a 4-layer network with hundreds of neurons is a flamethrower to light a candle; if you insist on a network, make it small and with generous dropout.
  • Tip: for any network training run, always plot history.history["loss"] and ["val_loss"]. Divergence between the two = overfitting (more dropout, smaller network, stop earlier); both high and flat = underfitting or a badly chosen learning rate. It's the diagnosis of 06-05 applied epoch by epoch.

Exercises

  1. Training curves. Retrain the example's network without dropout (remove the Dropout layers) and without early stopping, with a fixed epochs=300. Plot loss and val_loss per epoch alongside those of the original network. Which pattern from 06-05 appears in the unregularized network, and how do dropout and early stopping correct it?
  2. Depth vs. width. With roughly the same neuron budget (~112), compare three architectures on the scaled churn data: (a) a single layer of 112, (b) 64→32→16 (the example's), (c) 6 layers of ~19 neurons. Use early stopping and evaluate test accuracy with 5 different seeds each. Does depth win on this tabular problem? Why was that to be expected?
  3. The definitive duel. Pit the example's network against HistGradientBoostingClassifier (07-03) on the same train/test: compare test F1 (use a 0.5 threshold on the network's probabilities), training time (time.perf_counter) and lines of preparation code. Write in three sentences the recommendation you'd make to MercaFresh's data team.

Solutions

  1. Without dropout or early stopping, loss falls monotonically while val_loss hits a minimum early and then climbs: the classic overfitting divergence (the same silhouette as the validation curve of 06-05 and as exercise 3 of 07-03). With dropout, both curves move forward closer together (the train set can't even be memorized, because each step sees a different subnetwork); early stopping additionally cuts off near the validation minimum and restores those weights. Regularization and early stopping attack the same symptom through complementary routes.
  2. With 5 tabular features and 1,000 rows, the three architectures come out statistically tied (the differences between seeds exceed the differences between architectures); the deepest one is often the most unstable. It was to be expected: the hierarchy of representations shines when the data has compositional structure (pixels→edges→objects); five RFM features already are the representation — there is no hierarchy to learn. Depth pays on unstructured data, not here.
  3. Typical result: similar F1 or in favor of boosting, with boosting training in a fraction of the time, without scaling and with less code. A reasonable recommendation: "For churn and MercaFresh's other tabular problems, keep gradient boosting as the standard tool; reserve deep learning for the catalog image classifier and other unstructured data; revisit the decision if data volume grows by orders of magnitude."

Conclusion

Deep learning is the MLP of 04-07 raised to a hierarchy: layers building representations on top of representations, trainable thanks to a handful of concrete techniques — ReLU against vanishing gradients, dropout and early stopping against overfitting, batch normalization for stability — and to the historical conjunction of GPUs and massive data we've traced since 01-02. Each architecture encodes its data's structure: CNNs for images, RNN/LSTM for sequences, transformers and their attention for language and today's generative AI. With Keras you trained your first deep network on MercaFresh's churn and drew the mature conclusion: on tabular data, gradient boosting still rules; the networks' kingdom is images, audio and text — and we'll truly visit it in project 09-02. Meanwhile, one outstanding debt runs through the whole module: alpha, n_estimators, learning rate, layers, dropout… every technique has added hyperparameters that so far we've chosen by eye. The module's final lesson brings order: systematic hyperparameter optimization.

Machine Learning Course

Module 1: Introduction to Machine Learning

Module 2: Foundations of Statistics and Probability

Module 3: Data Preprocessing

Module 4: Supervised Machine Learning Algorithms

Module 5: Unsupervised Machine Learning Algorithms

Module 6: Model Evaluation and Validation

Module 7: Advanced Techniques and Optimization

Module 8: Model Implementation and Deployment

Module 9: Hands-On Projects

Module 10: Additional Resources

© Copyright 2026. All rights reserved