• 机器学习
  • 数学

An Introduction to Geometric Deep Learning

v2026.11.02-review

This article aims to answer what Geometric Deep Learning is, how it works and why it is useful by providing you a walk-through to derive key components of well-known architectures from equivariance.

An Introduction to Geometric Deep Learning

Cover image: Oken, Echinoderms and Crinoids, 1836. The echinoderm is featured by its symmetry.

Motivation

Most of the time we consider deep learning as an empirical subject. We are told what works, what doesn't, and what the standard solution is to some specific problems, yet few people have asked for the reason behind them.

This article aims to serves as a brief introduction to Geometric Deep Learning (GDL), which introduce a different way of understanding the famous Deep Learning architectures such as CNN, GCN and Transformers, explaining why these architecture designs may take the advantages of exploiting the underlying data's geometric properties.

With no need of bordering with the obscure mathematics in the original Geometric Deep Learning book, this blog wishes to give you a clean big picture by deriving the key components of CNN, DeepSets, GCN and Transformer and revealing their relations.

Before you start

No worries if you don't understand some terms in the above introduction. I've tried to explain them all from first principles as much as possible, but for a smooth reading experience, you at least ought to be familiar with the following.

  • First-year level linear algebra.
  • Basic knowledge about Convolutional Neural Network and Self-attention.

Here are some nice YouTube videos that give a sense of them.

As a complement, there are some awesome materials of introductory Group Theory. To read this article, they are not a must, but they definitely help if you want to dive deeper into the formal language of GDL.

Geometric Equivariance

We've talked about take the advantages of the geometric properties of the data's geometric properties. To make our language clearer, we are specifically referring the ones called the equivariance under certain transformation. The following example shows a simple model ff which estimates the prices of items on your desk.

f(Laptop,Mouse,Mug,Phone)=(25000¥,300¥,90¥,10000¥)f(\text{Laptop},\text{Mouse},\text{Mug},\text{Phone})=(25000¥,300¥,90¥,10000¥)

Note that the input data comes as a sequence. The model should, as we expected, assign the same value to each corresponding item if we permute the order of them, despite the fact that the prices might be interdependent as a one is likely richer if they owns a more expensive Laptop.

f(Mug,Laptop,Phone,Mouse)=(90¥, 25000¥, 10000¥, 300¥)f(\text{Mug},\text{Laptop},\text{Phone},\text{Mouse}) = (90¥,\,25000¥,\,10000¥,\,300¥)

We can conclude that such a model satisfies the equivariance under permutation. Generally speaking, that is for any input x\mathbf{x} after some transformation gg, the model outputs the exact same results as if we had applied the same transformation to the output instead:

f(g⋅x)=g⋅f(x).f(g\cdot \mathbf{x}) = g \cdot f(\mathbf{x}).

In other words, we say that the model ff and the transformation gg commute, and this is exactly what we mean by equivariance.

Assumptions like this are called inductive biases. An inductive bias is a prior belief, built into a learning algorithm before any data is seen, about which functions are likely to solve the task. A model that is equivariant by construction does not need to learn the symmetry from examples. Instead, the symmetry is already in its architecture, which usually means fewer parameters and better sample efficiency.

Many real tasks have such symmetries. A classical Computer Vision task is to categorise image data with a wide range of tags, and it is more than reasonable to design the model architecture around the equivariance of interest. In the next section we will take the simplest case, shift equivariance, and derive the core component of the Convolutional Neural Network (CNN).

Convolutional Neural Networks

To keep things simple, we will represent an image as a column vector, i.e. a single row of pixels. A barcode is a good example. It is essentially a 1D image, a row of black and white bars, where each bar plays the role of one pixel and its value records the brightness the pixel has.

x=[x1x2⋮xn]\mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix}

With the signal written as a column vector, we can now ask what the first layer should compute. You already know that a CNN is built from convolution layers. The question here is why convolution is the natural choice among all linear layers. To answer it, look at the first layer. Whatever we do with the signal later, this layer has to turn it into a feature map. Suppose the layer acts on the signal by left multiplication with a matrix WW, so y=Wx\mathbf{y} = W\mathbf{x}, with y\mathbf{y} of the same spatial size as x\mathbf{x}. For now we assume a single channel and no bias. Images have no preferred origin. If a pattern appears at a different position, the layer should produce the same response, shifted along with the pattern.

