Graph Embeddings
What is a Graph?
Graphs provide a unique way of displaying relationships between entities. An entity can be people on a social media platform, research papers, or even IP addresses on a network.
Entities are represented by nodes, and these nodes have relationships with other nodes. This relationship is denoted by an edge which is a line connecting one node to the other. If we take a look at the graph above there is 7 nodes and 6 edges. Node A has an edge shared with nodes F, B, C and Node B has an edge shared with E and A.
In graph theory there is first order proximity and second order proximity. First order proximity are all the current nodes direct neighbors (shared edge) and second order proximity are the neighbors of the nodes neighbors. For C its first order neighbors would be D, A and G and second order neighbors would be F and B. This will not be mentioned throughout this tutorial, it was just to provide additional graph definitions that might be relevant to your train of thought.
What is an Embedding?
An embedding is the product of taking a sparse and large piece of information and condensing it to a smaller and meaningful representation. It is commonly used in the field of natural language processing (NLP) and a foundational component to the modern day LLMs (ChatGPT, Claude, etc).
There are many algorithms for creating embeddings, for graph embeddings a common algorithm used is based on an NLP model called Word2Vec. The Word2Vec algorithm creates embeddings on the bases of co-occurrent relationships between words. This is based on the distribution hypothesis where words that appear in similar context can have similar meaning. The same principle can be applied to graph embeddings as well: nodes that appear near each other can have similar meaning.
To give a general example of what an embedding looks like, table 1 provides two words and a three-dimensional vector for each. For the purpose of trying to simplify the embedding concept, the embeddings were made to be smaller than usual. In most research embeddings are larger with embedding sizes going up by 64 (64, 128, 256, etc). Deciding on the size depends on a variety of factors such as the business problem, data size, etc. The two embeddings appear to be close in value per dimension, if we were to take the similarity between the two they would have a high similarity (how the similarity is calculated will be discussed further along this tutorial).
Lets Combine Graph with Embedding
Now that we know what a graph and an embedding is, lets combine the two. Remember the purpose of an embedding is to provide a meaningful representation of something sparse and large. Graphs can be a perfect example of this because graphs can grow exponentially with data. There are four type of embeddings: node embeddings, edge embeddings, subgraph embeddings and graph embeddings. Deciding on which embedding is based on the business problem at hand. Node embeddings are useful for PubMed paper topic prediction. Where each node was a paper and the goal was to predict the topic of the paper based on the nodes features and the surrounding nodes features. Edge embeddings can be used to predict whether two people on a social media website are friends or not. Subgraph and Graph embeddings can apply to anomaly detection across ip addresses on a network.
DeepWalk
Deep Walk creates node embeddings by applying a random walk method to create the node sequences and then feeds the sequences into a Word2Vec model that will then create the embeddings.[3]
Random Walk
Random walk is a simple algorithm, it is used to create the representation of the nodes to feed into the model. It randomly selects a node on the graph and then from that node it randomly chooses one of its neighbors. This keeps going until a walk length is met. A walk length is a parameter that determines the length of the walks that will be passed into the word2vec model. The number of walks is another parameter that is implemented that gives the number of random walks that will occur.
Lets take a look at the original graph and do a random walk. Lets say we have a walk length of 4 and the first node randomly assigned is A. Then after A it goes to C, then D then back to C. The result is sequence is [A, C, D, C]. After creating N walks each of the sequences is converted to an N x M (number of unique nodes) matrix. For our example above there are a total of seven distinct nodes. Each vector in the matrix is going to be a length of seven. For example: A, C, D, C will be as follows:
[[1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0]]
One important point to note, if you are familiar with one hot encoding it is understandable that over time the larger the graph gets the more expensive this operation can be. After building out this dataset a word2vec model is then used to convert the one hot encodings into embedding. For the purpose of focusing on graph embeddings this article will give a brief description of what a word2vec model is.
A word2vec model is a model used frequently in the field of natural language processing and focuses on the idea of creating word embeddings based on the co-occurrence of words. Words that appear frequently together can have similar context. The same principle can be applied to graph nodes, nodes that appear in similar context have similar meaning.
How is the model applied to graph embeddings?
Initially, all the random walk sequences will be collected and these will be passed through the word2vec model. The intial step would be to generate a hidden layer with a size (n x m) matrix. The n is the number of nodes in total within the graph and m is the dimension size of the hidden layer. This hidden layer will represent the embeddings. There is a total of 7 nodes so the lenght of the matrix will be of size 7. In order to pick a good dimension size for M, I would refer you to go to research papers below. For our example and to provide a understand we are only going to do a embedding dimension of 2. Is this practical? absolutely not. But for the sake of understanding, we are going to use 2. Lets get our randomly intialized embeddings for the nodes:
Node A: [0.30471707975443135, -1.0399841062404955]
Node B: [0.7504511958064572, 0.9405647163912139]
Node C: [-1.9510351886538364, -1.302179506862318]
Node D: [0.12784040316728537, -0.3162425923435822]
Node E: [-0.016801157504288795, -0.85304392757358]
Node F: [0.8793979748628286, 0.7777919354289483]
Node G: [0.06603069756121605, 1.1272412069680329]
Each node is provided with its own embedding, these embeddings will be updated throughout training.
Get Aaron Zellner’s stories in your inbox
Join Medium for free to get updates from this writer.
How will these embedding be updated?
Returning to the Word2Vec model, it offers two training approaches: Skip‑Gram and Continuous Bag of Words (CBOW). Skip‑Gram predicts the surrounding context words (based on a chosen window size) from a single target word, while CBOW does the opposite by predicting the target word from its surrounding context. I will give a brief description of how the skip gram model works. There is some math to it, but that is out of the scope of this tutorial.
The context window defines how many words before and after the target word the model considers. For example, with a window size of 2 and a sequence length of 5, the target word can only look two positions ahead and two positions behind. At the start of the sequence, the first target word has no previous words available, so its context only includes the next two words.
For this experiment, we’ll use the Skip‑Gram approach. Skip‑Gram training consists of two main steps: the forward pass and backpropagation. In the forward pass, the target node is fed into a shallow neural network with a single hidden layer, producing a 1×N softmax distribution over the entire node graph. Each value in this vector represents the probability that a given node is the correct context node. This process is repeated for every context node in the window. Using the cross‑entropy loss for each prediction, the model updates the weights associated with the target node. Over time, these updates reduce the cross entropy, and nodes that frequently appear together in sequences develop embeddings that move closer to one another.
Results
Simple note: The embeddings you seen above are not the same ones used for the results. Those above were an example of showing what the inputs will look like.
For this example, we will take two nodes: A and B and identify which nodes are the most similar to them. After running the word2vec model we take the embeddings of all the nodes and apply cosine similarity which shows how similar two nodes are to one another. For node A, the top 3 most similar nodes can be found below.
If you refer back to the graph above, it makes sense that F and B have the highest similarity score to A because they both appear closest to it in the graph. It is interesting that E has a higher similarity score than C but this is because of randomess of the random walk algorithm. There were more sequences that were randomly generated with E and A than there were with C and A. Now lets look at node B:
Node B’s most related nodes are E and A which structurally aligns with the graph because node E and A are both direct neighbors to B.
Conclusion
Overall, the purpose of the introduction was to provide a simple explanation on how graph embeddings work. My goal for this tutorial was to be broad and expand not just to technical folks, but to non technical as well. I hope you enjoyed this read and all feedback is appreciated :) .
References
[1] Cai, H., Zheng, V. W., & Chang, K. C.-C. (2018). A comprehensive survey of graph embedding: Problems, techniques and applications. arXiv. https://arxiv.org/abs/1709.07604
[2] Velickovic, P., Cucurull, G., Casanova, A., Romero, A., Lio, P., & Bengio, Y. (2018). Graph Attention Networks. In Proceedings of the International Conference on Learning Representations (ICLR). arXiv:1710.10903.
[3] Perozzi, B., Al‑Rfou, R., & Skiena, S. (2014). DeepWalk: Online learning of social representations. arXiv:1403.6652.
[4] Kipf, T. N., & Welling, M. (2017). Semi-supervised classification with graph convolutional networks. arXiv:1609.02907. https://arxiv.org/abs/1609.02907
[5] Bruna, J., Zaremba, W., Szlam, A., & LeCun, Y. (2014). Spectral networks and deep locally connected networks on graphs. arXiv:1312.6203. https://arxiv.org/abs/1312.6203
[6] Kipf, T. N., & Welling, M. (2017). Semi-supervised classification with graph convolutional networks. arXiv:1609.02907. https://arxiv.org/abs/1609.02907
[7] Liang, A. (2025). A graph attention-based recommendation framework for sparse user-item interactions. Journal of Computer Science and Software Applications, 5(4).
[8] Liu, Y., Yang, S., Xu, Y., Miao, C., Wu, M., & Zhang, J. (2023). Contextualized graph attention network for recommendation with item knowledge graph. IEEE Transactions on Knowledge and Data Engineering, 35(1), 181–195. https://doi.org/10.1109/TKDE.2021.3082948
[9] Wang, X., He, X., Cao, Y., Liu, M., & Chua, T.-S. (2019, August). KGAT: Knowledge Graph Attention Network for Recommendation. In Proceedings of the 25th ACM SIGKDD International Conference on Knowledge Discovery & Data Mining (pp. 950–958). ACM. https://doi.org/10.1145/3292500.3330989
[10] Tao, Z., Wei, Y., Wang, X., He, X., Huang, X., & Chua, T.-S. (2020). MGAT: Multimodal Graph Attention Network for Recommendation. Information Processing & Management, 57(5), Article 102277. https://doi.org/10.1016/j.ipm.2020.102277










