404 Not Found

404 Not Found


nginx

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


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:

▶ Example: Three major tasks, completed in 30 lines of code (Difficulty: ⭐)

PYTHON
# 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'])
💻 Output:

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.

TEXT
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: ⭐)

PYTHON
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)
💻 Output:

TEXT
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.]]
💡 Tip: The vertical kernel detects the left and right edges of the white square (positive and negative values indicate direction), while the horizontal kernel detects the top and bottom edges. In a CNN, the weights of these kernels are learned automatically—the network discovers on its own which kernels are most useful.

(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:

TEXT
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
💡 Tip: The core idea behind CNNs is hierarchical feature extraction: lower layers learn edges → middle layers learn shapes → higher layers learn semantics. This is strikingly similar to how the human visual cortex works—Hubel and Wiesel’s 1962 Nobel Prize-winning research discovered a similar mechanism.


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

TEXT
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: ⭐⭐)

PYTHON
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)")
💻 Output:

TEXT
Seed:   'hel'
Output: 'hellhlelohl'
(Untrained RNN outputs random characters)
💡 Tip: An untrained RNN outputs random characters. After training, it learns patterns such as "an 'l' is often followed by an 'l'" or "an 'o' may be followed by an 'h'." The power of an RNN lies in its ability to use sequence history to make predictions.

(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.

100%
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: ⭐⭐)

PYTHON
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)")
💻 Output:

TEXT
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)
💡 Tip: In a trained Transformer, "cat" pays close attention to "sat" and "mat" (subject-verb-preposition relationship), while "The" and "the" pay attention to each other (article agreement). The attention weights are learned automatically—and that is precisely the secret behind how Transformers understand language.

(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:

PYTHON
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))
💻 Output:

TEXT
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.   ]]
💡 Tip: Each position has a unique encoding pattern; adjacent positions have similar encodings, while distant positions have significantly different encodings—this allows the Transformer to distinguish between "I love you" and "You love me."

(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:

TEXT
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: ⭐)

PYTHON
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%}")
💻 Output:

TEXT
Top 3 predictions for the image:
  tiger, Panthera tigris: 89.2%
  tiger cat: 3.4%
  cheetah, cheetah, Acinonyx jubatus: 1.2%
💡 Tip: This model (ViT) stands for Vision Transformer—which shows that Transformers have not only unified NLP but are also encroaching on the territory of CNNs! ViT divides images into small chunks and processes them as "words," using self-attention to learn image features.

▶ Example: Sentiment Analysis Using a Pre-trained Model (Difficulty: ⭐)

PYTHON
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")
💻 Output:

TEXT
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%)
💡 Tip: The third comment is emotionally ambiguous, and the model assigned it a 67% positive rating—this shows that pre-trained models are not “omnipotent” and will lower their confidence when dealing with ambiguous text. In actual business operations, when confidence falls below the threshold, the case should be referred to manual review.

▶ Example: Text Generation Using a Pre-trained Model (Difficulty: ⭐⭐)

PYTHON
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")
💻 Output:

TEXT
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
⚠️ Note: GPT-2 is a 2019 model, and its output quality is limited. Modern models (GPT-4, Claude, Qwen) are of much higher quality, but they require API calls rather than running locally. The usage on Hugging Face 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: ⭐⭐⭐)

PYTHON
# ============================================
# 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)
💻 Output:

TEXT
============================================================
  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

Q Why did Transformers replace RNNs?
A For three reasons: ① Parallelism—RNNs must compute step by step, while Transformers compute all positions simultaneously, making training 10 to 100 times faster; ② Long-range dependencies—Information propagates step by step in RNNs and can decay, whereas Transformers use self-attention to reach any position in a single step; ③ Pre-training-friendly—The Transformer’s parallelism makes pre-training on massive datasets possible, whereas RNNs are too slow to train effectively.
Q Can CNNs only process images?
A No. CNNs are also used for: ① text classification (1D convolutions slide over word sequences to extract n-gram features); ② speech recognition (1D convolutions extract acoustic features from audio waveforms); ③ recommendation systems (feature interaction); ④ time series (1D convolutions detect local patterns). The core of a CNN is “local feature extraction + parameter sharing”; as long as the data exhibits local correlations, a CNN can be useful.
Q What is a pre-trained model?
A A pre-trained model is one that has been trained on massive amounts of data (typically billions of words or images) and has already learned general features (such as the grammatical structure of language or the edges and textures of images). To use them, you simply need to: ① use them directly (zero-shot) ② fine-tune them on your own small dataset ③ use them as feature extractors. It’s like hiring a college graduate—with just a little training, they’re ready to work, which is much faster than training someone from scratch.
Q What resources are needed to train a Transformer on your own?
A It depends on the scale. Training a GPT-2-level model (150 million parameters) requires 8 V100 GPUs and takes about 1 day, at a cost of approximately $500. Training a GPT-3-level model (175 billion parameters) requires thousands of A100 GPUs running for several weeks, costing millions of dollars. Most developers don’t need to train from scratch—they can simply fine-tune Hugging Face’s pre-trained models, which can be done in a few hours on a single GPU.
Q What is Hugging Face?
A Hugging Face is the "GitHub" of the AI field—a hosting platform for open-source models and datasets. Key features: ① Model Hub: Over 500K pre-trained models (BERT, GPT, Stable Diffusion, etc.) ② 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.
Q Which is better, ViT or CNN?
A Each has its own advantages. ViT outperforms CNN on large datasets (>1 million images) because self-attention can learn global relationships; however, CNN still has the edge on small datasets because the inductive biases of convolution (locality and translation invariance) are more efficient with small amounts of data. In practice, the two are often combined: ConvNeXt (a CNN reimagined using Transformer design principles), ViT + CNN hybrid architectures, and so on. There is no “absolute best”; only “best suited for the current task.”

📖 Summary


📝 Exercises

  1. 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.

  2. 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?).

  3. Challenge Question (Difficulty ⭐⭐⭐): Use pipeline("text-generation", model="gpt2") to generate three text segments, then use temperature=0.3, temperature=0.7, and temperature=1.5 to generate one segment each. Compare the diversity and quality of the generated text, and write an analysis explaining how the temperature parameter affects the generation results.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