AmpliGraph Documentation

repository·develop·Indexed 24 days ago

https://github.com/accenture/ampligraph

An open-source Python library based on TensorFlow 2 and Keras-style APIs for relational learning on knowledge graphs. It provides tools for predicting links between concepts using neural machine learning models, including embedding models like TransE, DistMult, ComplEx, and HolE. The library includes modules for dataset loading (including disk-based backends for massive datasets), model evaluation using metrics like MRR and Hits@N, and discovery tasks such as fact discovery, entity clustering, and duplicate detection.

Tokens
37.9K
Snippets
78
Records
140
Agent score
81%

What's inside AmpliGraph

  1. Overview of AmpliGraph

    develop

    AmpliGraph is an open-source Python library designed for Relational Learning, a branch of machine learning focused on supervised learning on knowledge graphs. It uses neural machine learning models to generate knowledge graph embeddings (vector representations of concepts in a metric space) and combines them with scoring functions to predict unseen or novel links.

    Core Use Cases:

    • Discovering new knowledge from existing knowledge graphs.
    • Completing large knowledge graphs by filling in missing statements.
    • Generating stand-alone knowledge graph embeddings.
    • Developing and evaluating new relational models.

    Key Technical Features:

    • Keras-style APIs: The AmpliGraph 2.x APIs follow a Keras-like pattern for a smoother user experience.
    • GPU-Ready: Built on top of TensorFlow 2, allowing seamless execution on both CPU and GPU devices for accelerated training.
    • Extensible: Users can implement custom knowledge graph embedding models by extending AmpliGraph base estimators.
  2. Explore the AmpliGraph API submodules

    develop

    AmpliGraph provides a structured API organized into five main submodules that cover the entire lifecycle of Knowledge Graph Embedding (KGE) models. These submodules allow you to load datasets, train models, evaluate performance, and perform downstream tasks like link prediction or entity discovery.

    The core submodules are:

    • ampligraph.datasets: Tools for loading benchmark datasets or customized user datasets.
    • ampligraph.latent_features: Interfaces for working with latent feature representations.
    • ampligraph.evaluation: Metrics and tools for evaluating model performance (e.g., MRR, Hits@N).
    • ampligraph.discovery: APIs for discovery-related tasks in knowledge graphs.
    • ampligraph.utils: General utility functions for model management, such as saving and reloading trained models.
    • ampligraph.pretrained_models: Access to pre-trained model weights and architectures.
  3. Use the ampligraph.discovery module for knowledge graph analysis

    develop

    The ampligraph.discovery module provides high-level functions for performing discovery tasks on a trained AmpliGraph model. It allows you to extract new information, identify patterns, and query the model for specific types of knowledge.

    Key capabilities include:

    • Fact Discovery: Finding new potential facts (triples) that the model predicts with high confidence.
    • Cluster Identification: Finding groups of entities that are closely related in the embedding space.
    • Duplicate Detection: Identifying entities that are likely duplicates based on their embeddings.
    • Top-N Querying: Retrieving the top $N$ most likely entities for a specific subject-relation pair.
  4. Overview of AmpliGraph modules

    develop

    AmpliGraph is a suite of neural machine learning models for relational learning on knowledge graphs. It is built on TensorFlow 2 and uses Keras-style APIs. The library is organized into several functional modules:

    • Datasets: Helper functions for loading knowledge graphs.
    • Models: Knowledge graph embedding models. AmpliGraph 2 includes TransE, DistMult, ComplEx, and HolE.
    • Evaluation: Metrics and protocols to assess model predictive power.
    • Discovery: High-level APIs for discovering new facts, clustering entities, and predicting near duplicates.
    • Compat: Provides compatibility for users transitioning from AmpliGraph 1.x to 2.x APIs.
  5. How the AmpliGraph data pipeline handles massive datasets

    develop

    AmpliGraph uses a data pipeline designed to handle datasets that are too large to fit in CPU or GPU memory. The pipeline consists of a data handler that leverages the GraphDataLoader to move data from a source into a specific backend.

    To handle massive datasets, you must use a disk-based backend instead of the default in-memory storage. This allows data to be persisted on disk and loaded into memory in manageable chunks during training, preventing RAM overload.

  6. How ScoringBasedEmbeddingModel works in AmpliGraph

    develop

    In AmpliGraph 2, Knowledge Graph Embedding (KGE) models are implemented using the ScoringBasedEmbeddingModel class. This class inherits from tf.keras.Model, allowing you to use standard Keras features like initializers (e.g., HeNormal, GlorotNormal), regularizers (L1, L2), optimizers (Adam, AdaGrad), and callbacks (e.g., early stopping).

    A model's anatomy consists of several layers and components:

    • Embedding Generation Layer: Generates embeddings for entities and relations (currently uses EmbeddingLookupLayer).
    • Negatives Generation Layer: Generates synthetic negative triples (e.g., CorruptionGenerationLayerTrain).
    • Scoring Layer: Applies a scoring function to a triple to represent its plausibility.
    • Loss Function: Optimizes the scores (e.g., PairwiseLoss, NLLLoss).
    • Optimizer & Regularizer: Standard Keras-compatible components.
  7. Understand Knowledge Graph Embedding (KGE) scoring functions

    develop

    AmpliGraph implements various neural architectures to encode entities and relations into low-dimensional vectors. These models use a scoring function $f_{m}(t)$ to assign a score to a triple $t=(sub, pred, obj)$. The goal is to learn embeddings such that positive statements receive high scores and unlikely statements receive low scores.

    Key scoring models available in AmpliGraph include:

    • TransE: Relies on distances (e.g., $f_{TransE}=-||\mathbf{e}{sub} + \mathbf{e}{pred} - \mathbf{e}_{obj}||_n$).
    • DistMult: A bilinear-diagonal model.
    • ComplEx: A bilinear-diagonal model.
    • RotatE: Models relations as rotations in complex space.
    • HolE: Uses circular correlation.
    • ConvE: Uses convolutional layers.
  8. Predictive performance metrics in AmpliGraph

    develop

    AmpliGraph reports predictive power using filtered metrics. When evaluating models on standard datasets like FB15K-237, WN18RR, or YAGO3-10, the following metrics are used:

    • MR: Mean Rank
    • MRR: Mean Reciprocal Rank
    • Hits@1: Hits at rank 1
    • Hits@3: Hits at rank 3
    • Hits@10: Hits at rank 10
  9. Explore AmpliGraph Modules

    develop

    AmpliGraph is organized into several functional submodules to support the full relational learning workflow:

    • Datasets: Helper functions for loading knowledge graph datasets.
    • Models: Contains knowledge graph embedding models. Supported models include TransE, DistMult, ComplEx, HolE, and RotatE.
    • Evaluation: Provides metrics and evaluation protocols to assess model predictive power.
    • Discovery: High-level convenience APIs for tasks like discovering new facts, clustering entities, and predicting near-duplicates.
    • Compat: Provides compatibility with AmpliGraph 1.x APIs for users migrating from older versions.
  10. Partition graphs for large-scale training

    develop

    When using a disk-based backend, you must define how data is split into chunks that fit in memory. This process is known as graph partitioning. The pipeline splits nodes into $P$ partitions; during training, the model loads one partition, trains on it, unloads it, and then loads the next.

    AmpliGraph provides different partitioning strategies, but for optimal runtime performance, it is recommended to use the BucketGraphPartitioner strategy.

  11. Use embeddings for clustering and classification

    develop
    The ClusteringAndClassificationWithEmbeddings tutorial demonstrates how to use knowledge embeddings generated by AmpliGraph for downstream machine learning tasks. Specifically, it shows how to take embeddings from a graph (using an example of international football matches) and apply them to clustering and classification workflows.