ai papers

Neural Networks with Graphs

Junyoung Park · 2022-12-17 · 16 min

Most models we have examined implicitly assume that the entire dataset is i.i.d.—independent and identically distributed. In other words, every sample in a modality exists independently and is unaffected by other samples during inference. Neural networks over graphs take a different approach.

What Is a Graph?

As shown above, a graph consists of individual vertices or nodes (points) and the edges (lines) connecting them. Each node or vertex represents an item or entity, while an edge represents the relationship between two of them.

What Is a Euclidean Domain?

A Euclidean function f:RNf : R \rightarrow \mathbb{N} defined over a domain RR must satisfy the following property.

For arbitrary aR and bR,a=bq+rthere exist q,rR such that r=0 or f(r)<f(b). \begin{aligned} \text{For arbitrary }&a\in R \text{ and }b \in R, \newline &a = bq+r \newline \text{there exist }&q, r \in R\text{ such that }r = 0 \text{ or }f(r) < f(b). \end{aligned}

A Euclidean domain is simply an integral domain for which at least one such Euclidean function exists. The terminology may seem difficult in isolation, but examples of datasets in Euclidean domains make the idea easier to understand.

An image like the one above can be represented as data on a 2D grid. A 2D grid is a representative Euclidean domain—ordinary coordinate systems fall into this category—so image data can be called Euclidean. Likewise, tokenizing a sentence or speech signal and processing it as embeddings places the data on a 1D grid, another Euclidean domain.

A graph structure, however, is non-Euclidean. A 3D mesh whose faces are connected through vertices and edges is one example, as is a social network defined by relationships among users. On Instagram, users A, B, C, and so on may be connected as friends, close friends, frequent DM correspondents, or even non-friends who exchange DMs. Each user can also have properties such as a private account, public account, or an account that posts stories frequently.

Structures used for pose estimation are another example. In general, non-Euclidean data that cannot be placed on a grid includes any modality defined by relationships between points.

Graph Notation

Before continuing, let us define the notation used throughout the discussion.

  • VV : A set of vertices(node)
  • NN : The number of vertices in a set of vertices VV
  • viv_i : The ithi^{th} vertex in a set of vertices VV
  • viFv_i^F : The feature vector of vertex viv_i
  • ne(vi)ne(v_i) : The set of vertex indices for the vertices that are direct neighbors of viv_i
  • EE : A set of edges
  • MM : The number of edges in a set of edges EE
  • ei,je_{i,j} : The edge between the ithi^{th} vertex and the jthj^{th} vertex, in a set of edges EE.
  • ei,jFe_{i,j}^F : The feature vector of edge ei,je_{i,j}
  • hikh_i^k : The kthk^{th} hidden layer's representation of the ithi^{th} vertex's local neighborhood.
  • oio_i : The ithi^{th} output of GNN(indexing is framework dependent)
  • G=G(V,E)G = G(V, E) : A graph defined by the set of vertices VV and the set of edges EE

A vertex feature vector describes the properties of that node. It may include the vertex number or index; for a person, it might include their name, nationality, and age.

We also need representations of graph connectivity.

  • AA : The adjacency matrix; each element Ai,jA_{i, j} represents if the ithi^{th} vertex is connected to the jthj^{th} vertex by a weight
  • WW : The weight matrix; each element Wi,jW_{i,j} represents the 'weight' of the edge between the ithi^{th} vertex and the jthj^{th} vertex. The 'weight' typically represents some real concept of property. For example, the weight between two given vertices could be inversely proportional to their distance from one another(i.e., close vertices have a higher weight between them). Graphs with a weight matrix are referred to as weighted graphs, but not all graphs are weighted graphs.
  • DD : The degree matrix; a diagonal matrix of vertex degrees or valencies(the number of edges incident to a vertex). Formally defined as Di,j=jAi,jD_{i,j} = \sum_j A_{i, j}
  • LL : The non-normalized graph Laplacian; defined as L=DWL = D-W. For unweighted graphs, W=AW = A. Using these definitions, we can derive each component of the graph above.
D=[200000030000002000000300000030000001] D = \begin{bmatrix} 2 & 0 & 0 & 0 & 0 & 0 \newline 0 & 3 & 0 & 0 & 0 & 0 \newline 0 & 0 & 2 & 0 & 0 & 0 \newline 0 & 0 & 0 & 3 & 0 & 0 \newline 0 & 0 & 0 & 0 & 3 & 0 \newline 0 & 0 & 0 & 0 & 0 & 1 \end{bmatrix}

