A node embedding is a fixed-size numerical vector that represents a node in a graph. It compresses information about the node’s position and relationships into a form usable for visualization, clustering, or prediction. The useful information depends on how the vector was made: a random-walk method captures graph topology, while a graph neural network (GNN) can combine topology with node features. section 2.1.2 section 2.0
Two practical ways to produce node embeddings are:
- Node2Vec: start random walks from nodes, learn from the nodes encountered, and retrieve a vector for each node. Walk length and walks per node control how much context is sampled; return and in-out parameters adjust how the walks explore. This is a separate embedding-generation step tied to the graph used to train it. section 2.1 section 2.1.1 section 2.5
- GNN: pass node features and graph connections through message-passing layers. Each layer combines a node’s information with aggregated information from its neighbors, updating the embedding. Training can refine those vectors for a prediction task rather than producing them in a separate step. section 2.2.1 section 2.3.3
From co-purchased books to vectors
In the Political Books graph, nodes are books and edges indicate frequent co-purchases. Load the GML graph with NetworkX, fit the Python Node2Vec library, and retrieve a book’s vector with `model.wv[str(node)]`. A UMAP projection can then put the vectors in two dimensions to inspect patterns. Treat that projection as a view of the embeddings, not as the original graph. section 2.1 section 2.1.1 section 2.1.2
For a GNN implementation, a two-layer PyTorch Geometric model can take `data.x` and `data.edge_index`, apply `GCNConv`, and return the resulting node vectors. A forward pass through an untrained model demonstrates how layers construct embeddings, but optimization is needed to tailor them to classification or link prediction. To inspect vectors separately from predictions, return the output of the last graph-convolution layer before applying the classification activation. section 2.2.1 section 2.3.3 section 3.1.2
Listing 2.4 Our SimpleGNN class
class SimpleGNN_embeddings(torch.nn.Module):
def __init__(self, num_features, hidden_channels): #1
super(SimpleGNN, self).__init__()
self.conv1 = GCNConv(num_features, \
hidden_channels) #2
self.conv2 = GCNConv(hidden_channels,\
hidden_channels) #3
def forward(self, x, edge_index): #4
x = self.conv1(x, edge_index) #5
x = torch.relu(x) #6
x = torch.dropout(x, p=0.5, train=self.training) #7
x = self.conv2(x, edge_index) #8
return x #9
The two `GCNConv` calls show where node features and graph connections become updated node vectors. section 2.2.1
Embeddings can also be frozen and used as features for a conventional classifier. In that workflow, align every vector with its node label before applying a labeled-data mask: Node2Vec vectors need explicit ordering, while the GNN output follows the input node order. Choose the method with the deployment setting in mind. Node2Vec must be reapplied to new nodes or graphs; a learned GNN mapping can work on unseen graph data when its node features have a similar feature space. section 2.3.1 section 2.2.2 section 2.3.3














































