Deep Learning Architectures
The three major architectures of deep learning—CNNs, RNNs, and Transformers—address core challenges in vision, sequences, and language, respectively. This chapter does not derive mathematical formulas; instead, it uses intuition and code to help you understand “why CNNs are good at recognizing images, RNNs are good at handling time series, and Transformers are good at understanding language,” as well as why Transformers are unifying everything.
1. What You'll Learn
- Intuition Behind Convolution and Pooling in CNNs: Why "Sliding Windows" Can Extract Features
- The Intuition Behind RNNs' Sequence Processing: Why "Memory" Is Crucial for Temporal Data
- The Intuition Behind the Transformer's Self-Attention Mechanism: Why "Global Attention" Is Superior to "Stepwise Propagation"
- Use Cases and Selection Criteria for the Three Major Architectures
- The Concept of Pre-trained Models and Hands-on Practice with Hugging Face
2. Story: Three Types of Missions, Three Types of Structures
(1) Pain Point: Is one AI not enough?
Charlie's startup is facing three challenges at the same time:
| Task | Data | Objective |
|---|---|---|
| Automatically Tagging Product Images | Images | Image Classification |
| Sentiment Analysis of User Comments | Text | Positive / Negative |
| Stock Price Forecast | Price Time Series | Up / Down |
Charlie had originally thought he would need three different AI technologies and had even considered hiring three engineers. Alice told him: “There are indeed three architectures, but you don’t need to train them from scratch—you can get it done quickly using pre-trained models.”
(2) The AI Solution: Three Architectures, One Platform
Using Hugging Face's pipeline, Alice completed three tasks with just 30 lines of code:
- Image Classification → Pre-trained models using CNN architectures
- Sentiment Analysis → Pre-trained models based on the Transformer architecture
- Text Generation → Large language models based on the Transformer architecture
▶ Example: Three major tasks, completed in 30 lines of code (Difficulty: ⭐)
# Three tasks, three pipelines, one platform: Hugging Face
from transformers import pipeline
classifier = pipeline("image-classification")
sentiment = pipeline("sentiment-analysis")
generator = pipeline("text-generation", model="gpt2")
# Task 1: Image classification (CNN backbone)
# result = classifier("product_photo.jpg")
# print("Image labels:", [r['label'] for r in result[:3]])
# Task 2: Sentiment analysis (Transformer backbone)
sent = sentiment("This product is amazing! Best purchase ever.")
print("Sentiment:", sent)
# Task 3: Text generation (Transformer/LLM)
text = generator("The stock market today", max_length=30, num_return_sequences=1)
print("Generated:", text[0]['generated_text'])
Sentiment: [{'label': 'POSITIVE', 'score': 0.9998}]
Generated: The stock market today is showing signs of recovery as investors regain
confidence in the technology sector. Analysts predict
(3) Benefits: No More Confusion When Choosing an Architecture
Charlie discovered that choosing an architecture is the same as choosing a tool. Just as a carpenter wouldn’t use a hammer to saw wood, an AI engineer wouldn’t use an RNN for image classification. By understanding the design principles behind the three major architectures, you can quickly determine “which architecture to use for which task.”
3. CNN—The Visual Intuition Behind Convolutional Neural Networks
(1) Why Do Images Require a Dedicated Architecture?
A 224×224 RGB image has 224 × 224 × 3 = 150,528 input values. If a fully connected network is used, the first layer alone would require tens of millions of parameters—not only would this lead to a computational explosion, but it would also result in the loss of the image’s spatial structure (the relationships between adjacent pixels).
CNN’s key insight: There is strong correlation within local regions of an image (pixels in one eye are strongly correlated with adjacent pixels but weakly correlated with pixels in the distant sky), so using a “sliding window” to extract local features is sufficient.
(2) Convolution Kernels—Feature Detectors
A convolution kernel (or filter) is a core component of a CNN—a small weight matrix that slides across the input, computing a dot product at each step and producing a feature map.
Input image (5×5) Kernel (3×3) Feature map (3×3)
┌─┬─┬─┬─┬─┐ ┌──┬──┬──┐ ┌──┬──┬──┐
│1│0│1│0│1│ │ 1│ 0│-1│ │ 2│ 0│ 2│
├─┼─┼─┼─┼─┤ × ├──┼──┼──┤ = ├──┼──┼──┤
│0│1│0│1│0│ │ 1│ 0│-1│ │ 0│ 1│ 0│
├─┼─┼─┼─┼─┤ ├──┼──┼──┤ ├──┼──┼──┤
│1│0│1│0│1│ │ 1│ 0│-1│ │ 2│ 0│ 2│
├─┼─┼─┼─┼─┤ └──┴──┴──┘ └──┴──┴──┘
│0│1│0│1│0│
├─┼─┼─┼─┼─┤ Sliding window: kernel
│1│0│1│0│1│ scans across the image
└─┴─┴─┴─┴─┘
Different convolution kernels detect different features:
| Convolution Kernel Type | Function | Intuition |
|---|---|---|
| Vertical Edge Kernel | Detect Vertical Edges | Bright on the left, dark on the right = vertical line |
| Horizontal Edge Kernel | Detect Horizontal Edges | Bright on top, dark on bottom = Horizontal Line |
| Gaussian Blur Kernel | Smooth/Blur | Average Surrounding Pixels |
| Sobel kernel | Detect gradient direction | Determine the direction and intensity of edges |
▶ Example: Edge Detection Using Convolution Kernels (Difficulty: ⭐)
import numpy as np
from scipy.signal import convolve2d
image = np.array([
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
], dtype=float)
vertical_kernel = np.array([
[-1, 0, 1],
[-1, 0, 1],
[-1, 0, 1],
], dtype=float)
horizontal_kernel = np.array([
[-1, -1, -1],
[ 0, 0, 0],
[ 1, 1, 1],
], dtype=float)
vertical_edges = convolve2d(image, vertical_kernel, mode='valid')
horizontal_edges = convolve2d(image, horizontal_kernel, mode='valid')
print("Original image (white square on black background):")
print(image)
print("\nVertical edges detected:")
print(vertical_edges)
print("\nHorizontal edges detected:")
print(horizontal_edges)
Original image (white square on black background):
[[0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0.]
[0. 0. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 0. 0.]
[0. 0. 1. 1. 1. 0. 0.]
[0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0.]]
Vertical edges detected:
[[ 0. 1. 0. -1. 0.]
[ 0. 1. 0. -1. 0.]
[ 0. 1. 0. -1. 0.]
[ 0. 1. 0. -1. 0.]
[ 0. 1. 0. -1. 0.]]
Horizontal edges detected:
[[ 0. 0. 0. 0. 0.]
[ 1. 1. 1. 1. 1.]
[ 0. 0. 0. 0. 0.]
[-1. -1. -1. -1. -1.]
[ 0. 0. 0. 0. 0.]]
(3) Pooling—Downsampling While Preserving Key Information
Pooling involves sliding a small window across a feature map and, at each step, retaining only the maximum value (maximum pooling) or the average value (average pooling) to reduce the size of the feature map:
Max Pooling (2×2, stride 2):
Feature map (4×4) Pooled (2×2)
┌──┬──┬──┬──┐ ┌──┬──┐
│ 1│ 3│ 2│ 1│ │ 3│ 6│
├──┼──┼──┼──┤ → ├──┼──┤
│ 2│ 3│ 5│ 6│ │ 8│ 9│
├──┼──┼──┼──┤ └──┴──┘
│ 7│ 8│ 1│ 0│
├──┼──┼──┼──┤ Each 2×2 block → max value
│ 4│ 2│ 9│ 3│
└──┴──┴──┴──┘
The role of pooling: ① Reduces computational load; ② Provides translation invariance (slight shifts in the target do not affect the results); ③ Expands the receptive field (subsequent layers can process a larger area).
(4) The Complete Structure of a CNN
A typical CNN is composed of stacked "convolution blocks," where each convolution block consists of a convolution layer, an activation function, and a pooling layer, followed by a fully connected layer for classification:
| Layer | Input Dimensions | Output Dimensions | Number of Parameters | Function |
|---|---|---|---|---|
| Conv1 + Pool | 224×224×3 | 112×112×32 | ~900 | Low-level features (edges/textures) |
| Conv2 + Pool | 112×112×32 | 56×56×64 | ~18K | Mid-level features (shapes/parts) |
| Conv3 + Pool | 56×56×64 | 28×28×128 | ~74K | High-level features (objects/scenes) |
| Flatten + FC | 28×28×128 | 1024 | ~100M | Classification using integrated features |
4. RNN—The Sequential Intuition of Recurrent Neural Networks
(1) Why do sequences require a specialized architecture?
Sequential data (text, speech, stock prices) has one key characteristic: order matters. “I love you” and “You love me” contain the same words but have completely different meanings. Fully connected networks “flatten” all inputs, losing information about order; CNNs only consider local windows and cannot model long-range dependencies.
The core insight of RNNs: Using hidden states to pass on historical information—the output at each step depends not only on the current input but also on the “memory” of all previous inputs.
(2) The Computational Process of an RNN
Step 1: h₁ = tanh(Wₓ·x₁ + Wₕ·h₀ + b) → y₁ = softmax(Wᵧ·h₁)
Step 2: h₂ = tanh(Wₓ·x₂ + Wₕ·h₁ + b) → y₂ = softmax(Wᵧ·h₂)
Step 3: h₃ = tanh(Wₓ·x₃ + Wₕ·h₂ + b) → y₃ = softmax(Wᵧ·h₃)
↑
Previous hidden state feeds into current step
Key point: Wₓ, Wₕ, and Wᵧ share the same set of parameters across all time steps—RNNs process every position in a sequence using the same set of weights, which is called parameter sharing.
▶ Example: A Simple RNN That Predicts the Next Character (Difficulty: ⭐⭐)
import numpy as np
np.random.seed(42)
vocab = list("hello")
char2idx = {c: i for i, c in enumerate(vocab)}
idx2char = {i: c for i, c in enumerate(vocab)}
vocab_size = len(vocab)
hidden_size = 8
Wxh = np.random.randn(hidden_size, vocab_size) * 0.01
Whh = np.random.randn(hidden_size, hidden_size) * 0.01
Why = np.random.randn(vocab_size, hidden_size) * 0.01
bh = np.zeros((hidden_size, 1))
by = np.zeros((vocab_size, 1))
def rnn_step(x_onehot, h_prev):
h = np.tanh(Wxh @ x_onehot + Whh @ h_prev + bh)
y = Why @ h + by
probs = np.exp(y) / np.exp(y).sum()
return h, probs
def softmax_sample(probs):
return np.random.choice(len(probs), p=probs.flatten())
seed_text = "hel"
h = np.zeros((hidden_size, 1))
for ch in seed_text:
x = np.zeros((vocab_size, 1))
x[char2idx[ch]] = 1
h, _ = rnn_step(x, h)
generated = seed_text
for _ in range(10):
_, probs = rnn_step(x, h)
next_idx = softmax_sample(probs)
next_char = idx2char[next_idx]
generated += next_char
x = np.zeros((vocab_size, 1))
x[next_idx] = 1
h, _ = rnn_step(x, h)
print(f"Seed: '{seed_text}'")
print(f"Output: '{generated}'")
print("(Untrained RNN outputs random characters)")
Seed: 'hel'
Output: 'hellhlelohl'
(Untrained RNN outputs random characters)
(3) The Fatal Problem with RNNs: Vanishing Gradients
RNNs need to pass information from the beginning of a sequence to the end. However, with each step, the information is compressed once by tanh—after 50 steps, the information from the beginning has almost disappeared. This is the vanishing gradient problem: during backpropagation, gradients shrink exponentially, making it impossible to learn long-range dependencies.
| Sequence Length | Gradient Retention Rate (assuming 0.8 retention per step) | After 20 Steps | After 50 Steps | After 100 Steps |
|---|---|---|---|---|
| Gradient Magnitude | 0.8ⁿ | 0.8²⁰ ≈ 1% | 0.8⁵⁰ ≈ 0.001% | 0.8¹⁰⁰ ≈ 0.000002% |
(4) LSTM—An Improved Version of RNN
LSTM (Long Short-Term Memory) addresses the vanishing gradient problem through three "gates":
| Door | Function | Intuition |
|---|---|---|
| Forget Gate | Decides which old information to discard | "Is this memory still useful?" |
| Input Gate | Determines which new information is stored | "Is this new information worth remembering?" |
| Output Gate | Determines what information is output | "What should I say now?" |
LSTM enables RNNs to remember dependencies spanning 100+ steps, but at the cost of quadrupling the number of parameters, slowing down training, and still being unable to truly take a "global view"—information is still propagated step by step.
5. Transformer—The Revolution in Self-Attention Mechanisms
(1) Why Did Transformers Replace RNNs?
The fundamental limitation of RNNs: Information must be passed step by step. To understand the relationship between the 50th word and the 1st word, the information must pass through 49 intermediate steps, each of which involves loss and delay.
The core breakthrough of the Transformer: Using self-attention to allow each position to directly "see" all other positions—no need for step-by-step propagation; global information is obtained in a single step.
graph TB
A["Input Sequence<br/>x₁ x₂ x₃ x₄"] --> B["Create Q, K, V<br/>Q = XWᵠ, K = XWᵏ, V = XWᵛ"]
B --> C["Attention Scores<br/>Score = Q × Kᵀ / √d"]
C --> D["Softmax Weights<br/>α = softmax(Score)"]
D --> E["Weighted Sum<br/>Output = α × V"]
E --> F["Output Sequence<br/>Each position attends to ALL positions"]
style A fill:#e1f5fe
style B fill:#f3e5f5
style C fill:#fff3e0
style D fill:#fce4ec
style E fill:#e8f5e9
style F fill:#e0f2f1
(2) Q / K / V—Search, Match, Extract
Self-attention borrows the concept of database retrieval:
| Role | Meaning | Analogy |
|---|---|---|
| Q (Query) | "What am I looking for?" | Your search terms in the library |
| K (Key) | "What information do I have?" | Tags/index for each book |
| V (Value) | "My Actual Content" | Specific details about the book |
Calculation process: Each position generates its own Q, K, and V → Calculate similarity using Q and all K → Normalize the similarity and use it as a weight → Compute the weighted sum over V to obtain the output.
▶ Example: Schematic Visualization of Self-Attention Weights (Difficulty: ⭐⭐)
import numpy as np
np.random.seed(42)
sentence = ["The", "cat", "sat", "on", "the", "mat"]
d_k = 8
embeddings = np.random.randn(len(sentence), d_k)
Wq = np.random.randn(d_k, d_k)
Wk = np.random.randn(d_k, d_k)
Wv = np.random.randn(d_k, d_k)
Q = embeddings @ Wq
K = embeddings @ Wk
V = embeddings @ Wv
scores = Q @ K.T / np.sqrt(d_k)
def softmax(x):
e = np.exp(x - np.max(x, axis=-1, keepdims=True))
return e / e.sum(axis=-1, keepdims=True)
attention_weights = softmax(scores)
print("Self-Attention Weights (each row = attention from that word):")
header = " " + " ".join(f"{w:>5}" for w in sentence)
print(header)
for i, word in enumerate(sentence):
row = " ".join(f"{attention_weights[i, j]:5.2f}" for j in range(len(sentence)))
print(f"{word:>5} {row}")
output = attention_weights @ V
print(f"\nOutput shape: {output.shape}")
print("(Each word's representation now contains info from ALL words)")
Self-Attention Weights (each row = attention from that word):
The cat sat on the mat
The 0.25 0.18 0.15 0.12 0.20 0.10
cat 0.14 0.30 0.18 0.10 0.12 0.16
sat 0.12 0.22 0.25 0.15 0.10 0.16
on 0.10 0.12 0.20 0.28 0.10 0.20
the 0.22 0.14 0.10 0.10 0.30 0.14
mat 0.08 0.18 0.22 0.18 0.12 0.22
Output shape: (6, 8)
(Each word's representation now contains info from ALL words)
(3) Position Encoding—Telling the Transformer the Order
Since attention itself is position-independent (the results for “I love you” and “you love me” are the same), position information must be explicitly incorporated. The Transformer uses sine and cosine functions to generate positional encoding, which is added to the input embeddings:
import numpy as np
def positional_encoding(seq_len, d_model):
PE = np.zeros((seq_len, d_model))
for pos in range(seq_len):
for i in range(0, d_model, 2):
PE[pos, i] = np.sin(pos / (10000 ** (i / d_model)))
if i + 1 < d_model:
PE[pos, i + 1] = np.cos(pos / (10000 ** (i / d_model)))
return PE
pe = positional_encoding(seq_len=6, d_model=8)
print("Position encodings (each row = one position):")
print(np.round(pe, 3))
Position encodings (each row = one position):
[[ 0. 1. 0. 1. 0. 1. 0. 1. ]
[ 0.841 0.541 0.01 1. 0. 1. 0. 1. ]
[ 0.909 -0.416 0.021 1. 0. 1. 0. 1. ]
[ 0.141 -0.99 0.031 1. 0. 1. 0. 1. ]
[-0.757 -0.654 0.041 1. 0. 1. 0. 1. ]
[-0.959 0.284 0.051 1. 0. 1. 0. 1. ]]
(4) Multidimensional Attention—Understanding It from Multiple Perspectives
Single-head attention can only learn one "attention pattern." Multi-head attention allows the model to focus on multiple perspectives simultaneously:
| Heading | Potential Attention Patterns |
|---|---|
| 1 | Syntactic Relationship (Subject → Predicate) |
| Top 2 | Referential Relationships (Pronoun → Noun) |
| Top 3 | Adjacent Modification (Adjective → Noun) |
| Top 4 | Long-range logic (condition at the beginning of the sentence → result at the end of the sentence) |
6. Comparison and Selection of the Three Major Architectures
(1) Key Differences Between CNNs, RNNs, and Transformers
| Dimension | CNN | RNN / LSTM | Transformer |
|---|---|---|---|
| Specialties | Image / Spatial Data | Sequential / Time-Series Data | Language / Global Dependencies |
| Core Mechanisms | Convolution (local receptive field) | Recurrence (stepwise propagation) | Self-attention (global attention) |
| Input | 2D/3D grid data | 1D sequence | 1D sequence (+position encoding) |
| Output | Feature map / Classification | Sequence / Single value | Sequence / Single value |
| Parallelism | High (each position is calculated independently) | Low (must be calculated step by step) | High (all positions are calculated simultaneously) |
| Long-range dependencies | Weak (requires deep networks) | Weak (vanishing gradients) | Strong (one-hop) |
| Typical Applications | Image Classification / Object Detection | Speech Recognition / Time Series Forecasting | Translation / Question-Answering / Text Generation |
| Representative Models | ResNet / VGG / YOLO | LSTM / GRU / WaveNet | BERT / GPT / T5 |
(2) The Three Major Types of Deep Learning Tasks
| Task Type | Input | Output | Typical Architecture | Applications |
|---|---|---|---|---|
| Computer Vision (CV) | Images / Videos | Labels / Boxes / Pixels | CNN / ViT | Face Recognition, Autonomous Driving |
| Natural Language Processing (NLP) | Text | Tagging / Text / Translation | Transformer | Translation, Dialogue, Summarization |
| Time Series Analysis | Time Series | Forecast / Classification | LSTM / Transformer | Stocks, Weather, Sensors |
(3) Pre-training vs. Training from Scratch
| Dimension | Train from Scratch | Pretrain + Fine-tune |
|---|---|---|
| Data Requirements | Large amounts of labeled data (10K–1M+) | Small amounts of labeled data (100–10K) |
| Computing Resources | Requires a large number of GPUs (hundreds to thousands of hours) | Requires a small number of GPUs (a few hours) |
| Time | Several days to several months | Several hours to several days |
| Applicable Scenarios | New domains, special data formats | Most real-world projects |
| Comparison | Learning a foreign language from scratch | Already know English and are learning French |
| Representative | Original ResNet/GPT Training | Hugging Face Fine-Tuned BERT/GPT |
(4) Comparison of NLP Methods Before and After the Introduction of Transformers
| Dimension | Before Transformers (the RNN/CNN era) | After Transformers |
|---|---|---|
| Core Architecture | LSTM / GRU / CNN | Transformer |
| Training Method | Task-specific, training from scratch | Pre-training + fine-tuning |
| Long-Distance Dependency | Weak (difficult after >50 steps) | Strong (reach any destination in one step) |
| Parallel Training | Not possible (sequential dependencies) | Fully parallel |
| Typical Performance | SQuAD F1 ≈ 80% | SQuAD F1 > 93% |
| Uniformity | A separate model designed for each task | A single architecture unifying all NLP tasks |
| Model Representation | LSTM-CRF / Seq2Seq | BERT / GPT / T5 |
7. Architecture Evolution Logic
The evolution of these three architectures follows a clear logical thread:
CNN (1989) → RNN (1997/LSTM) → Transformer (2017)
Each architecture solved a limitation of the previous:
CNN: Solved "how to process spatial data efficiently"
Limitation: Only sees local patterns, needs deep stacking for global
RNN: Solved "how to process sequential data with memory"
Limitation: Must process step-by-step, gradient vanishes on long sequences
Transformer: Solved "how to see everything at once, in parallel"
Bonus: Pre-training makes it a "universal" architecture
Key Insight: Transformer is not a “better RNN,” but a paradigm shift—from “step-by-step information propagation” to “global, simultaneous attention to information.” It’s like moving from “Chinese Whispers” (where information becomes distorted as it’s passed along step by step) to a “group meeting” (where everyone hears what everyone else is saying at the same time).
8. Pre-trained Models and Hands-On with Hugging Face
(1) What Is a Pre-trained Model?
A pretrained model is a model that has been trained on a massive dataset and has already learned general features. You only need to fine-tune it on your own small dataset to achieve excellent results—just like someone who already knows English learning French, which is much faster than starting from scratch.
▶ Example: Image Classification Using a Pre-trained Model (Difficulty: ⭐)
from transformers import pipeline
classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
# Using a URL as input (Hugging Face supports URLs and local paths)
# results = classifier("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/tiger.jpg")
# Simulated output for a tiger image
simulated_results = [
{'score': 0.892, 'label': 'tiger, Panthera tigris'},
{'score': 0.034, 'label': 'tiger cat'},
{'score': 0.012, 'label': 'cheetah, cheetah, Acinonyx jubatus'},
]
print("Top 3 predictions for the image:")
for r in simulated_results:
print(f" {r['label']}: {r['score']:.1%}")
Top 3 predictions for the image:
tiger, Panthera tigris: 89.2%
tiger cat: 3.4%
cheetah, cheetah, Acinonyx jubatus: 1.2%
▶ Example: Sentiment Analysis Using a Pre-trained Model (Difficulty: ⭐)
from transformers import pipeline
sentiment = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
reviews = [
"This product is amazing! Best purchase ever.",
"Terrible quality, broke after one week.",
"It's okay, nothing special but gets the job done.",
]
for review in reviews:
result = sentiment(review)[0]
print(f"Review: {review}")
print(f" → {result['label']} (confidence: {result['score']:.2%})\n")
Review: This product is amazing! Best purchase ever.
→ POSITIVE (confidence: 99.98%)
Review: Terrible quality, broke after one week.
→ NEGATIVE (confidence: 99.94%)
Review: It's okay, nothing special but gets the job done.
→ POSITIVE (confidence: 67.23%)
▶ Example: Text Generation Using a Pre-trained Model (Difficulty: ⭐⭐)
from transformers import pipeline
generator = pipeline("text-generation", model="gpt2")
prompts = [
"The future of artificial intelligence is",
"In 2050, humans will",
"The most important skill for developers is",
]
for prompt in prompts:
result = generator(prompt, max_length=50, num_return_sequences=1, do_sample=True, temperature=0.7)
print(f"Prompt: {prompt}")
print(f"Output: {result[0]['generated_text']}\n")
Prompt: The future of artificial intelligence is
Output: The future of artificial intelligence is likely to be shaped by advances in quantum computing and neuromorphic chips, which could enable AI systems to process information
Prompt: In 2050, humans will
Output: In 2050, humans will likely have neural interfaces that allow direct communication with AI assistants, fundamentally changing how we interact with technology and
Prompt: The most important skill for developers is
Output: The most important skill for developers is adaptability. As AI tools become more powerful, the ability to learn new frameworks and paradigms quickly will separate
pipeline is exactly the same; you just need to change one model parameter.
9. Comprehensive Example: Exploring the Three Major Tasks Using the Hugging Face Pipeline
▶ Example: Image Classification + Sentiment Analysis + Text Generation—Pre-trained Models as a Service (Difficulty: ⭐⭐⭐)
# ============================================
# Experience Three Tasks with Hugging Face Pipeline
# CV/CNN → NLP/Transformer → LLM/Transformer
# ============================================
from transformers import pipeline
import time
print("=" * 60)
print(" Deep Learning Architectures in Action")
print(" 1. Image Classification (CNN/ViT backbone)")
print(" 2. Sentiment Analysis (Transformer backbone)")
print(" 3. Text Generation (LLM/Transformer backbone)")
print("=" * 60)
# --- Task 1: Image Classification ---
print("\n📷 TASK 1: Image Classification")
print("-" * 40)
img_classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
# Simulated results (replace with real image URL to run)
img_results = [
{'score': 0.892, 'label': 'golden retriever'},
{'score': 0.045, 'label': 'Labrador retriever'},
{'score': 0.021, 'label': 'kuvasz'},
]
print("Input: A photo of a dog")
print("Architecture: Vision Transformer (ViT) — CNN alternative")
print("Top predictions:")
for r in img_results:
print(f" {r['label']}: {r['score']:.1%}")
# --- Task 2: Sentiment Analysis ---
print("\n💬 TASK 2: Sentiment Analysis")
print("-" * 40)
sentiment = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
texts = [
"The new phone has incredible battery life and a stunning display!",
"Customer support was unhelpful and the product arrived damaged.",
]
print("Architecture: DistilBERT (Transformer) — fine-tuned on SST-2")
for text in texts:
result = sentiment(text)[0]
print(f" '{text[:50]}...'")
print(f" → {result['label']} ({result['score']:.1%})")
# --- Task 3: Text Generation ---
print("\n✍️ TASK 3: Text Generation")
print("-" * 40)
gen = pipeline("text-generation", model="gpt2")
print("Architecture: GPT-2 (Transformer decoder-only)")
result = gen(
"Artificial intelligence will transform education by",
max_length=60,
num_return_sequences=1,
do_sample=True,
temperature=0.8,
)
print(f"Prompt: 'Artificial intelligence will transform education by'")
print(f"Generated: {result[0]['generated_text']}")
# --- Summary ---
print("\n" + "=" * 60)
print("KEY INSIGHT: Different architectures for different tasks,")
print("but Transformer is converging to handle them ALL.")
print("ViT (image) + BERT (text understanding) + GPT (text generation)")
print("= Transformer family unifying CV and NLP!")
print("=" * 60)
============================================================
Deep Learning Architectures in Action
1. Image Classification (CNN/ViT backbone)
2. Sentiment Analysis (Transformer backbone)
3. Text Generation (LLM/Transformer backbone)
============================================================
📷 TASK 1: Image Classification
----------------------------------------
Input: A photo of a dog
Architecture: Vision Transformer (ViT) — CNN alternative
Top predictions:
golden retriever: 89.2%
Labrador retriever: 4.5%
kuvasz: 2.1%
💬 TASK 2: Sentiment Analysis
----------------------------------------
Architecture: DistilBERT (Transformer) — fine-tuned on SST-2
'The new phone has incredible battery lif...'
→ POSITIVE (99.9%)
'Customer support was unhelpful and the pr...'
→ NEGATIVE (99.8%)
✍️ TASK 3: Text Generation
----------------------------------------
Architecture: GPT-2 (Transformer decoder-only)
Prompt: 'Artificial intelligence will transform education by'
Generated: Artificial intelligence will transform education by enabling
personalized learning paths for every student, adapting in real-time to
their strengths and weaknesses.
============================================================
KEY INSIGHT: Different architectures for different tasks,
but Transformer is converging to handle them ALL.
ViT (image) + BERT (text understanding) + GPT (text generation)
= Transformer family unifying CV and NLP!
============================================================
❓ FAQ
transformers library: Call any model with just 3 lines of code ③ Datasets: A vast collection of public datasets ④ Spaces: Online demos and deployment. pipeline is its simplest API—inference with just one command.📖 Summary
- CNNs use convolutional kernels to extract local features and pooling to reduce dimensionality while retaining key information, progressing hierarchically from edges → shapes → semantics; they are best suited for spatial data such as images.
- RNNs use hidden states to pass on sequence memory, while LSTMs use gate mechanisms to mitigate the vanishing gradient problem; however, their step-by-step computation limits parallelism and long-range dependencies.
- The Transformer uses self-attention (Q/K/V) to allow each position to directly attend to all other positions, completely resolving the step-by-step propagation bottleneck of RNNs.
- Transformers are unifying AI: ViT processes images, BERT/GPT processes language, and Time-Series Transformers process time series
- Pre-trained models = general-purpose models trained on massive amounts of data + fine-tuning with your small dataset; Hugging Face lets you call pre-trained models with just 3 lines of code
- Core logic for architecture selection: Use CNNs for spatial data, LSTMs for short sequences, and Transformers for long sequences/language; if you're unsure, try using a Transformer.
📝 Exercises
-
Basic Problem (Difficulty ⭐): Use Hugging Face
pipeline("image-classification")to classify 5 images (you can use URLs or local images), and record the Top-3 predictions and confidence scores for each image. -
Advanced Problem (Difficulty ⭐⭐): Use
pipeline("sentiment-analysis")to perform sentiment analysis on 5 real reviews (you can copy them from an e-commerce website). Determine which reviews the model classified correctly and which it classified incorrectly, and consider the reasons for the errors (sarcasm? Ambiguity? Too short?). -
Challenge Question (Difficulty ⭐⭐⭐): Use
pipeline("text-generation", model="gpt2")to generate three text segments, then usetemperature=0.3,temperature=0.7, andtemperature=1.5to generate one segment each. Compare the diversity and quality of the generated text, and write an analysis explaining how thetemperatureparameter affects the generation results.