First, DD is a diagonal matrix containing only di,jd_{i,j} where i=ji=j. Each element gives the number of other nodes connected to that node.

A=[010010101010010100001011110100000100] A = \begin{bmatrix} 0 & 1 & 0 & 0 & 1 & 0 \newline 1 & 0 & 1 & 0 & 1 & 0 \newline 0 & 1 & 0 & 1 & 0 & 0 \newline 0 & 0 & 1 & 0 & 1 & 1 \newline 1 & 1 & 0 & 1 & 0 & 0 \newline 0 & 0 & 0 & 1 & 0 & 0 \end{bmatrix}

For AA, element ai,ja_{i,j} indicates whether vertex ii and vertex jj are connected, using 00 or 11. The matrix is therefore symmetric, as shown.

L=[210010131010012100001311110130000101] L = \begin{bmatrix} 2 & -1 & 0 & 0 & -1 & 0 \newline -1 & 3 & -1 & 0 & -1 & 0 \newline 0 & -1 & 2 & -1 & 0 & 0 \newline 0 & 0 & -1 & 3 & -1 & -1 \newline -1 & -1 & 0 & -1 & 3 & 0 \newline 0 & 0 & 0 & -1 & 0 & 1 \end{bmatrix}

For an unweighted graph, the unnormalized Laplacian is simply the degree matrix minus the adjacency matrix. Several other Laplacian matrices can also be defined.

  • InI_n : An n×nn \times n identity matrix; all zeros except for ones along the diagonal.
  • LsnL^{sn} : The symmetric normalized graph Laplacian; defined as L=InD12AD12L = I_n - D^{-\frac{1}{2}}AD^{-\frac{1}{2}}
  • LrwL^{rw} : The random-walk normalized graph Laplacian; defined as L=InD1AL = I_n - D^{-1}A

Graph Neural Networks (GNNs)

A graph neural network (GNN) is a trainable architecture for processing graphs; the graph structure itself enters the neural network during training and evaluation. Because the number of vertices and edges can vary, the architecture does not impose a fixed size. Most importantly, a GNN can process both data in structured Euclidean domains and data in non-Euclidean domains.

A GNN plays a role analogous to an MLP in an ordinary neural network: through repeated feature extraction, it obtains meaningful high-level feature representations from a graph. The resulting high-level feature representation resembles a decoder output in a familiar deep-learning framework.
The goals of a GNN can therefore be divided into two parts.

  1. Compute a high-level hidden feature vector for each vertex using transition function ff.
  2. Produce meaningful outputs from the resulting hidden feature vectors f()f(\cdot) using output function gg. Let us examine each process.

Step 1: Transition

Consider the nodes adjacent to each vertex viv_i, denoted above by ne(vi)ne(v_i). We must use these neighborhoods to compute a hidden representation of the target node. Since each vertex can have a different number of neighbors, the transition process computes an aggregation of the neighbors ne(vi)ne(v_i). This gives every vertex a hidden feature vector of the same size. The hidden state hh, or embedding, of vertex viv_i at state kk is formulated as follows.

hik=jne(vi)f(viF,ei,jF,vjF,hjk1),Where all hi0 are defined upon initialization h_i^k = \sum_{j \in ne(v_i)} f(v_i^F, e_{i,j}^F, v_j^F, h_j^{k-1}), \text{Where all }h_i^0 \text{ are defined upon initialization}

As the expression shows, aggregation is represented by summation. The function takes as input the target vertex's features, each adjacent vertex's features, the edge features describing their relationship, and the adjacent vertex's hidden state. One might ask why it does not include the target vertex's own previous hidden state; the answer is simply that this particular formulation omits it. A concrete formulation may vary by application. Function ff is a nonlinear transformation, such as a simple MLP with an activation function.

In this transition process, the kthk^{th} state means the following.

Nodes have features viFv_i^F at every layer. At the zeroth layer, the 0th0^{th} state of vertex A=vAFA=v_A^F is hidden state hA0h_A^0. Continuing this process, state nn aggregates information from vertices up to nn hops away from the target. A GNN transition stage therefore measures how far through the graph the information used to represent the current vertex has traveled.
kk can be arbitrarily large; in general, transition function ff is applied repeatedly until the kthk^{th} state becomes stable.

Step 2: Output

After applying function ff for kk steps until convergence, the graph implicitly contains computed feature vectors. The output function uses these converged hidden states to produce a meaningful output.
Outputs fall into three categories—vertex-level, edge-level, and graph-level—each suited to different tasks.

First, a vertex-level framework uses only vertex information to produce its output: the vertex feature vector viFv_i^F and hidden state hikmaxh_i^{kmax}.