To make this precise, assume the signal is periodic, so positions wrap around in a circle. This is the periodic boundary condition. For example, the unit shift acts on the vector as:

S[x1x2⋮xn]=[xnx1⋮xn−1].S \begin{bmatrix} x_1 \\ x_2 \\ \vdots \\ x_n \end{bmatrix} = \begin{bmatrix} x_n \\ x_1 \\ \vdots \\ x_{n-1} \end{bmatrix}.

Equivalently, SS is the permutation matrix

S=[00…0110…0001…00⋮⋮⋱⋮⋮00…10].S = \begin{bmatrix} 0 & 0 & \dots & 0 & 1 \\ 1 & 0 & \dots & 0 & 0 \\ 0 & 1 & \dots & 0 & 0 \\ \vdots & \vdots & \ddots & \vdots & \vdots \\ 0 & 0 & \dots & 1 & 0 \end{bmatrix}.

Aside

If you are familiar with group theory, the shifts of a periodic signal form a cyclic group:

G={S0,S1,…,Sn−1},S0=I.G = \{S^0, S^1, \dots, S^{n-1}\}, \quad S^0 = I.

A few basic facts are enough.

  • A shift can be composed with itself. For example, S2S^2 means applying the unit shift twice, which is the same as shifting by two pixels at once.
  • Shifts can also be composed with each other, and the result is still a shift, SaSb=Sa+bS^a S^b = S^{a+b} with the exponent taken modulo nn. This also means every shift has an inverse; for example, Sn−1S^{n-1} undoes SS.

Since every other shift is a composition of SS with itself, it is enough to require that WW commute with SS:

W(Sx)=S(Wx)for all x.W(S \mathbf{x}) = S(W \mathbf{x}) \quad \text{for all } \mathbf{x}.

This is shift equivariance. Since this holds for all x\mathbf{x}, it becomes WS=SWW S = S W. Pointwise nonlinearities commute with shifts, so this property survives the rest of the feature extractor. We will see that this single equation forces WW to be a convolution and cuts its free parameters from quadratic to linear.

Shift equivariance implies WS=SWW S = S W, which ensures WW to be a circulant matrix for some parameters θ0,…,θn−1\theta_0,\dots,\theta_{n-1} of the form:

W=[θ0θn−1θn−2…θ1θ1θ0θn−1…θ2θ2θ1θ0…θ3⋮⋮⋮⋱⋮θn−1θn−2θn−3…θ0]W = \begin{bmatrix} \theta_0 & \theta_{n-1} & \theta_{n-2} & \dots & \theta_1 \\ \theta_1 & \theta_0 & \theta_{n-1} & \dots & \theta_2 \\ \theta_2 & \theta_1 & \theta_0 & \dots & \theta_3 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ \theta_{n-1} & \theta_{n-2} & \theta_{n-3} & \dots & \theta_0 \end{bmatrix}

by Theorem 1.

The subscript of θ\theta is the cyclic offset from the diagonal, so θ0\theta_0 is the diagonal entry, θ1\theta_1 the first subdiagonal, and θn−1\theta_{n-1} the first superdiagonal. Observe that the layer computes exactly a discrete convolution:

yi=∑j=1nWi,jxj=(x∗θ)i.y_i = \sum_{j=1}^{n} W_{i, j} x_j = (\mathbf{x} * \boldsymbol{\theta})_i.
y=x∗θ\mathbf{y}=\mathbf{x}*\boldsymbol{\theta}

The vector

θ=[θ0θ1⋮θn−1]\boldsymbol{\theta} = \begin{bmatrix} \theta_0\\ \theta_1\\ \vdots\\ \theta_{n-1} \end{bmatrix}

is the convolution kernel. That way, we naturally deduces the convolution formula from linearity and shift equivariance. Note that our kernel size is equal to the width of the image, which reduces to a smaller one as most CNN has if we manually set further weights to zero.

Theorem 1

