CNN vs ViT: Inductive Bias

CNNs assume a lot about images. Transformers assume almost nothing. Each has a price.

🔴 advanced5 min readvision
The real difference between CNNs and Vision Transformers is how much each assumes about images. A CNN is built to look at small nearby regions and to recognise a pattern wherever it appears. Those assumptions are usually right, so CNNs learn well from modest amounts of data. A Vision Transformer assumes almost nothing and must learn those patterns itself, so it needs far more data or heavy augmentation. In return, with enough data it keeps improving after CNNs level off.

Structural Comparison

  CONVOLUTIONAL NEURAL NETWORK (CNN)              VISION TRANSFORMER (ViT)
  Local 3x3 Sliding Window Receptive Fields      Global 16x16 Patch Token Self-Attention
  ┌───┬───┬───┐                                  ┌──────┬──────┐
  │ . │ . │ . │  Local Locality                  │Patch1│Patch2│  Global Interactions
  ├───┼───┼───┤  Hard-Coded Inductive Bias!      ├──────┼──────┤  Learned Spatial Relationships!
  │ . │ . │ . │                                  │Patch3│Patch4│
  └───┴───┴───┘                                  └──────┴──────┘

Detailed Comparison Matrix

PropertyConvolutional Neural Network (ResNet / ConvNeXt)Vision Transformer (ViT / Swin)
Core OperationLocal 3x3 / 7x7 Sliding ConvolutionsGlobal / Windowed Multi-Head Self-Attention
Inductive BiasHigh (Local Locality & Translation Invariance)Minimal (Learns spatial layout from data)
Small Dataset PerformanceStrong (Works well on ImageNet-1K from scratch)Poor without heavy augmentation / pre-training
Large Scale Data ScalingSaturates earlierScales exceptionally with compute & data
Receptive Field GrowthLinear with depth (O(L)O(L) layers)Global immediately in Layer 1 (O(1)O(1))
Computational ComplexityO(H⋅W⋅C2)O(H \cdot W \cdot C^2) (Linear with resolution)O((HW)2⋅C)O((HW)^2 \cdot C) (Quadratic with resolution)

Patch Tokenization Math in ViT

Given input image X∈RH×W×CX \in \mathbb{R}^{H \times W \times C} and patch size P=16P = 16:

  1. Number of patch tokens NN:
N=H⋅WP2N = \frac{H \cdot W}{P^2}
  1. Each patch is flattened into vector xp∈RP2⋅Cx_p \in \mathbb{R}^{P^2 \cdot C}.
  2. Linear projection E∈R(P2⋅C)×DE \in \mathbb{R}^{(P^2 \cdot C) \times D} maps patches to DD-dimensional embeddings.
  3. Prepend learnable [CLS] token and add 1D Learnable Position Embeddings EposE_{\text{pos}}:
z0=[xclass;xp1E;xp2E;… ;xpNE]+Epos\mathbf{z}_0 = [ x_{\text{class}}; x_p^1 E; x_p^2 E; \dots; x_p^N E ] + E_{\text{pos}}
  1. Pass z0\mathbf{z}_0 into standard Transformer Encoder layers!

Hybrid & Swin Transformers

Standard ViT self-attention has quadratic O(N2)O(N^2) complexity with respect to image pixels, making high-resolution processing slow.

Say this out loud

CNNs use local sliding convolutions with strong spatial inductive biases (locality and translation invariance), excelling on smaller datasets. Vision Transformers (ViT) tokenize images into 16x16 patch tokens and use global self-attention. Lacking inductive bias, ViTs require massive pre-training data (JFT-300M) or Swin shifted windows, but scale significantly better with compute.

Follow-ups to expect

Check yourself

Question 1 of 3

What spatial Inductive Biases are hard-coded into Convolutional Neural Networks (CNNs) that Vision Transformers (ViT) lack?

More in Computer Vision

See all →
Image Augmentation Strategies5 minResizing, Normalization & Colour Spaces4 minResNet, EfficientNet & Friends5 min