oi=g(viF, hikmax) o_i = g(v_i^F,~h_i^{kmax})

Typical tasks are node classification or regression, such as labeling an unlabeled node from its neighbors.

Second, an edge-level framework requires five inputs. Because an edge connects two vertices, it needs their two feature vectors viFv_i^F and vjFv_j^F, their hidden states hikmaxh_i^{kmax} and hjkmaxh_j^{kmax}, and the edge feature vector eijFe_{ij}^F.

oi=g(viF, hikmax, vjF, hjkmax, eijF) o_i = g(v_i^F,~h_i^{kmax},~v_j^F,~h_j^{kmax},~e_{ij}^F)

Typical tasks include edge classification—classifying relationships—and, by extension, link prediction. Link prediction asks whether two nodes may develop a meaningful relationship in the future. An online marketplace such as Amazon uses a similar idea when it recommends items you may want after a purchase; many content-recommendation platforms can use it as well.

Third, a graph-level framework uses information from the network as a whole. It need not include every graph feature; depending on the design, it may use final vertex or edge hidden states, or even initial states.

One typical task is graph classification, which assigns different graphs to groups as shown above.

It can also construct a graph over the pixels of an image—data in a Euclidean domain—and classify from that graph.

Reformulation to Neural Network form

The earlier expressions denote transition and output functions abstractly as ff and gg without showing their neural-network formulation.
Following the perceptron definition, a neural network computes an affine transform for the next state using trainable weights and biases, then applies nonlinear activations so that multiple layers increase functional complexity.

hv0=xvhvk=σ(WkuN(v)huk1N(v)), k(1,,K)zv=hvK \begin{aligned} h_v^0 =& x_v \newline h_v^k =& \sigma\left( W_k \sum_{u \in N(v)} \frac{h_u^{k-1}}{\vert N(v) \vert} \right),~\forall k \in (1, \cdots, K) \newline z_v =& h_v^K \end{aligned}

To compute state kk, hvkh_v^k, for vertex vv, average the (k1)th(k-1)^{th} hidden states huk1h_u^{k-1} of its adjacent vertices uu and multiply by weight WkW_k. Add the previous hidden state hvk1h_v^{k-1} of vv multiplied by bias term BkB_k, then apply the activation function to the result.

H(l+1)=σ(H(l)W0(l)+A~H(l)W1(l))with A~=D12AD12 \begin{aligned} H^{(l+1)} =& \sigma(H^{(l)}W_0^{(l)}+\tilde{A}H^{(l)}W_1^{(l)}) \newline \text{with } \tilde{A} =& D^{-\frac{1}{2}}AD^{-\frac{1}{2}} \end{aligned}

This can also be written in vector form as above. A loss function over the resulting hidden states or embeddings enables gradient-based optimization of trainable weight and bias parameters.

Graph embedding

A trained neural network can map data into an embedding space: a node, a graph substructure, or an entire graph structure can become a vector. Embedding quality is primarily measured by whether similarities in the original graph, such as node similarity, are preserved in the dd-dimensional embedding space. The resulting embeddings can support many downstream tasks.

Convolutional GNNs

The discussion so far has explained how neural networks infer over graph structures. We now turn to convolutional architectures. Convolutional GNNs, or CGNNs, fall into two types: those in the spatial domain and those in the spectral (frequency) domain.

CGNNs in spatial domain

Consider a digit image with the following graph structure.

Treat the image as a specially structured graph. Convolution multiplies a fixed-size filter, here 3×33 \times 3, over the input and aggregates the result, then shifts the filter and repeats.

Ordinary convolution works on modalities such as images, but is difficult to apply to other graphs with spatial order. Unlike images designed around a grid, non-Euclidean datasets can give each vertex a different number of neighbors, preventing uniform aggregation.

In the example above, the target vertices are colored red, green, and blue, with dotted boxes defining their respective neighborhoods. Spatial convolution selects a neighborhood and aggregates its vertex feature vectors. The aggregate determines the target vertex's next embedding. Repeating this process for every neighborhood produces embeddings used by the next spatial-convolution layer, enabling hierarchical feature extraction. The procedure is:

  1. Using spatial connectivity, define graph neighborhoods around all vertices and select the first neighborhood in the input graph.
  2. Aggregate the values in that neighborhood using an operation such as a sum or mean.
  3. Update the vertex hidden state, or embedding, with the resulting value.
  4. Repeat for every neighborhood.

The GraphSAGE paper extracts node embeddings and is useful when nodes have rich attribute information.