Statement. Let n∈Nn \in \mathbb{N} and let S∈Rn×nS \in \mathbb{R}^{n \times n} be the permutation matrix of the unit shift on a periodic grid of nn pixels, that is, (Sx)i=xi−1(S\mathbf{x})_i = x_{i-1} for i=1,…,ni = 1,\dots,n, with indices read modulo nn in {1,…,n}\{1,\dots,n\}. Let W∈Rn×nW \in \mathbb{R}^{n \times n}. If WS=SWW S = S W, then there exist θ0,…,θn−1∈R\theta_0,\dots,\theta_{n-1} \in \mathbb{R} such that

Wi,j=θi−jfor all i,j∈{1,…,n}.W_{i,j} = \theta_{i-j} \quad \text{for all } i,j \in \{1,\dots,n\}.

The subscript of θ\theta is also read modulo nn, so it always lies in {0,…,n−1}\{0,\dots,n-1\}.

Equivalently,

W=[θ0θn−1θn−2…θ1θ1θ0θn−1…θ2θ2θ1θ0…θ3⋮⋮⋮⋱⋮θn−1θn−2θn−3…θ0].W = \begin{bmatrix} \theta_0 & \theta_{n-1} & \theta_{n-2} & \dots & \theta_1 \\ \theta_1 & \theta_0 & \theta_{n-1} & \dots & \theta_2 \\ \theta_2 & \theta_1 & \theta_0 & \dots & \theta_3 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ \theta_{n-1} & \theta_{n-2} & \theta_{n-3} & \dots & \theta_0 \end{bmatrix}.

Proof. The unit shift is the permutation matrix

S=[00…0110…0001…00⋮⋮⋱⋮⋮00…10].S = \begin{bmatrix} 0 & 0 & \dots & 0 & 1 \\ 1 & 0 & \dots & 0 & 0 \\ 0 & 1 & \dots & 0 & 0 \\ \vdots & \vdots & \ddots & \vdots & \vdots \\ 0 & 0 & \dots & 1 & 0 \end{bmatrix}.

By the definition of SS, right multiplication by SS shifts column j+1j+1 of WW to column jj, so

(WS)i,j=Wi,j+1.(W S)_{i,j} = W_{i,j+1}.

Similarly, left multiplication by SS shifts row i−1i-1 of WW to row ii, so

(SW)i,j=Wi−1,j.(S W)_{i,j} = W_{i-1,j}.

Since WS=SWW S = S W, we obtain

Wi,j+1=Wi−1,j∀i,j.W_{i,j+1} = W_{i-1,j} \quad \forall i,j.

Letting i′=i−1i' = i-1 gives

Wi′+1,j+1=Wi′,j∀i′,j.W_{i'+1,j+1} = W_{i',j} \quad \forall i',j.

Thus Wi,jW_{i,j} is constant along each cyclic diagonal and depends only on the cyclic offset i−ji-j. Define θk:=Wk+1,1\theta_k := W_{k+1,1} for k=0,…,n−1k = 0,\dots,n-1. Then

Wi,j=θi−j.  □W_{i,j} = \theta_{i-j}.\;\square

To wrap up, shift equivariance forces the linear layer to be a convolution. That said, a practical CNN also has element-wise nonlinearities, pooling, and other details. The key here is that the GDL never specifies a complete, ready-to-use architecture. It identifies the core components that a symmetry forces. The mathematical results inform how those components should be designed, and the remaining details are engineering choices compatible with that symmetry.

In the following chapters we will meet architectures you may not be familiar with. You do not need much prior expertise in them. The GDL informs the symmetry principle that comes before the architectural details, and that principle is enough to see why each architecture is built the way it is.

DeepSets

From now on, we will focus on architectures that concern the so-called permutation equivariance. As discussed in the introduction, many kinds of data come as a set. A point cloud is a set of 3D points, and a bag of images is a set of instances. The order of the elements carries no meaning, so permuting the input should only permute the output, and a set-level prediction should not change at all.

Since the publication of Deep Sets (Zaheer et al., 2017), its simple architecture has become a standard baseline for tasks such as population statistic estimation, point cloud classification, set expansion, and outlier detection. We will derive the layer from permutation equivariance. The input is a matrix

