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 defined over a domain must satisfy the following property.
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.
- : A set of vertices(node)
- : The number of vertices in a set of vertices
- : The vertex in a set of vertices
- : The feature vector of vertex
- : The set of vertex indices for the vertices that are direct neighbors of
- : A set of edges
- : The number of edges in a set of edges
- : The edge between the vertex and the vertex, in a set of edges .
- : The feature vector of edge
- : The hidden layer's representation of the vertex's local neighborhood.
- : The output of GNN(indexing is framework dependent)
- : A graph defined by the set of vertices and the set of edges
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.
- : The adjacency matrix; each element represents if the vertex is connected to the vertex by a weight
- : The weight matrix; each element represents the 'weight' of the edge between the vertex and the 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.
- : The degree matrix; a diagonal matrix of vertex degrees or valencies(the number of edges incident to a vertex). Formally defined as
- : The non-normalized graph Laplacian; defined as . For unweighted graphs, . Using these definitions, we can derive each component of the graph above.
First, is a diagonal matrix containing only where . Each element gives the number of other nodes connected to that node.
For , element indicates whether vertex and vertex are connected, using or . The matrix is therefore symmetric, as shown.
For an unweighted graph, the unnormalized Laplacian is simply the degree matrix minus the adjacency matrix. Several other Laplacian matrices can also be defined.
- : An identity matrix; all zeros except for ones along the diagonal.
- : The symmetric normalized graph Laplacian; defined as
- : The random-walk normalized graph Laplacian; defined as
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.
- Compute a high-level hidden feature vector for each vertex using transition function .
- Produce meaningful outputs from the resulting hidden feature vectors using output function . Let us examine each process.
Step 1: Transition
Consider the nodes adjacent to each vertex , denoted above by . 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 . This gives every vertex a hidden feature vector of the same size. The hidden state , or embedding, of vertex at state is formulated as follows.
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 is a nonlinear transformation, such as a simple MLP with an activation function.
In this transition process, the state means the following.
Nodes have features at every layer. At the zeroth layer, the state of vertex is hidden state . Continuing this process, state aggregates information from vertices up to 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.
can be arbitrarily large; in general, transition function is applied repeatedly until the state becomes stable.
Step 2: Output
After applying function for 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 and hidden state .
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 and , their hidden states and , and the edge feature vector .
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 and 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.
To compute state , , for vertex , average the hidden states of its adjacent vertices and multiply by weight . Add the previous hidden state of multiplied by bias term , then apply the activation function to the result.
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 -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 , 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:
- Using spatial connectivity, define graph neighborhoods around all vertices and select the first neighborhood in the input graph.
- Aggregate the values in that neighborhood using an operation such as a sum or mean.
- Update the vertex hidden state, or embedding, with the resulting value.
- 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.
| Variant | Aggregator | Updater |
|---|---|---|
| Neural FPs | ||
| DCNN | Node classification: Graph classification: | |
| GraphSAGE |
CGNNs differ from conventional GNNs in one key respect. A GNN repeatedly applies transition function until its embedding stabilizes, optimizing through . A CGNN instead has a fixed number of layers and often uses a single layer for a simple update ().
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 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.
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 over vertices is such a signal. It represents all vertices as a vector of size , where is the number of vertices and element is the signal value of vertex . Here, is not a neural network but simply a feature map over vertices.
Laplacian
The Laplacian operator is the second-order gradient . Given graph signal , its gradient is
Here, and can be replaced by vertex indices and edge weight .
The Laplacian can then be defined as follows.
Expanding this into matrix form gives
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 gives . Eigenvectors become the Fourier bases of graph , and eigenvalues become its frequency components. The Fourier transform of graph signal and its inverse are then
as above. Expressing the inner products over the individual Fourier bases gives
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.
If we can define an arbitrary filter , this enables the following hidden-channel computation. Replacing with trainable weight gives
Let us apply this expression to the following graph.
The graph has vertices, edges, and graph signals per vertex, where is the layer index. The feature vectors above are the initial state, so
For columns and to be updated,
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 . Later work addressed this limitation with the second-generation ChebNet and third-generation GCN.
GCN
GCN is the best-known spectral convolutional GNN. It is an -layer network with a more familiar neural-network structure. As shown in the table, its hidden layer uses aggregator as follows.
For the resulting hidden embeddings, the output layer is
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.