Skip to content

Simple Graph Embeddings

Introduction

Graph embeddings are a way of representing graph nodes, edges, or entire graphs as vectors in a continuous vector space. This transformation allows the application of various machine learning algorithms directly on the graph data. The goal of graph embeddings is to capture the graph's structural and relational information in a lower-dimensional space.

Mathematical Definition

Given a graph ( G = (V, E) ), where ( V ) is the set of vertices and ( E ) is the set of edges, a graph embedding is a function ( f: V \rightarrow \mathbb{R}^d ) that maps each node to a ( d )-dimensional vector.

Objective

The objective of graph embedding is to ensure that nodes that are close in the graph are also close in the embedding space. This can be mathematically formulated as minimizing the following objective function:

\[ \min \sum_{(u, v) \in E} \text{distance}(f(u), f(v)) \]
where ( \text{distance} ) is typically the Euclidean distance between the vectors ( f(u) ) and ( f(v) ).

Simple Embeddings

A simple approach to generate graph embeddings is to use random embeddings or basic aggregation techniques. One such technique is to initialize each node's embedding randomly and then optimize it using gradient descent to minimize the objective function.

Random Initialization

For a graph with \( N \) nodes, initialize a matrix \( W \in \mathbb{R}^{N \times d} \) where each entry \( W_{i,j} \) is sampled from a normal distribution \( \mathcal{N}(0, \sigma^2) \).

Optimization

The embeddings can be further refined using optimization techniques such as stochastic gradient descent (SGD) to minimize the objective function.

Example Code

Below is an example code that demonstrates how to generate simple graph embeddings using random initialization.

from euler.graph_api import KnowledgeGraphAPI
api = KnowledgeGraphAPI()

Create more nodes

api.create_node(id='1', label='Person', properties={'name': 'Alice', 'age': 30})
api.create_node(id='2', label='Person', properties={'name': 'Bob', 'age': 35})
api.create_node(id='3', label='Person', properties={'name': 'Charlie', 'age': 25})

Create edges

api.create_edge(id='1-2', source='1', target='2', label='knows', properties={'since': '2020'})
api.create_edge(id='2-3', source='2', target='3', label='knows', properties={'since': '2018'})
api.create_edge(id='3-1', source='3', target='4', label='knows', properties={'since': '2015'})

convert graph into Graph

network_graph = api.graph.to_networkx()

Generate a simple embeddings

api.graph.generate_embeddings(network_graph, method="simple", dimensions=64)
simple_embedding = api.graph.get_embedding('1')
print("Simple embedding for node '1':", simple_embedding)