X=[x1x2⋮xn]∈Rn×d,X = \begin{bmatrix} \mathbf{x}_1 \\ \mathbf{x}_2 \\ \vdots \\ \mathbf{x}_n \end{bmatrix} \in \mathbb{R}^{n \times d},

where each row xi∈R1×d\mathbf{x}_i \in \mathbb{R}^{1 \times d} is one set element, nn is the number of elements, and the dd columns are the channels. Let P∈Rn×nP \in \mathbb{R}^{n \times n} be a permutation matrix, so PXPX permutes the rows of XX. Assume the layer is a fixed linear map X↦WXX \mapsto WX with W∈Rn×nW \in \mathbb{R}^{n \times n}. Permutation equivariance requires

W(PX)=P(WX)for every P and every X.W(PX) = P(WX) \quad \text{for every } P \text{ and every } X.

Since this holds for all XX,

WP=PW.W P = P W.

By Theorem 2, since WW commutes with every permutation matrix, its diagonal entries are all equal and its off-diagonal entries are all equal. That is,

W=αI+β11⊤=[α+ββ…ββα+β…β⋮⋮⋱⋮ββ…α+β].W = \alpha I + \beta \mathbf{1}\mathbf{1}^\top = \begin{bmatrix} \alpha+\beta & \beta & \dots & \beta \\ \beta & \alpha+\beta & \dots & \beta \\ \vdots & \vdots & \ddots & \vdots \\ \beta & \beta & \dots & \alpha+\beta \end{bmatrix}.

Equivalently,

