Skip to content

🏗️ Architectures Overview

Level: Core Solves: Hiểu các neural network architectures phổ biến và khi nào sử dụng chúng

Architecture Selection Framework

💡 Giáo sư Tom

Đừng chọn architecture vì nó "hot" - chọn vì nó phù hợp với data và problem của bạn. CNNs vẫn beat Transformers cho nhiều vision tasks khi data limited. Hiểu trade-offs là key.

┌─────────────────────────────────────────────────────────────────┐
│              ARCHITECTURE SELECTION GUIDE                       │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Data Type              Recommended Architecture                │
│  ─────────              ────────────────────────                │
│                                                                 │
│  Images (fixed size)    CNN (ResNet, EfficientNet)              │
│  Images (variable)      ViT, CNN + adaptive pooling             │
│  Sequences (short)      RNN/LSTM, 1D CNN                        │
│  Sequences (long)       Transformer, State Space Models         │
│  Text                   Transformer (BERT, GPT)                 │
│  Audio                  CNN + RNN, Transformer                  │
│  Graphs                 GNN (GCN, GAT)                          │
│  Tabular                MLP, TabNet, Tree-based (non-DL)        │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Convolutional Neural Networks (CNNs)

Core Concepts

┌─────────────────────────────────────────────────────────────────┐
│                    CNN BUILDING BLOCKS                          │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  CONVOLUTION LAYER                                              │
│  ─────────────────                                              │
│  • Learns local patterns (edges, textures, shapes)              │
│  • Parameter sharing → translation equivariance                 │
│  • Key params: kernel_size, stride, padding, channels           │
│                                                                 │
│  ┌─────────┐     ┌───┐     ┌─────────┐                          │
│  │ Input   │  *  │ K │  =  │ Feature │                          │
│  │ H×W×C   │     │3×3│     │  Map    │                          │
│  └─────────┘     └───┘     └─────────┘                          │
│                                                                 │
│  POOLING LAYER                                                  │
│  ─────────────                                                  │
│  • Reduces spatial dimensions                                   │
│  • Max pooling: takes max value (most common)                   │
│  • Average pooling: takes mean value                            │
│  • Global pooling: reduces to 1×1 (before classifier)           │
│                                                                 │
│  NORMALIZATION                                                  │
│  ─────────────                                                  │
│  • BatchNorm: normalize across batch (standard for CNNs)        │
│  • GroupNorm: normalize across groups (small batch sizes)       │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

CNN Architecture Evolution

ArchitectureYearKey InnovationUse Case
LeNet1998First practical CNNDigit recognition
AlexNet2012ReLU, Dropout, GPUImageNet breakthrough
VGG2014Deep, 3×3 convs onlyFeature extraction
ResNet2015Skip connectionsVery deep networks
DenseNet2017Dense connectionsFeature reuse
EfficientNet2019Compound scalingEfficiency
ConvNeXt2022Modernized CNNCompete with ViT

ResNet: The Most Important CNN

┌─────────────────────────────────────────────────────────────────┐
│                    RESIDUAL BLOCK                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│         x ─────────────────────────────────┐                    │
│         │                                  │                    │
│         ▼                                  │                    │
│    ┌─────────┐                             │                    │
│    │ Conv 3×3│                             │ Skip Connection    │
│    │ BN, ReLU│                             │ (Identity)         │
│    └────┬────┘                             │                    │
│         │                                  │                    │
│         ▼                                  │                    │
│    ┌─────────┐                             │                    │
│    │ Conv 3×3│                             │                    │
│    │   BN    │                             │                    │
│    └────┬────┘                             │                    │
│         │                                  │                    │
│         ▼                                  │                    │
│        (+) ←───────────────────────────────┘                    │
│         │                                                       │
│         ▼                                                       │
│       ReLU                                                      │
│         │                                                       │
│         ▼                                                       │
│      F(x) + x                                                   │
│                                                                 │
│  Why it works:                                                  │
│  • Easier to learn F(x) = 0 than F(x) = x                       │
│  • Gradients flow directly through skip connections             │
│  • Enables training of 100+ layer networks                      │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Recurrent Neural Networks (RNNs)

Core Concepts

┌─────────────────────────────────────────────────────────────────┐
│                    RNN FUNDAMENTALS                             │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  VANILLA RNN                                                    │
│  ───────────                                                    │
│  h_t = tanh(W_hh · h_{t-1} + W_xh · x_t + b)                    │
│                                                                 │
│  Problem: Vanishing gradients for long sequences                │
│                                                                 │
│  ┌───┐   ┌───┐   ┌───┐   ┌───┐                                  │
│  │ h │ → │ h │ → │ h │ → │ h │                                  │
│  └─▲─┘   └─▲─┘   └─▲─┘   └─▲─┘                                  │
│    │       │       │       │                                    │
│  ┌─┴─┐   ┌─┴─┐   ┌─┴─┐   ┌─┴─┐                                  │
│  │x_1│   │x_2│   │x_3│   │x_4│                                  │
│  └───┘   └───┘   └───┘   └───┘                                  │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