This operates almost identically to the GNN aggregation expression. Spatial CGNNs use aggregators such as the following.

VariantAggregatorUpdater
Neural FPshNvt=hvt1+k=1Nvhkt1h_{\mathcal{N_v}}^t = h_v^{t-1}+\sum_{k=1}^{\mathcal{N_v}}h_k^{t-1}hvt=σ(hNvtWLNv)h_v^t = \sigma(h_{\mathcal{N_v}}^t W_L^{\mathcal{N_v}})
DCNNNode classification:
N=PXN = P\ast X
Graph classification:
N=1NTPX/NN = 1_N^T P\ast X/N
H=f(WcN)H = f(W^c \odot N)
GraphSAGEhNvt=AGGREGATEt(hut1,uNv)h_{\mathcal{N_v}^t} = \text{AGGREGATE}_t(h_u^{t-1}, \forall u \in \mathcal{N_v})hvt=σ(Wt(hvt1hNvt))h_v^t = \sigma(W^t \cdot (h_v^{t-1} \parallel h_{\mathcal{N_v}}^t))

CGNNs differ from conventional GNNs in one key respect. A GNN repeatedly applies transition function ff until its embedding stabilizes, optimizing through hvkmaxh_v^{kmax}. A CGNN instead has a fixed number of layers and often uses a single layer for a simple update (k=2k = 2).

CGNNs in spectral domain

The spectral domain is the domain of frequency. Frequency is intuitive for temporal signals such as speech because sound transmits information through vibrations in a medium such as air, and the Fourier transform F\mathcal{F} is commonly used to analyze it in the frequency domain. Spectral methods can also be applied to image convolution.
After a Fourier transform, image convolution becomes multiplication in the frequency domain.

F(fg)=F(f)×F(g) \mathcal{F}(f \ast g) = \mathcal{F}(f) \times \mathcal{F}(g)

With this background, let us discuss graph signals.

Graph signals

A graph signal is defined when every vertex maps to a real-valued embedding. A function f:VR, VGf: V \rightarrow \mathbb{R},~\forall V \in G over vertices is such a signal. It represents all vertices as a vector of size NN, where NN is the number of vertices and element ii is the signal value of vertex viv_i. Here, ff is not a neural network but simply a feature map over vertices.

Laplacian

The Laplacian operator Δ\Delta is the second-order gradient 2\nabla^2. Given graph signal ff, its gradient is

f(i)=(f(i+1)f(i))/δ \nabla f(i) = (f(i+1)-f(i))/\delta

Here, δ\delta and ii can be replaced by vertex indices i,ji,j and edge weight ww.

f(i, j)=(f(i)f(j))w(i, j) \nabla f(i,~j) = (f(i)-f(j))w(i,~j)

The Laplacian can then be defined as follows.

Δf(i, j)=j2f(i)j2Δf(i, j)jΩi(f(i)f(j))w(i, j) \begin{aligned} \Delta f(i,~j) =& \sum_j \frac{\partial^2 f(i)}{\partial j^2} \newline \Delta f(i,~j) \simeq& \sum_{j \in \Omega_i} (f(i)-f(j))w(i,~j) \end{aligned}

Expanding this into matrix form gives

Δf(i, j)=j2f(i)j2jΩi(f(i)f(j))w(i, j)=(jwij)f(i)jwijf(j)=(Df)i(Wf)i=((DW)f)i \begin{aligned} \Delta f(i,~j) =& \sum_j \frac{\partial^2 f(i)}{\partial j^2} \newline \simeq& \sum_{j \in \Omega_i} (f(i)-f(j))w(i,~j) \newline =& (\sum_j w_{ij})f(i) - \sum_j w_{ij}f(j) \newline =& (Df)_i - (Wf)_i = ((D-W)f)_i \end{aligned}

This explains why the graph Laplacian introduced in the notation section can be expressed using the degree matrix and adjacency—or weight—matrix.

Graph Laplacian

Eigendecomposing LL gives L=UΛUL = U\Lambda U^\top. Eigenvectors ui, i=0,,Nu_i,~i=0,\cdots,N become the Fourier bases of graph GG, and eigenvalues λi, i=0,,N\lambda_i,~i=0,\cdots,N become its frequency components. The Fourier transform ff^f \rightarrow \hat{f} of graph signal ff and its inverse f^f\hat{f} \rightarrow f are then

f^=Uff=Uf^ \begin{aligned} \hat{f} =& U^\top f \newline f =& U\hat{f} \end{aligned}

as above. Expressing the inner products over the individual Fourier bases gives