Wij={α+β,i=j,β,i≠j.W_{ij} = \begin{cases} \alpha+\beta, & i=j, \\ \beta, & i\ne j. \end{cases}

The layer therefore computes

yi=αxi+β∑j=1nxj.\mathbf{y}_i = \alpha \mathbf{x}_i + \beta \sum_{j=1}^{n} \mathbf{x}_j.

It is worth pausing on what this layer does. The term αxi\alpha \mathbf{x}_i keeps the element itself, and the term β∑j=1nxj\beta \sum_{j=1}^{n} \mathbf{x}_j merges all elements with equal weight. This makes sense for a set. With no additional structure, the only information attached to an element is its own feature vector, so every other element enters with the same weight.

Theorem 2

Statement. Let W∈Rn×nW \in \mathbb{R}^{n \times n}. If WP=PWW P = P W for every permutation matrix P∈Rn×nP \in \mathbb{R}^{n \times n}, then there exist α,β∈R\alpha,\beta \in \mathbb{R} such that

W=αI+β11⊤.W = \alpha I + \beta \mathbf{1}\mathbf{1}^\top.

Proof. For n≤2n\le 2 the claim follows from the first two equalities below, so assume n≥3n\ge 3. Let PP be the permutation matrix that swaps ii and jj.

Right multiplication by PP swaps columns ii and jj of WW, and left multiplication by PP swaps rows ii and jj. Since WP=PWWP=PW, the two matrices agree entry by entry.

At (i,i)(i,i), the entry of WPWP is Wi,jW_{i,j}, while the entry of PWPW is Wj,iW_{j,i}. Hence

Wi,j=Wj,i.W_{i,j}=W_{j,i}.

At (i,j)(i,j), the entry of WPWP is Wi,iW_{i,i}, while the entry of PWPW is Wj,jW_{j,j}. Hence

Wi,i=Wj,j.W_{i,i}=W_{j,j}.

For k∉{i,j}k\notin\{i,j\}, the (i,k)(i,k) entry of WPWP is Wi,kW_{i,k}, while the (i,k)(i,k) entry of PWPW is Wj,kW_{j,k}. Hence

Wi,k=Wj,k.W_{i,k}=W_{j,k}.

The second equality makes all diagonal entries equal and the third makes all off-diagonal entries in a fixed column equal.

For distinct k,lk,l and m∉{k,l}m\notin\{k,l\}, the same column-swap argument with the transposition swapping kk and ll gives Wm,k=Wm,lW_{m,k}=W_{m,l}, so different columns share the same off-diagonal value.

Hence all diagonal entries equal some dd and all off-diagonal entries equal some β\beta. Therefore W=(d−β)I+β11⊤W=(d-\beta)I+\beta\mathbf{1}\mathbf{1}^\top, and setting α=d−β\alpha=d-\beta proves the statement. □\square

The linear layer WW can be read as a message aggregation matrix. Row ii specifies how element ii merges information from all elements, and WijW_{ij} is the weight it gives to element jj. For W=αI+β11⊤W=\alpha I+\beta\mathbf{1}\mathbf{1}^\top, every off-diagonal weight is the same β\beta, so the aggregation cannot distinguish one neighbour from another. The matrix is fixed, so the model can only tell an element apart from the rest of the set.

Graph Convolutional Networks

A set has no structure beyond its elements. The only relation available is whether two elements are the same, which is why DeepSets can only distinguish i=ji=j from i≠ji\ne j. Many data sources carry richer relations, such as citations between papers, friendships between users, or bonds between atoms.

A graph is given by an adjacency matrix AA, with Aij=1A_{ij}=1 when nodes ii and jj are connected, and Aij=0A_{ij}=0 otherwise. Since we now consider graph-structured data, we will refer to elements as nodes from now on.

A richer layer should let each row compute its own weights from the others, according to the graph structure. Specifically, row xi\mathbf{x}_{i} can read any other row xj\mathbf{x}_{j} and assign it a weight that reflects whether they are connected, written aija_{ij}. The same node can then aggregate its neighbours differently under different graphs. This is why WW becomes a function of AA. Later, we replace WW by a matrix G(A)∈Rn×nG(A) \in \mathbb{R}^{n \times n}, a function of the adjacency matrix. The simplest choice is G(A)=AG(A)=A.

A=[a11a12…a1na21a22…a2n⋮⋮⋱⋮an1an2…ann].A = \begin{bmatrix} a_{11} & a_{12} & \dots & a_{1n} \\ a_{21} & a_{22} & \dots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{n1} & a_{n2} & \dots & a_{nn} \end{bmatrix}.

Additionally, we can map each element to a feature with a shared linear transform WVW_V before aggregation:

V(X)=XWV∈Rn×d′.V(X) = XW_V \in \mathbb{R}^{n \times d'}.

Then

Y=G(A)V(X).Y = G(A)V(X).

Because the same WVW_V is applied to every row, V(PX)=P V(X)V(PX)=P\,V(X). The linear map therefore preserves equivariance, while giving the layer more expressive ability. The simplest choice is WV=IW_V=I, which gives V(X)=XV(X)=X.

To see whether G(A)V(X)G(A)V(X) still holds permutation equivariance. Permuting the nodes turns the adjacency matrix into

𝑃𝐴𝑃⊤.𝑃𝐴𝑃^⊤.

To see why, let's write the adjacency matrix with its row and column indices,

A=12⋯n1a11a12⋯a1n2a21a22⋯a2n⋮⋮⋮⋱⋮nan1an2⋯ann.A = \begin{array}{c|cccc} & 1 & 2 & \cdots & n \\ \hline 1 & a_{11} & a_{12} & \cdots & a_{1n} \\ 2 & a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ n & a_{n1} & a_{n2} & \cdots & a_{nn} \end{array}.

The entry aija_{ij} records the edge between node ii and node jj. When the nodes are relabelled by PP, the row index and the column index has both changed. Left multiplication by PP permutes the rows, and right multiplication by P⊤P^\top permutes the columns, hence the permuted matrix is PAP⊤PAP^\top.

Theorem 3 shows that replacing the fixed layer WXWX by G(A)V(X)G(A)V(X), which depends on the graph, keeps the layer permutation equivariant as long as GG satisfies

G(PAP⊤)=P G(A) P⊤.G(PAP^\top) = P\,G(A)\,P^\top.

Theorem 3

Statement. Let G:Rn×n→Rn×nG: \mathbb{R}^{n \times n} \to \mathbb{R}^{n \times n} and V:Rn×d→Rn×d′V: \mathbb{R}^{n \times d} \to \mathbb{R}^{n \times d'}. Define Y(X,A)=G(A)V(X)Y(X,A)=G(A)V(X). If

G(PAP⊤)=P G(A) P⊤,V(PX)=P V(X)G(PAP^\top) = P\,G(A)\,P^\top, \qquad V(PX) = P\,V(X)

for every permutation matrix P∈Rn×nP \in \mathbb{R}^{n \times n}, then YY is permutation equivariant,

Y(PX,PAP⊤)=P Y(X,A)Y(PX,PAP^\top) = P\,Y(X,A)

for every PP, XX, and AA.

Proof. For any permutation matrix PP,

Y(PX,PAP⊤)=G(PAP⊤)V(PX)=P G(A) P⊤P V(X)=P G(A)V(X)=P Y(X,A),\begin{aligned} Y(PX,PAP^\top) &= G(PAP^\top)V(PX) \\ &= P\,G(A)\,P^\top P\,V(X) \\ &= P\,G(A)V(X) \\ &= P\,Y(X,A), \end{aligned}

where P⊤P=IP^\top P = I. □\square

For Graph Convolutional Networks (GCN), G(A)G(A) is the symmetric normalised propagation matrix. Add edge on each node to itself so that each node keeps its own features:

A~=A+I,A~ij=Aij+δij,\tilde A = A + I, \qquad \tilde A_{ij} = A_{ij} + \delta_{ij},

where δij=1\delta_{ij}=1 when i=ji=j and 00 otherwise. The degree of node ii in A~\tilde A is

d~i=∑j=1nA~ij=di+1,\tilde d_i = \sum_{j=1}^{n} \tilde A_{ij} = d_i + 1,

and the degree matrix is

D~=diag⁡(d~1,…,d~n).\tilde D = \operatorname{diag}(\tilde d_1, \dots, \tilde d_n).

The symmetric normalized propagation matrix is

G(A)=D~−1/2A~D~−1/2,Gij(A)=A~ijd~id~j.G(A) = \tilde D^{-1/2} \tilde A \tilde D^{-1/2}, \qquad G_{ij}(A) = \frac{\tilde A_{ij}}{\sqrt{\tilde d_i \tilde d_j}}.

In the framework Y=G(A)V(X)Y=G(A)V(X), the GCN layer applies a pointwise nonlinearity:

Y=σ(G(A)V(X)),yi=σ(∑j=1nGij(A) xjWV),Y = \sigma(G(A)V(X)), \qquad \mathbf{y}_i = \sigma\left(\sum_{j=1}^{n} G_{ij}(A)\,\mathbf{x}_j W_V\right),

where V(X)=XWVV(X)=XW_V and WV∈Rd×d′W_V \in \mathbb{R}^{d \times d'} is the learnable linear transform.

Under a permutation PP, the features and the adjacency matrix transform together:

X′=PX,A′=PAP⊤.X' = PX, \qquad A' = P A P^\top.

Then

A~′=PA~P⊤,D~′=PD~P⊤,\tilde A' = P \tilde A P^\top, \qquad \tilde D' = P \tilde D P^\top,

so

G(A′)=P G(A) P⊤.G(A') = P\,G(A)\,P^\top.

Also V(X′)=P V(X)V(X')=P\,V(X). By Theorem 3,

Y′=P Y.Y' = P\,Y.

Self-Attention

Since the publication of Attention is all you need (Vaswani et al., 2017), this very mechanism has been applied across a wide range of tasks, such as natural language processing (NLP), audio processing and computer vision.

In this chapter, we will discuss how to design a layer enables row compute its own weights from the others, according to their content. Specifically, row xi\mathbf{x}_{i} can read any other row xj\mathbf{x}_{j} and assign it a weight that reflects its relevance to xi\mathbf{x}_{i}, written g(xi,xj)g(\mathbf{x}_{i},\mathbf{x}_{j}). The same element can then aggregate the set differently under different inputs. This is why WW becomes a function of XX. Later, we replace WW by a matrix G(X)∈Rn×nG(X) \in \mathbb{R}^{n \times n} measuring relevance between rows.

G(X)=[g(x1,x1)g(x1,x2)…g(x1,xn)g(x2,x1)g(x2,x2)…g(x2,xn)⋮⋮⋱⋮g(xn,x1)g(xn,x2)…g(xn,xn)].G(X) = \begin{bmatrix} g(\mathbf{x}_1,\mathbf{x}_1) & g(\mathbf{x}_1,\mathbf{x}_2) & \dots & g(\mathbf{x}_1,\mathbf{x}_n) \\ g(\mathbf{x}_2,\mathbf{x}_1) & g(\mathbf{x}_2,\mathbf{x}_2) & \dots & g(\mathbf{x}_2,\mathbf{x}_n) \\ \vdots & \vdots & \ddots & \vdots \\ g(\mathbf{x}_n,\mathbf{x}_1) & g(\mathbf{x}_n,\mathbf{x}_2) & \dots & g(\mathbf{x}_n,\mathbf{x}_n) \end{bmatrix}.

Each element is projected into a query, a key, and a value:

Q=XWQ,K=XWK,V(X)=XWV,Q = XW_Q, \qquad K = XW_K, \qquad V(X) = XW_V,

with WQ,WK∈Rd×dkW_Q, W_K \in \mathbb{R}^{d \times d_k} and WV∈Rd×d′W_V \in \mathbb{R}^{d \times d'}.

The relevance of element jj to element ii is the scaled dot product of its query and key:

S=QK⊤dk,Sij=xiWQWK⊤xj⊤dk.S = \frac{QK^\top}{\sqrt{d_k}}, \qquad S_{ij} = \frac{\mathbf{x}_i W_Q W_K^\top \mathbf{x}_j^\top}{\sqrt{d_k}}.

A row-wise softmax turns these scores into aggregation weights:

G(X)=softmax⁡row(QK⊤dk),Gij(X)=exp⁡(Sij)∑k=1nexp⁡(Sik).G(X) = \operatorname{softmax}_{\mathrm{row}}\left(\frac{QK^\top}{\sqrt{d_k}}\right), \qquad G_{ij}(X) = \frac{\exp(S_{ij})}{\sum_{k=1}^{n} \exp(S_{ik})}.

The layer output is

Y=G(X)V(X),yi=∑j=1nGij(X) xjWV.Y = G(X)V(X), \qquad \mathbf{y}_i = \sum_{j=1}^{n} G_{ij}(X)\,\mathbf{x}_j W_V.

Theorem 4

Statement. Let G:Rn×d→Rn×nG: \mathbb{R}^{n \times d} \to \mathbb{R}^{n \times n} and V:Rn×d→Rn×d′V: \mathbb{R}^{n \times d} \to \mathbb{R}^{n \times d'}. Define Y(X)=G(X)V(X)Y(X)=G(X)V(X). If

G(PX)=P G(X) P⊤,V(PX)=P V(X)G(PX) = P\,G(X)\,P^\top, \qquad V(PX) = P\,V(X)

for every permutation matrix P∈Rn×nP \in \mathbb{R}^{n \times n}, then YY is permutation equivariant,

Y(PX)=P Y(X)Y(PX) = P\,Y(X)

for every PP and XX.

Proof. For any permutation matrix PP,

Y(PX)=G(PX)V(PX)=P G(X) P⊤P V(X)=P G(X)V(X)=P Y(X),\begin{aligned} Y(PX) &= G(PX)V(PX) \\ &= P\,G(X)\,P^\top P\,V(X) \\ &= P\,G(X)V(X) \\ &= P\,Y(X), \end{aligned}

where P⊤P=IP^\top P = I. □\square

Self-attention satisfies both conditions. Substitute PXPX into the projections:

Q(PX)=(PX)WQ=P Q,K(PX)=(PX)WK=P K,Q(PX) = (PX)W_Q = P\,Q, \qquad K(PX) = (PX)W_K = P\,K,

so the score matrix becomes

S(PX)=Q(PX)K(PX)⊤dk=P Q (P K)⊤dk=P Q K⊤P⊤dk=P(QK⊤dk)P⊤=P S P⊤.\begin{aligned} S(PX) &= \frac{Q(PX)K(PX)^\top}{\sqrt{d_k}} \\ &= \frac{P\,Q\,(P\,K)^\top}{\sqrt{d_k}} \\ &= \frac{P\,Q\,K^\top P^\top}{\sqrt{d_k}} \\ &= P\left(\frac{QK^\top}{\sqrt{d_k}}\right)P^\top \\ &= P\,S\,P^\top. \end{aligned}

The row-wise softmax commutes with the simultaneous row and column permutation, hence

G(PX)=P G(X) P⊤.G(PX) = P\,G(X)\,P^\top.

Also V(PX)=(PX)WV=P V(X)V(PX) = (PX)W_V = P\,V(X). Theorem 4 then gives

Y(PX)=P Y(X).Y(PX) = P\,Y(X).

Softmax is a design choice. Equivariance only requires G(PX)=P G(X) P⊤G(PX)=P\,G(X)\,P^\top; any weight function with this property works. Softmax makes the weights nonnegative and row-normalized, which is convenient for training.

Self-attention is the case G=G(X)G=G(X). GCN is the case G=G(A)G=G(A), and DeepSets is the case where GG is constant. All three fit the same framework

Y=G(X,A)V(X).Y = G(X,A)V(X).

Putting it all together

In the unified form

Y=G(X,A)V(X),Y = G(X,A)V(X),

the architectures discussed so far differ in the aggregation matrix GG and in the symmetry they respect.

CNN uses a fixed circulant aggregation:

G=[θ0θn−1θn−2…θ1θ1θ0θn−1…θ2θ2θ1θ0…θ3⋮⋮⋮⋱⋮θn−1θn−2θn−3…θ0]G = \begin{bmatrix} \theta_0 & \theta_{n-1} & \theta_{n-2} & \dots & \theta_1 \\ \theta_1 & \theta_0 & \theta_{n-1} & \dots & \theta_2 \\ \theta_2 & \theta_1 & \theta_0 & \dots & \theta_3 \\ \vdots & \vdots & \vdots & \ddots & \vdots \\ \theta_{n-1} & \theta_{n-2} & \theta_{n-3} & \dots & \theta_0 \end{bmatrix}

which commutes with the cyclic shift. Its symmetry group is the cyclic group of shifts, while the other three use the full permutation group.

DeepSets uses a fixed aggregation:

G=αI+β11⊤.G = \alpha I + \beta \mathbf{1}\mathbf{1}^\top.

GCN uses the symmetric normalized adjacency:

G(A)=D~−1/2A~D~−1/2.G(A) = \tilde D^{-1/2} \tilde A \tilde D^{-1/2}.

Self-attention computes the weights from the content:

G(X)=softmax⁡row(XWQWK⊤X⊤dk).G(X) = \operatorname{softmax}_{\mathrm{row}}\left(\frac{XW_QW_K^\top X^\top}{\sqrt{d_k}}\right).

So CNN depends on the cyclic offset, DeepSets depends only on identity, GCN depends on graph structure, and self-attention depends on content.

Aside

The name of GCN deserves a final comment. Every shift is a permutation, so a CNN is a special case of a GCN on a fixed regular graph. Each pixel is a node, and the pixels covered by the kernel are its neighbours. The adjacency matrix of this graph is circulant, so the aggregation matrix is a convolution matrix. GCN extends this aggregation to an arbitrary graph, which is why the name keeps the word convolutional.

References

  • Bronstein, M. M., Bruna, J., Cohen, T., & Veličković, P. (2021). Geometric deep learning: Grids, groups, graphs, geodesics, and gauges. arXiv preprint arXiv:2104.13478.
  • Kipf, T. N., & Welling, M. (2017). Semi-supervised classification with graph convolutional networks. In Proceedings of the 5th International Conference on Learning Representations (ICLR).
  • LeCun, Y., Bottou, L., Bengio, Y., & Haffner, P. (1998). Gradient-based learning applied to document recognition. Proceedings of the IEEE, 86(11), 2278–2324.
  • Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention is all you need. In Advances in Neural Information Processing Systems (Vol. 30, pp. 5998–6008).
  • Zaheer, M., Kottur, S., Ravanbakhsh, S., Póczos, B., Salakhutdinov, R., & Smola, A. J. (2017). Deep sets. In Advances in Neural Information Processing Systems (Vol. 30, pp. 3391–3401).

BUILD IT. SHOW IT. SHARE IT.

想展示你的项目,
或想找人一起共创?

把项目介绍、仓库链接或共创想法发送到

公众号二维码
关注公众号
获取项目动态