Package: fastgraphml
fastgraphmlGiven an input graph it generates Graph Embeddings using Low-Code framework built on top ofPyG. 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 withArangoDBwhich 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.InstallationRequired DependenciesPyTorch1.12.*is required.Install using previous version that matches your CUDA version:pytorchTo find your installed CUDA version runnvidia-smiin your terminal.pygFAISSNote: For FAISS-CPU one needsnumba==0.53.0Latest Releasepip install fastgraphmlQuickstart: Graph Embedding GenerationUse Case 1: Generates Graph Embeddings using the graphs stored inside ArangoDB:Example Homogneous Graphsfromfastgraphml.graph_embeddingsimportSAGE,GATfromfastgraphml.graph_embeddingsimportdownstream_tasksfromfastgraphmlimportDatasetsfromarangoimportArangoClient# 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 ArangoDBDatasets(db).load("AMAZON_COMPUTER_PRODUCTS")# Optionally use arangodb graph# arango_graph = db.graph('product_graph')# metadata information of arango_graphmetagraph={"vertexCollections":{"Computer_Products":{"x":"features","y":"label"},},"edgeCollections":{"bought_together":{},},}# generating graph embeddings with 3 lines of codemodel=SAGE(db,'product_graph',metagraph,embedding_size=64)# define graph embedding modelmodel._train(epochs=10)# trainembeddings=model.get_embeddings()# get embeddingsExample Heterogeneous Graphsfromfastgraphml.graph_embeddingsimportMETAPATH2VEC,DMGIfromfastgraphml.graph_embeddingsimportdownstream_tasksfromfastgraphmlimportDatasetsfromarangoimportArangoClient# Initialize the ArangoDB client.client=ArangoClient("http://127.0.0.1:8529")db=client.db('_system',username='root')# Loading IMDB Dataset into ArangoDBDatasets(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 codemodel=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 modelmodel._train(epochs=10,lr=0.03)# trainembeddings=model.get_embeddings()# get embeddingsUse Case 2: Generates Graph Embeddings using PyG graphs:fromfastgraphml.graph_embeddingsimportSAGE,GATfromfastgraphml.graph_embeddingsimportdownstream_tasksfromtorch_geometric.datasetsimportPlanetoid# load pyg datasetdataset=Planetoid(root='/tmp/Cora',name='Cora')data=dataset[0]# generating graph embeddings with 3 lines of codemodel=SAGE(pyg_graph=data,embedding_size=64)# define graph embedding modelmodel._train(epochs=10)# trainembeddings=model.get_embeddings()# get embeddingsModels SupportedModelHomogeneousHeterogeneousNode FeaturesGraphSage✔️✔️GAT✔️✔️Metapath2Vec✔️DMGI✔️✔️Quickstart: Downstream TasksIn 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 VisualisationThis method helps in visualization of generated Graph Embeddings by reducing them 2 dimensions using U-Map.Example# amazon computers datasetclass_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 codedownstream_tasks.visualize_embeddings(model.G,embeddings,class_mapping=class_names,emb_percent=0.1)# model.G is PyG data objectDownstream Task 2: Scalable Similarity Search with FaissFaissis 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:exact search: For precise similarity search but at the cost of scalability.approx search: For scalable similarity search but at the cost of some precision loss.Example 1downstream_tasks.similarity_search(embeddings,top_k_nbors=10,nlist=10,search_type='exact')Example 2If 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')