f^=Uf=i=1Nf(i)uk(i)f=Uf^=k=1Nf^(k)uk(i) \begin{aligned} \hat{f} =& U^\top f = \sum_{i=1}^N f(i) u_k^\ast (i) \newline f =& U\hat{f} = \sum_{k=1}^N \hat{f}(k) u_k(i) \end{aligned}

The reverse relationship also follows by duality: just as convolution in the spatial domain becomes multiplication in the frequency domain, vertex-wise multiplication corresponds to frequency-domain convolution.

((^f)g^)(i)=k=1Nf^(k)g^(k)uk(i) (\hat(f) \ast \hat{g})(i) = \sum_{k=1}^N \hat{f}(k) \hat{g}(k) u_k(i)

If we can define an arbitrary filter gg, this enables the following hidden-channel computation. Replacing gg with trainable weight Θ\Theta gives

Hjk=σ(i=1fk1U(Θk(UHik1))),where j=1,2,,fk \begin{aligned} H_j^k =& \sigma(\sum_{i=1}^{f_{k-1}} U(\Theta^k(U^\top H_i^{k-1}))), \newline \text{where }j =& 1,2, \cdots, f_k \end{aligned}

Let us apply this expression to the following graph.

The graph has N=5N=5 vertices, M=5M=5 edges, and fk=2f_k=2 graph signals per vertex, where kk is the layer index. The feature vectors above are the initial state, so

H0=[0.280.460.370.3120.14]H^0 = \begin{bmatrix} 0.2 & 8 \newline 0.4 & 6 \newline 0.3 & 7 \newline 0.3 & 12 \newline 0.1 & 4 \end{bmatrix}

For columns H1kH^k_1 and H2kH^k_2 to be updated,

H10=[0.20.40.30.30.1] H^0_1 = \begin{bmatrix} 0.2 & 0.4 & 0.3 & 0.3 & 0.1 \end{bmatrix}^\top H20=[867124] H^0_2 = \begin{bmatrix} 8 & 6 & 7 & 12 & 4 \end{bmatrix}^\top Hjk=σ(i=12U(Θk(UHik1))),where j=1,2 \begin{aligned} H_j^k =& \sigma(\sum_{i=1}^2 U(\Theta^k(U^\top H_i^{k-1}))), \newline \text{where }j =& 1,2 \end{aligned}

This is the resulting expression. It requires computing an eigensystem determined by the graph structure. First-generation spectral CGNNs therefore suffered from an eigensystem whose size grew with the node count NN. Later work addressed this limitation with the second-generation ChebNet and third-generation GCN.

VariantAggregatorUpdater
ChebNetNk=Tk(L~)XN_k = T_k(\tilde{L})XH=k=0KNkΘkH = \sum_{k=0}^K N_k \Theta_k
1st1^{st} order modelN0=XN_0 = X
N1=D12AD12XN_1 = D^{-\frac{1}{2}}AD^{-\frac{1}{2}}X
H=N0Θ0+N1Θ1H = N_0 \Theta_0 + N_1 \Theta_1
Single parameterN=(IN+D12AD12)XN = (I_N + D^{-\frac{1}{2}}AD^{-\frac{1}{2}})XH=NΘH = N\Theta
GCNN=D~12A~D~12XN = \tilde{D}^{-\frac{1}{2}}\tilde{A}\tilde{D}^{-\frac{1}{2}}XH=NΘH = N\Theta

GCN

GCN is the best-known spectral convolutional GNN. It is an ll-layer network with a more familiar neural-network structure. As shown in the table, its hidden layer uses aggregator A^\hat{A} as follows.

H(l+1)=σ(A^H(l)W(l)),with A^=D~12A~D~12XDij~={k=1Aik~if i=j0otherwise \begin{aligned} H^{(l+1)} =& \sigma(\hat{A}H^{(l)}W^{(l)}), \newline \text{with }\hat{A} =& \tilde{D}^{-\frac{1}{2}}\tilde{A}\tilde{D}^{-\frac{1}{2}}X \newline \tilde{D_{ij}} =& \begin{cases} \sum_{k=1} \tilde{A_{ik}} & \text{if }i = j \newline 0 & \text{otherwise} \end{cases} \end{aligned}

For the resulting hidden embeddings, the output layer is

Z=f(X,A)=softmax(A^ReLU(A^XW(0))W(1)) Z = f(X, A) = softmax(\hat{A}\text{ReLU}(\hat{A}XW^{(0)})W^{(1)})

as shown above.

I do not yet fully understand graph-based networks. I may study them further when I have the opportunity, but for now this remains a difficult area for me to explore in depth.