CNN vs GNN - A Comparative Analysis
Convolutional Neural Networks (CNNs) and Graph Neural Networks (GNNs) represent two fundamental paradigms in deep learning, each designed for different data structures. While CNNs excel at processing grid-like data such as images, GNNs are specialized for graph-structured data. This post provides a comprehensive comparison of these architectures, exploring their similarities, differences, and when to use each.
Overview
Convolutional Neural Networks (CNNs)
CNNs are deep learning architectures designed for processing data with a grid-like topology, most commonly images. They leverage the spatial locality and translation invariance properties of visual data through convolutional operations.
Graph Neural Networks (GNNs)
GNNs are neural network architectures designed to operate on graph-structured data, where entities (nodes) are connected by relationships (edges). They can handle irregular, non-Euclidean data structures.
Fundamental Differences
Data Structure
CNNs operate on:
- Euclidean/Grid data: Regular structures like images (2D grids), videos (3D grids), or time series (1D sequences)
- Fixed topology: The spatial relationships between pixels are fixed and regular
- Homogeneous structure: All elements (pixels) have the same type and properties
GNNs operate on:
- Non-Euclidean/Graph data: Irregular structures with variable connectivity
- Variable topology: The relationships between nodes can vary dramatically
- Heterogeneous structure: Nodes and edges can have different types and attributes
Mathematical Foundation
CNN Convolution
According to the wiki, convolution is a mathematical operation on two functions (f and g) to produce a third function that expresses how the shape of one is modified by the other. Roughly speaking, we shift one function towards another, and for each delta shift, we calculate both functions’ area intersection.
In the traditional convolution notation, this is written as:
\[(f * g)(i, j) = \sum_{m} \sum_{n} f(m, n) \cdot g(i - m, j - n)\]where $f$ represents the input feature map and $g$ represents the learnable kernel (weight matrix). For a 2D image, the convolution operation at position $(i, j)$ aggregates information from a local neighborhood defined by the kernel size (e.g., 3×3 or 5×5).
Show numeric convolution example at (2,2)
A Concrete Numeric Example: Convolution at (2,2)
To illustrate the convolution equation: \[ (f * g)(i,j)=\sum_m \sum_n f(m,n)\, g(i-m, j-n), \] we apply it to a simple \(3\times3\) input patch (raw pixels): \[ f = \begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{bmatrix} \] and a \(3\times3\) kernel: \[ g = \begin{bmatrix} 1 & 0 & -1\\ 1 & 0 & -1\\ 1 & 0 & -1 \end{bmatrix}. \] Because this is a valid convolution (no padding), the only output location is the center, i.e., (i, j) = (2, 2).Step 1: Elementwise Multiplication
\[ \begin{bmatrix} 1\cdot1 & 2\cdot0 & 3\cdot(-1)\\ 4\cdot1 & 5\cdot0 & 6\cdot(-1)\\ 7\cdot1 & 8\cdot0 & 9\cdot(-1) \end{bmatrix} = \begin{bmatrix} 1 & 0 & -3\\ 4 & 0 & -6\\ 7 & 0 & -9 \end{bmatrix} \]Step 2: Sum All Products
\[ 1 + 0 - 3 + 4 + 0 - 6 + 7 + 0 - 9 = -6. \]Final Output at (2,2)
\[ (f * g)(2,2) = -6. \]Interpretation
The value at \((2,2)\) is the sum across the entire \(3\times3\) receptive field, showing how a convolution aggregates local spatial structure into a single output.CNN as Message Passing on Regular Grids
We can express CNN convolution in the same message-passing framework as GNNs. Let’s clarify the notation:
Notation:
- $(i, j)$: Absolute pixel coordinates in the image (e.g., pixel at row $i$, column $j$)
- $(m, n)$: Relative offsets from the center pixel $(i,j)$ (e.g., $(-1, -1)$ means one pixel up and left)
- $(i+m, j+n)$: Absolute coordinates of a neighbor pixel
Message Passing Formulation:
For a pixel at absolute position $(i, j)$ in layer $l+1$:
\[h_{(i,j)}^{(l+1)} = \text{UPDATE}^{(l)}\left(h_{(i,j)}^{(l)}, \text{AGGREGATE}^{(l)}\left(\{h_{(i+m, j+n)}^{(l)} : (m, n) \in \mathcal{N}_{kernel}\}\right)\right)\]Breaking Down the Components:
- Neighborhood Definition $\mathcal{N}_{kernel}$:
- This is the set of relative offsets $(m, n)$ that define which neighbors to consider
- For a 3×3 kernel centered at $(i,j)$, the offsets are: \(\mathcal{N}_{kernel} = \{(-1,-1), (-1,0), (-1,1), (0,-1), (0,0), (0,1), (1,-1), (1,0), (1,1)\}\)
- These offsets map to the absolute positions of neighbors:
- $(-1,-1)$ → neighbor at $(i-1, j-1)$ (top-left)
- $(-1,0)$ → neighbor at $(i-1, j)$ (top-center)
- $(0,0)$ → center pixel at $(i, j)$
- $(1,1)$ → neighbor at $(i+1, j+1)$ (bottom-right)
- etc.
- Learnable Weights $W_{m,n}^{(l)}$:
- These are the kernel weights (the actual learnable parameters)
- Each offset $(m,n)$ has a corresponding weight $W_{m,n}$
- For a 3×3 kernel, we have 9 weights: $W_{-1,-1}, W_{-1,0}, …, W_{1,1}$
AGGREGATE Function (Weighted Sum): \(\text{AGGREGATE}^{(l)}\left(\{h_{(i+m, j+n)}^{(l)}\}\right) = \sum_{(m,n) \in \mathcal{N}_{kernel}} W_{m,n}^{(l)} \cdot h_{(i+m, j+n)}^{(l)}\)
This multiplies each neighbor’s feature $h_{(i+m, j+n)}$ by its corresponding weight $W_{m,n}$ and sums them up.
UPDATE Function: \(\text{UPDATE}^{(l)}(h_{(i,j)}^{(l)}, m_{(i,j)}^{(l)}) = \sigma\left(m_{(i,j)}^{(l)} + b^{(l)}\right)\)
where $m_{(i,j)}^{(l)}$ is the aggregated message, $\sigma$ is a non-linear activation function (e.g., ReLU), and $b^{(l)}$ is a bias term.
Concrete Example:
For a pixel at $(i=5, j=5)$ with a 3×3 kernel:
- The neighborhood includes pixels at: $(4,4), (4,5), (4,6), (5,4), (5,5), (5,6), (6,4), (6,5), (6,6)$
- The aggregation computes: \(W_{-1,-1} \cdot h_{(4,4)} + W_{-1,0} \cdot h_{(4,5)} + W_{-1,1} \cdot h_{(4,6)} + W_{0,-1} \cdot h_{(5,4)} + W_{0,0} \cdot h_{(5,5)} + W_{0,1} \cdot h_{(5,6)} + W_{1,-1} \cdot h_{(6,4)} + W_{1,0} \cdot h_{(6,5)} + W_{1,1} \cdot h_{(6,6)}\)
This is exactly the standard CNN convolution operation, just written in message-passing notation!
Visual Mapping for 3×3 Kernel:
For a pixel at $(i, j)$, here’s how the offsets $(m,n)$ map to absolute positions and weights:
| Relative Offset $(m,n)$ | Absolute Position $(i+m, j+n)$ | Weight $W_{m,n}$ | Description |
|---|---|---|---|
| $(-1, -1)$ | $(i-1, j-1)$ | $W_{-1,-1}$ | Top-left neighbor |
| $(-1, 0)$ | $(i-1, j)$ | $W_{-1,0}$ | Top-center neighbor |
| $(-1, 1)$ | $(i-1, j+1)$ | $W_{-1,1}$ | Top-right neighbor |
| $(0, -1)$ | $(i, j-1)$ | $W_{0,-1}$ | Left neighbor |
| $(0, 0)$ | $(i, j)$ | $W_{0,0}$ | Center pixel (self) |
| $(0, 1)$ | $(i, j+1)$ | $W_{0,1}$ | Right neighbor |
| $(1, -1)$ | $(i+1, j-1)$ | $W_{1,-1}$ | Bottom-left neighbor |
| $(1, 0)$ | $(i+1, j)$ | $W_{1,0}$ | Bottom-center neighbor |
| $(1, 1)$ | $(i+1, j+1)$ | $W_{1,1}$ | Bottom-right neighbor |
The convolution operation computes: \(h_{(i,j)}^{(l+1)} = \sigma\left(\sum_{(m,n) \in \mathcal{N}_{kernel}} W_{m,n} \cdot h_{(i+m, j+n)}^{(l)} + b\right)\)
where the sum is over all 9 entries in the table above.
GNN Message Passing
In GNNs, information aggregation happens through message passing over the graph structure:
\[h_v^{(l+1)} = \text{UPDATE}^{(l)}\left(h_v^{(l)}, \text{AGGREGATE}^{(l)}\left(\{h_u^{(l)} : u \in \mathcal{N}(v)\}\right)\right)\]where $\mathcal{N}(v)$ represents the neighbors of node $v$, which can vary in number and type.
Key Insight: CNN as a Special Case of GNN
The CNN convolution operation is exactly a special case of GNN message passing! Here’s the explicit mapping:
Graph Structure:
- CNN: Each pixel $(i,j)$ is a node in a regular 2D grid graph
- GNN: Nodes can have arbitrary connectivity
Neighborhood Definition:
- CNN: $\mathcal{N}_{kernel} = {(-1,-1), (-1,0), …, (1,1)}$ (fixed, same for all pixels)
- GNN: $\mathcal{N}(v)$ can vary in size and composition for each node
Aggregation Function:
- CNN: Weighted sum with learnable kernel weights \(\text{AGGREGATE} = \sum_{(m,n) \in \mathcal{N}_{kernel}} W_{m,n} \cdot h_{(i+m, j+n)}\)
- GNN: Can be sum, mean, max, attention, or any permutation-invariant function \(\text{AGGREGATE} = \text{SUM/MEAN/MAX/ATTENTION}(\{h_u : u \in \mathcal{N}(v)\})\)
Mathematical Generalization:
The GNN equation: \(h_v^{(l+1)} = \text{UPDATE}^{(l)}\left(h_v^{(l)}, \text{AGGREGATE}^{(l)}\left(\{h_u^{(l)} : u \in \mathcal{N}(v)\}\right)\right)\)
reduces to CNN when:
- The graph is a regular grid: $v = (i,j)$ (pixel coordinates)
- The neighborhood is fixed: $\mathcal{N}(v) = \mathcal{N}_{kernel}$ (kernel pattern)
- The aggregation is weighted sum: $\text{AGGREGATE} = \sum_{u \in \mathcal{N}(v)} W_u \cdot h_u$
Key Differences in Generalization:
| Aspect | CNN (Special Case) | GNN (General Case) |
|---|---|---|
| Graph Structure | Regular grid (fixed topology) | Arbitrary graph (variable topology) |
| Neighborhood Size | Fixed (e.g., 8 for 3×3 kernel) | Variable (depends on node degree) |
| Neighborhood Pattern | Same for all nodes | Can vary per node |
| Aggregation | Weighted sum only | Sum, mean, max, attention, etc. |
| Spatial Locality | Enforced by grid structure | Can capture long-range dependencies |
Conclusion: GNNs are a strict generalization of CNNs. Every CNN operation can be expressed as a GNN operation on a regular grid graph, but GNNs can handle much more complex and irregular structures.
Architectural Comparison
CNN Architecture
A typical CNN consists of:
Convolutional Layers: Extract local features using learnable filters (
g is the weight matrix).Pooling Layers: Reduce spatial dimensions (max pooling, average pooling).
Fully Connected Layers: Final classification/regression
GNN Architecture
A typical GNN consists of:
Message Passing Layers: Aggregate information from neighbors \(m_v^{(l)} = \text{AGGREGATE}(\{h_u^{(l-1)} : u \in \mathcal{N}(v)\})\)
Update Layers: Combine node features with aggregated messages \(h_v^{(l)} = \text{UPDATE}(h_v^{(l-1)}, m_v^{(l)})\)
Readout Layers: Aggregate node features for graph-level tasks \(h_G = \text{READOUT}(\{h_v^{(L)} : v \in G\})\)
Detailed Comparison
1. Receptive Field
The receptive field is the region of the input space that influences the output of a particular unit (neuron/node) in the network. It determines how much context a unit can access when making its computation.
CNNs:
- Receptive field grows regularly with depth
- Fixed pattern: each layer increases receptive field by kernel size
- For a kernel size $k$ and $L$ layers: receptive field ≈ $k \times L$
GNNs:
- Receptive field grows irregularly based on graph structure
- Depends on node degrees and graph topology
- For $L$ layers: receptive field = nodes within $L$ hops
Receptive Field Example
CNN (Image):
- Layer 1: 3×3 receptive field (9 pixels)
- Layer 2: 5×5 receptive field (25 pixels)
- Layer 3: 7×7 receptive field (49 pixels)
- Pattern is uniform across all locations
GNN (Social Network):
- Layer 1: Direct neighbors (varies: 5-500 neighbors per node)
- Layer 2: Friends of friends (varies dramatically)
- Layer 3: 3-hop neighbors (highly variable)
- Pattern depends on local graph structure
2. Parameter Sharing
CNNs:
- Spatial parameter sharing: Same kernel weights applied across all spatial locations
- Enables translation invariance
- Number of parameters: $O(k^2 \times C_{in} \times C_{out})$ for a $k \times k$ kernel
GNNs:
- Structural parameter sharing: Same aggregation/update functions across all nodes
- Enables permutation invariance
- Number of parameters: $O(d_{in} \times d_{out})$ for feature dimensions
3. Computational Complexity
CNNs:
- Per layer: $O(H \times W \times C_{in} \times C_{out} \times k^2)$
- Where $H, W$ are image dimensions, $C$ are channels, $k$ is kernel size
- Total: Linear in image size
- Efficient due to regular structure and optimized implementations (cuDNN)
GNNs:
- Per layer: $O(E \times d)$
- Where $E$ is number of edges, $d$ is feature dimension
- Total: Depends on graph density
- Can be expensive for dense graphs ($E = O(V^2)$)
Complexity Comparison Example
CNN (224×224 RGB image, 3×3 conv, 64 filters):
- Operations: $224 \times 224 \times 3 \times 64 \times 9 ≈ 8.7 \times 10^8$
GNN (10,000 nodes, average degree 10, 64-dim features):
- Operations: $10,000 \times 10 \times 64 = 6.4 \times 10^6$
GNN (10,000 nodes, fully connected, 64-dim features):
- Operations: $10,000 \times 10,000 \times 64 = 6.4 \times 10^9$
This shows that GNN complexity heavily depends on graph density!
4. Inductive vs Transductive Learning
CNNs:
- Inductive: Can process new images not seen during training
- Generalization to different image sizes (with appropriate architecture)
- Transfer learning across different datasets
GNNs:
- Can be both:
- Transductive: Some GNNs (e.g., GCN) require full graph during training
- Inductive: Others (e.g., GraphSAGE) can handle new nodes/graphs
- Inductive capability depends on architecture design
5. Translation Invariance vs Permutation Invariance
CNNs:
- Translation invariance: Object at different positions produces similar features
- Achieved through parameter sharing and pooling
- Critical for image recognition tasks
GNNs:
- Permutation invariance/equivariance: Graph representation doesn’t depend on node ordering
- Achieved through permutation-invariant aggregation (sum, mean, max)
- Critical for graph learning tasks
Hybrid Approaches
Converting Images to Graphs
Images can be converted to graphs for GNN processing:
- Pixel as nodes: Each pixel becomes a node
- Spatial edges: Connect neighboring pixels
- Features: Pixel RGB values as node features
Advantages:
- Can handle irregular image regions
- More flexible than fixed kernels
Disadvantages:
- Much more expensive ($O(V^2)$ for dense connections)
- Loses efficiency of regular convolution
Converting Graphs to Images
Graphs can be converted to images for CNN processing:
- Adjacency matrix as image: $A \in \mathbb{R}^{V \times V}$
- Graph Laplacian as image: $L = D - A$
- Node feature matrix: $X \in \mathbb{R}^{V \times d}$
Advantages:
- Can leverage pre-trained CNNs
- Standard CNN tooling
Disadvantages:
- Fixed node ordering required
- Loses permutation invariance
- Not scalable for large graphs
Performance Characteristics
Training Efficiency
| Aspect | CNNs | GNNs |
|---|---|---|
| Batch processing | Highly efficient (regular structure) | Can be challenging (variable graph sizes) |
| GPU utilization | Excellent (optimized kernels) | Good (but less optimized) |
| Memory | Predictable (image size) | Variable (depends on graph density) |
| Convergence | Fast (strong inductive bias) | Can be slower (less structure) |
Scalability
CNNs:
- Scale well with image resolution
- Memory: $O(H \times W \times C)$
- Can process very large images with appropriate architecture
GNNs:
- Scale depends on graph density
- Memory: $O(V + E)$
- Dense graphs ($E = O(V^2)$) are challenging
- Use sampling techniques (GraphSAGE) for large graphs
Theoretical Expressiveness
CNNs
- Universal approximation: Can approximate any continuous function on compact sets
- Translation equivariance: Convolution preserves spatial relationships
- Limited to regular grids: Cannot directly handle irregular structures
GNNs
- WL test connection: Some GNNs (GIN) are as powerful as Weisfeiler-Lehman test
- Permutation invariance: Respects graph structure regardless of node ordering
- Can handle arbitrary graphs: More general than CNNs in terms of data structure
Theoretical Relationship
As shown in the Mathematical Foundation section, CNNs are mathematically a special case of GNNs. The GNN message-passing equation:
\[h_v^{(l+1)} = \text{UPDATE}^{(l)}\left(h_v^{(l)}, \text{AGGREGATE}^{(l)}\left(\{h_u^{(l)} : u \in \mathcal{N}(v)\}\right)\right)\]reduces to CNN convolution when we impose the following constraints:
- Regular Grid Graph: Each pixel $(i,j)$ is a node in a 2D lattice graph
- Fixed Neighborhood: $\mathcal{N}(v) = \mathcal{N}_{kernel}$ is the same fixed pattern for all nodes (e.g., 3×3 kernel = 8 neighbors)
- Weighted Sum Aggregation: $\text{AGGREGATE} = \sum_{u \in \mathcal{N}(v)} W_u \cdot h_u$ where $W_u$ are the learnable kernel weights
- Fixed Topology: The graph structure (grid connectivity) is known a priori and doesn’t change
Mathematical Proof Sketch:
Starting from the GNN equation with pixel $v = (i,j)$:
\[h_{(i,j)}^{(l+1)} = \text{UPDATE}\left(h_{(i,j)}^{(l)}, \sum_{(m,n) \in \mathcal{N}_{kernel}} W_{m,n}^{(l)} \cdot h_{(i+m, j+n)}^{(l)}\right)\]If we set:
- $\text{UPDATE}(h, m) = \sigma(m + b)$ (simple addition with activation)
- $\mathcal{N}_{kernel} = {(-1,-1), (-1,0), …, (1,1)}$ (3×3 kernel pattern)
- $W_{m,n}$ are the learnable kernel weights
We recover the standard CNN convolution operation!
Implications:
- Unified Framework: Both CNNs and GNNs operate under the same message-passing paradigm
- Generalization: GNNs can handle irregular structures that CNNs cannot
- Transfer Learning: Insights from one domain can inform the other
- Architecture Design: Understanding this relationship helps design better models
This perspective unifies both architectures under the message-passing framework and provides a theoretical foundation for understanding their relationship!
Recent Trends and Convergence
Vision Transformers (ViTs)
ViTs bridge the gap by:
- Treating images as sequences of patches
- Using self-attention (similar to GATs)
Graph Convolution as Generalization
Recent work shows that:
- Graph convolution generalizes standard convolution
- CNNs are GNNs on regular graphs
Conclusion
CNNs and GNNs are complementary architectures, each optimized for different data structures:
- CNNs excel at processing regular, grid-like data with strong spatial locality
- GNNs excel at processing irregular, graph-structured data with explicit relationships
The choice between them depends on:
- The inherent structure of your data
- The nature of the relationships you want to model
- Computational constraints
- Availability of pre-trained models
As research progresses, we’re seeing convergence between these paradigms, with hybrid architectures that combine the strengths of both. Understanding when and how to use each is crucial for building effective deep learning systems.
Further Reading
- Convolutional Neural Networks for Visual Recognition (CS231n)
- Graph Neural Networks: A Review of Methods and Applications
- Semi-Supervised Classification with Graph Convolutional Networks
- ImageNet Classification with Deep Convolutional Neural Networks (AlexNet)
- The Graph Neural Network Model
This post is part of our Graph Representation Learning series. For more on GNNs, check out our posts on Introduction to Graph Neural Networks and Advanced GNN Architectures.