LSTM (Long Short-Term Memory)

┌─────────────────────────────────────────────────────────────────┐
│                    LSTM CELL                                    │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Gates:                                                         │
│  • Forget gate (f): What to forget from cell state              │
│  • Input gate (i): What new info to store                       │
│  • Output gate (o): What to output                              │
│                                                                 │
│  Equations:                                                     │
│  f_t = σ(W_f · [h_{t-1}, x_t] + b_f)     # Forget gate          │
│  i_t = σ(W_i · [h_{t-1}, x_t] + b_i)     # Input gate           │
│  C̃_t = tanh(W_C · [h_{t-1}, x_t] + b_C)  # Candidate            │
│  C_t = f_t * C_{t-1} + i_t * C̃_t         # Cell state           │
│  o_t = σ(W_o · [h_{t-1}, x_t] + b_o)     # Output gate          │
│  h_t = o_t * tanh(C_t)                   # Hidden state         │
│                                                                 │
│  Key insight: Cell state C_t acts as "memory highway"           │
│  allowing gradients to flow unchanged                           │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

GRU (Gated Recurrent Unit)

AspectLSTMGRU
Gates3 (forget, input, output)2 (reset, update)
ParametersMoreFewer (~25% less)
PerformanceSlightly better on long sequencesComparable, faster training
Use caseDefault choiceWhen efficiency matters

Attention Mechanism

Self-Attention Explained

┌─────────────────────────────────────────────────────────────────┐
│                    SELF-ATTENTION                               │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Input: Sequence of embeddings X = [x_1, x_2, ..., x_n]         │
│                                                                 │
│  Step 1: Create Q, K, V                                         │
│  ─────────────────────────                                      │
│  Q = X · W_Q    (Query: "What am I looking for?")               │
│  K = X · W_K    (Key: "What do I contain?")                     │
│  V = X · W_V    (Value: "What do I provide?")                   │
│                                                                 │
│  Step 2: Compute attention scores                               │
│  ────────────────────────────                                   │
│  Attention(Q, K, V) = softmax(Q · K^T / √d_k) · V               │
│                                                                 │
│  ┌─────┐   ┌─────┐                                              │
│  │  Q  │ × │ K^T │  →  Attention Scores  →  softmax  →  × V     │
│  └─────┘   └─────┘                                              │
│                                                                 │
│  Why √d_k?                                                      │
│  • Prevents dot products from getting too large                 │
│  • Keeps softmax in good gradient region                        │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Multi-Head Attention

┌─────────────────────────────────────────────────────────────────┐
│                 MULTI-HEAD ATTENTION                            │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Idea: Run multiple attention operations in parallel            │
│  Each "head" can focus on different aspects                     │
│                                                                 │
│       Input X                                                   │
│          │                                                      │
│    ┌─────┼─────┬─────┬─────┐                                    │
│    ▼     ▼     ▼     ▼     ▼                                    │
│  ┌───┐ ┌───┐ ┌───┐ ┌───┐ ┌───┐                                  │
│  │ H1│ │ H2│ │ H3│ │ H4│ │...│  (h heads)                       │
│  └─┬─┘ └─┬─┘ └─┬─┘ └─┬─┘ └─┬─┘                                  │
│    │     │     │     │     │                                    │
│    └─────┴─────┴─────┴─────┘                                    │
│              │                                                  │
│         Concatenate                                             │
│              │                                                  │
│         Linear W_O                                              │
│              │                                                  │
│           Output                                                │
│                                                                 │
│  Typical: 8-16 heads, d_model/h dimensions per head             │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Transformers

Architecture Overview

┌─────────────────────────────────────────────────────────────────┐
│                 TRANSFORMER ARCHITECTURE                        │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  ENCODER (BERT-style)          DECODER (GPT-style)              │
│  ────────────────────          ───────────────────              │
│                                                                 │
│  ┌─────────────────┐           ┌─────────────────┐              │
│  │   Multi-Head    │           │  Masked Multi-  │              │
│  │   Attention     │           │  Head Attention │              │
│  └────────┬────────┘           └────────┬────────┘              │
│           │                             │                       │
│      Add & Norm                    Add & Norm                   │
│           │                             │                       │
│  ┌────────┴────────┐           ┌────────┴────────┐              │
│  │   Feed Forward  │           │  Cross-Attention│ (if enc-dec) │
│  │   Network       │           │  (to encoder)   │              │
│  └────────┬────────┘           └────────┬────────┘              │
│           │                             │                       │
│      Add & Norm                    Add & Norm                   │
│           │                             │                       │
│        Output                  ┌────────┴────────┐              │
│                                │   Feed Forward  │              │
│                                │   Network       │              │
│                                └────────┬────────┘              │
│                                         │                       │
│                                    Add & Norm                   │
│                                         │                       │
│                                      Output                     │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

