Skip to main content

fastgraphml

Given an input graph it generates Graph Embeddings using Low-Code framework built on top of PyG. The package supports training on both GPU and CPU enabled machines. Training jobs on GPUs results in much faster execution and increased in performance when it comes to handling large graphs as compared to CPUs. In addition, the framework provides tight integration with ArangoDB which is a scalable, fully managed graph database, document store and search engine in one place. Once Graph Embeddings are generated, they can be used for various downstream machine learning tasks like Node Classification, Link Prediction, Visualisation, Community Detection, Similartiy Search, Recommendation, etc.

Installation

Required Dependencies

  1. PyTorch 1.12.* is required.
    • Install using previous version that matches your CUDA version: pytorch
      • To find your installed CUDA version run nvidia-smi in your terminal.
  2. pyg
  3. FAISS
    • Note: For FAISS-CPU one needs numba==0.53.0

Latest Release

pip install fastgraphml

Quickstart: Graph Embedding Generation

Use Case 1: Generates Graph Embeddings using the graphs stored inside ArangoDB:

Example Homogneous Graphs

from fastgraphml.graph_embeddings import SAGE, GAT
from fastgraphml.graph_embeddings import downstream_tasks
from fastgraphml import Datasets 
from arango import ArangoClient

# Initialize the ArangoDB client.
client = ArangoClient("http://127.0.0.1:8529")
db = client.db('_system', username='root', password='')

# Loading Amazon Computer Products dataset into ArangoDB
Datasets(db).load("AMAZON_COMPUTER_PRODUCTS")

# Optionally use arangodb graph
# arango_graph = db.graph('product_graph')

# metadata information of arango_graph
metagraph = {
    "vertexCollections": {
        "Computer_Products": {"x": "features", "y": "label"},
    },
    "edgeCollections": {
        "bought_together": {},
    },
}

# generating graph embeddings with 3 lines of code
model = SAGE(db,'product_graph', metagraph, embedding_size=64) # define graph embedding model
model._train(epochs=10) # train
embeddings = model.get_embeddings() # get embeddings

Example Heterogeneous Graphs

from fastgraphml.graph_embeddings import METAPATH2VEC, DMGI
from fastgraphml.graph_embeddings import downstream_tasks 
from fastgraphml import Datasets 

from arango import ArangoClient

# Initialize the ArangoDB client.
client = ArangoClient("http://127.0.0.1:8529")
db = client.db('_system', username='root')

# Loading IMDB Dataset into ArangoDB
Datasets(db).load("IMDB_X")

# Optionally use ArangoDB Graph
# arango_graph = db.graph("IMDB")

metagraph = {
    "vertexCollections": {

        "movie": { "x": "x", "y": "y"},  
        "director": {"x": "x"},
        "actor": {"x": "x"},
    },
    "edgeCollections": {
        "to": {},
    },
}
metapaths = [('movie', 'to','actor'),
             ('actor', 'to', 'movie'), ] # MAM # co-actor relationship

# generating graph embeddings with 3 lines of code
model = METAPATH2VEC(db, "IMDB_X", metagraph, metapaths, key_node='movie', embedding_size=128,
                     walk_length=5, context_size=6, walks_per_node=5, num_negative_samples=5,
                     sparse=True) # define model
model._train(epochs=10, lr=0.03) # train
embeddings = model.get_embeddings() # get embeddings

Use Case 2: Generates Graph Embeddings using PyG graphs:

from fastgraphml.graph_embeddings import SAGE, GAT
from fastgraphml.graph_embeddings import downstream_tasks 
from torch_geometric.datasets import Planetoid

# load pyg dataset
dataset = Planetoid(root='/tmp/Cora', name='Cora')
data = dataset[0]

# generating graph embeddings with 3 lines of code
model = SAGE(pyg_graph=data, embedding_size=64) # define graph embedding model
model._train(epochs=10) # train
embeddings = model.get_embeddings() # get embeddings

Models Supported

Model Homogeneous Heterogeneous Node Features
GraphSage ✔️ ✔️
GAT ✔️ ✔️
Metapath2Vec ✔️
DMGI ✔️ ✔️

Quickstart: Downstream Tasks

In addition, the library also provides various low-code helper methods to carry out number of downstream tasks such as visualisation, similarity search (recommendation) , and link prediction (to be added soon).

Downstream Task 1: Graph Embedding Visualisation

This method helps in visualization of generated Graph Embeddings by reducing them 2 dimensions using U-Map.

Example

# amazon computers dataset
class_names = {0: 'Desktops',1: 'Data Storage',2: 'Laptops',3: 'Monitors',4: 'Computer Components',
 5: 'Video Projectors',6: 'Routers',7: 'Tablets',8: 'Networking Products',9: 'Webcams'}
# with one line of code
downstream_tasks.visualize_embeddings(model.G, embeddings, class_mapping=class_names, emb_percent=0.1) # model.G is PyG data object

Downstream Task 2: Scalable Similarity Search with Faiss

Faiss is a tool developed by Facebook that performs similarity search in sets of vectors of any size, up to ones that possibly do not fit in RAM. We support two types of search for now:

  1. exact search: For precise similarity search but at the cost of scalability.
  2. approx search: For scalable similarity search but at the cost of some precision loss.

Example 1

downstream_tasks.similarity_search(embeddings, top_k_nbors=10, nlist=10, search_type='exact')

Example 2

If nearest_nbors_search=True, store_embeddings method saves generated Graph Embeddings in ArangoDB along with top_k nearest neighbors (node ids with similar embeddings) and their corresponding similarity scores (i.e. cosine distance).

model.graph_util.store_embeddings(embeddings, collection_name=None, batch_size=100, class_mapping=None, 
        nearest_nbors_search=False, top_k_nbors=10, nlist=10, search_type='exact')

Release files for fastgraphml 0.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastgraphml 0.0.0
File Size Uploaded
fastgraphml-0.0.0.tar.gz 177.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastgraphml 0.0.0
File Interpreter ABI Platform
fastgraphml-0.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 205.5 kB

Release files / fastgraphml-0.0.0.tar.gz

Download URL fastgraphml-0.0.0.tar.gz
Size 177.0 kB
Tags Source
SHA-256 checksum
How to use checksums
de0e284930c71a16079ae3b4b526173b3a412edd1cb0bfe2e9b320ce9edf8e9a
BLAKE2b-256 checksum
How to use checksums
f150630a0c2a713a98bb0f72199837f80a2ffb729b0be24adc48b076e1dbf745
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.13

Release files / fastgraphml-0.0.0-py3-none-any.whl

Download URL fastgraphml-0.0.0-py3-none-any.whl
Size 28.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1190dd64e6100c4bd1c91d1845a7d96ec27262a4da44bdfde19b32457e0b8495
BLAKE2b-256 checksum
How to use checksums
609746723ce644f545ee76c7cf65ac06579456065d43efe4be39a0c0a4f7b611
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.8.13

Release history Release notifications | RSS feed

This release

0.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page