Positional Encoding

MethodDescriptionUse Case
SinusoidalFixed sin/cos functionsOriginal Transformer
LearnedTrainable embeddingsBERT, GPT
Rotary (RoPE)Rotation-basedLLaMA, modern LLMs
ALiBiAttention biasEfficient long context

Transformer Variants

ModelTypeKey FeatureUse Case
BERTEncoderBidirectionalClassification, NER
GPTDecoderAutoregressiveText generation
T5Encoder-DecoderText-to-textTranslation, summarization
ViTEncoderPatches as tokensImage classification

Vision Transformers (ViT)

┌─────────────────────────────────────────────────────────────────┐
│                 VISION TRANSFORMER                              │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  Step 1: Patch Embedding                                        │
│  ───────────────────────                                        │
│  Image (224×224) → 16×16 patches → 196 patches                  │
│  Each patch → Linear projection → Embedding                     │
│                                                                 │
│  ┌─────────────────────────────────────┐                        │
│  │ ┌───┬───┬───┬───┐                   │                        │
│  │ │ P1│ P2│ P3│...│  → Flatten →      │                        │
│  │ ├───┼───┼───┼───┤    Linear         │                        │
│  │ │ P5│ P6│ P7│...│                   │                        │
│  │ └───┴───┴───┴───┘                   │                        │
│  └─────────────────────────────────────┘                        │
│                                                                 │
│  Step 2: Add [CLS] token + Position embeddings                  │
│  ─────────────────────────────────────────────                  │
│  [CLS, P1, P2, ..., P196] + [pos_0, pos_1, ..., pos_196]        │
│                                                                 │
│  Step 3: Transformer Encoder                                    │
│  ───────────────────────────                                    │
│  Standard transformer encoder blocks                            │
│                                                                 │
│  Step 4: Classification                                         │
│  ─────────────────────                                          │
│  [CLS] token → MLP head → Class prediction                      │
│                                                                 │
└─────────────────────────────────────────────────────────────────┘

CNN vs ViT Trade-offs

AspectCNNViT
Inductive biasStrong (locality, translation)Weak (learns from data)
Data efficiencyBetter with small dataNeeds large datasets
ComputeEfficientQuadratic attention
Long-rangeLimited receptive fieldGlobal attention
InterpretabilityFeature mapsAttention maps

Architecture Selection Checklist

✅ Choosing the Right Architecture

  1. Data size: Small → CNN/pretrained; Large → Transformer
  2. Sequence length: Short → RNN; Long → Transformer
  3. Compute budget: Limited → Efficient architectures
  4. Interpretability: Important → Attention visualization
  5. Latency: Critical → Smaller models, distillation

📎 Cross-References

🧠 Quiz

Câu 1: Self-attention mechanism trong Transformer giải quyết hạn chế nào của RNN?

  • [ ] A) RNN quá nhanh khi training
  • [x] B) RNN xử lý sequential nên chậm và khó capture long-range dependencies
  • [ ] C) RNN cần quá ít data
  • [ ] D) RNN không thể dùng GPU

💡 Giải thích: RNN xử lý tokens tuần tự nên không thể parallelize và gặp khó khăn khi capture dependencies giữa các tokens cách xa nhau. Self-attention cho phép mỗi token "nhìn" trực tiếp đến tất cả tokens khác, giải quyết cả hai vấn đề.

Câu 2: CNN sử dụng ý tưởng nào để giảm số parameters so với fully-connected layers?

  • [ ] A) Dropout
  • [x] B) Weight sharing qua convolution filters và local connectivity
  • [ ] C) Batch normalization
  • [ ] D) Skip connections

💡 Giải thích: CNN dùng convolution filters (kernels) được chia sẻ trên toàn bộ input (weight sharing) và chỉ kết nối local (receptive field), giảm đáng kể số parameters so với fully-connected layers mà vẫn capture spatial patterns hiệu quả.

Câu 3: Residual connections (skip connections) trong deep networks giúp gì?

  • [ ] A) Tăng số parameters
  • [ ] B) Giảm kích thước model
  • [x] C) Cho phép gradient flow trực tiếp qua identity shortcut, giúp train networks rất sâu
  • [ ] D) Tăng tốc độ inference

💡 Giải thích: Residual connections tạo "đường tắt" cho gradient flow, giúp gradient không bị vanish khi truyền qua nhiều layers. Đây là breakthrough cho phép train networks hàng trăm layers (ResNet) mà trước đó là không thể.