### Clone and Install AmpliGraph in Editable Mode Source: https://docs.ampligraph.org/en/latest/dev.html Clone the AmpliGraph repository, checkout the develop branch, and install the package in editable mode using pip. This setup is for development purposes. ```bash git clone https://github.com/Accenture/AmpliGraph.git git checkout develop cd AmpliGraph pip install -e . ``` -------------------------------- ### Check AmpliGraph Installation (Stable) Source: https://docs.ampligraph.org/en/latest/install.html Verify the installation of the stable AmpliGraph release by importing it and checking its version. ```python >>> import ampligraph >>> ampligraph.__version__ '2.1.0' ``` -------------------------------- ### Install AmpliGraph from Pip Source: https://docs.ampligraph.org/en/latest/install.html Install the latest stable release of AmpliGraph using pip after TensorFlow has been successfully installed. ```bash pip install ampligraph ``` -------------------------------- ### Check AmpliGraph Installation (Develop) Source: https://docs.ampligraph.org/en/latest/install.html Verify the installation of the development version of AmpliGraph by importing it and checking its version. ```python >>> import ampligraph >>> ampligraph.__version__ '2.1-dev' ``` -------------------------------- ### Model Training Example Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.latent_features.ScoringBasedEmbeddingModel.html A comprehensive example demonstrating loading a dataset, initializing a ScoringBasedEmbeddingModel, compiling it, and fitting it to the training data. ```APIDOC ## Model Training Example ### Description This example shows the typical workflow for training a knowledge graph embedding model using Ampligraph, including data loading, model instantiation, compilation, and fitting. ### Request Example ```python >>> from ampligraph.datasets import load_fb15k_237 >>> from ampligraph.latent_features import ScoringBasedEmbeddingModel >>> X = load_fb15k_237() >>> model = ScoringBasedEmbeddingModel(eta=5, >>> k=300, >>> scoring_type='ComplEx', >>> seed=0) >>> model.compile(optimizer='adam', loss='nll') >>> model.fit(X['train'], >>> batch_size=10000, >>> epochs=5) Epoch 1/5 29/29 [==============================] - 2s 61ms/step - loss: 67361.3047 Epoch 2/5 29/29 [==============================] - 1s 35ms/step - loss: 67318.6094 Epoch 3/5 29/29 [==============================] - 1s 34ms/step - loss: 67020.0703 Epoch 4/5 29/29 [==============================] - 1s 34ms/step - loss: 65867.3750 Epoch 5/5 29/29 [==============================] - 1s 34ms/step - loss: 63517.9062 ``` ``` -------------------------------- ### Verify AmpliGraph Installation Source: https://docs.ampligraph.org/en/latest/tutorials/AmpliGraphBasicsTutorial.html Check the installed version of the AmpliGraph library. ```python import numpy as np import pandas as pd import ampligraph ampligraph.__version__ ``` ```text '2.0-dev' ``` -------------------------------- ### Launch Tensorboard Server Source: https://docs.ampligraph.org/en/latest/tutorials/AmpliGraphBasicsTutorial.html Command line instruction to start the Tensorboard server and visualize the generated embeddings. ```bash tensorboard --logdir=./visualizations ``` -------------------------------- ### Example Usage: Iterating Through Partitions Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.datasets.BucketGraphPartitioner.html Shows how to iterate through the partitions created by BucketGraphPartitioner and access the data within each partition. ```APIDOC ### Request Example ```python >>> import numpy as np >>> from ampligraph.datasets import GraphDataLoader, BucketGraphPartitioner >>> d = np.array([[1,1,2], [1,1,3],[1,1,4],[5,1,3],[5,1,2],[6,1,3],[6,1,2],[6,1,4],[6,1,7]]) >>> data = GraphDataLoader(d, batch_size=1, dataset_type="test") >>> partitioner = BucketGraphPartitioner(data, k=2) >>> for i, partition in enumerate(partitioner): >>> print("partition ", i) >>> for batch in partition: >>> print(batch) ``` ### Response Example ``` partition 0 [['0,0,1']] [['0,0,2']] [['0,0,3']] partition 1 [['4,0,1']] [['4,0,2']] [['5,0,1']] [['5,0,2']] [['5,0,3']] partition 2 [['5,0,6']] ``` ``` -------------------------------- ### Example Usage: Training a Model with BucketGraphPartitioner Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.datasets.BucketGraphPartitioner.html Demonstrates how to use BucketGraphPartitioner to partition a dataset and then train a ScoringBasedEmbeddingModel. ```APIDOC ### Request Example ```python >>> from ampligraph.datasets import load_fb15k_237, GraphDataLoader, BucketGraphPartitioner >>> from ampligraph.datasets.sqlite_adapter import SQLiteAdapter >>> from ampligraph.latent_features import ScoringBasedEmbeddingModel >>> dataset = load_fb15k_237() >>> dataset_loader = GraphDataLoader(dataset['train'], >>> backend=SQLiteAdapter, # Type of backend to use >>> batch_size=1000, # Batch size to use while iterating over the dataset >>> dataset_type='train', # Dataset type >>> use_filter=False, # Whether to use filter or not >>> use_indexer=True) # indicates that the data needs to be mapped to index >>> partitioner = BucketGraphPartitioner(dataset_loader, k=2) >>> # create and compile a model as usual >>> partitioned_model = ScoringBasedEmbeddingModel(eta=2, k=50, scoring_type='DistMult') >>> partitioned_model.compile(optimizer='adam', loss='multiclass_nll') >>> partitioned_model.fit(partitioner, # The partitioner object generate data for the model during training >>> epochs=10) # Number of epochs ``` ``` -------------------------------- ### RDF N-Triples Serialization Example Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.datasets.load_from_ntriples.html Example format for an ntriples file required by the load_from_ntriples function. ```text _:alice _:bob . _:bob _:alice . ``` -------------------------------- ### Initialize and Compile ScoringBasedEmbeddingModel Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.latent_features.ScoringBasedEmbeddingModel.html Example showing how to initialize the model with specific hyperparameters and compile it with an optimizer and loss function. ```python >>> from ampligraph.datasets import load_fb15k_237 >>> from ampligraph.latent_features import ScoringBasedEmbeddingModel >>> import numpy as np >>> dataset = load_fb15k_237() >>> model = ScoringBasedEmbeddingModel(eta=5, >>> k=300, >>> scoring_type='ComplEx') >>> model.compile(optimizer='adam', loss='nll') ``` -------------------------------- ### Find Duplicates Example Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.discovery.find_duplicates.html Demonstrates how to prepare a dataset and extract triples for duplicate detection using AmpliGraph. ```python >>> import pandas as pd >>> import numpy as np >>> import re >>> from ampligraph.latent_features.models import ScoringBasedEmbeddingModel >>> # The IMDB dataset used here is part of the Movies5 dataset found on: >>> # The Magellan Data Repository (https://sites.google.com/site/anhaidgroup/projects/data) >>> import requests >>> url = 'http://pages.cs.wisc.edu/~anhai/data/784_data/movies5.tar.gz' >>> open('movies5.tar.gz', 'wb').write(requests.get(url).content) >>> import tarfile >>> tar = tarfile.open('movies5.tar.gz', "r:gz") >>> tar.extractall() >>> tar.close() >>> >>> # Reading tabular dataset of IMDB movies and filling the missing values >>> imdb = pd.read_csv("movies5/csv_files/imdb.csv") >>> imdb["directors"] = imdb["directors"].fillna("UnknownDirector") >>> imdb["actors"] = imdb["actors"].fillna("UnknownActor") >>> imdb["genre"] = imdb["genre"].fillna("UnknownGenre") >>> imdb["duration"] = imdb["duration"].fillna("0") >>> >>> # Creating knowledge graph triples from tabular dataset >>> imdb_triples = [] >>> >>> for _, row in imdb.iterrows(): >>> movie_id = "ID" + str(row["id"]) >>> directors = row["directors"].split(",") >>> actors = row["actors"].split(",") >>> genres = row["genre"].split(",") >>> duration = "Duration" + str(int(re.sub("\D", "", row["duration"])) // 30) >>> >>> directors_triples = [(movie_id, "hasDirector", d) for d in directors] >>> actors_triples = [(movie_id, "hasActor", a) for a in actors] ``` -------------------------------- ### Model training output logs Source: https://docs.ampligraph.org/en/latest/tutorials/AmpliGraphBasicsTutorial.html Example of the console output generated during the model training process. ```text 2023-02-08 23:06:27.469961: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz Epoch 1/200 2023-02-08 23:06:28.230759: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled. 2/2 [==============================] - 1s 668ms/step - loss: 7687.5562 Epoch 2/200 2/2 [==============================] - 0s 43ms/step - loss: 7683.3623 Epoch 3/200 2/2 [==============================] - 0s 48ms/step - loss: 7679.1382 Epoch 4/200 2/2 [==============================] - 0s 40ms/step - loss: 7674.8130 Epoch 5/200 2/2 [==============================] - 0s 48ms/step - loss: 7670.5405 Epoch 6/200 2/2 [==============================] - 0s 44ms/step - loss: 7666.1504 Epoch 7/200 2/2 [==============================] - 0s 41ms/step - loss: 7661.6445 Epoch 8/200 2/2 [==============================] - 0s 41ms/step - loss: 7657.1768 Epoch 9/200 2/2 [==============================] - 0s 46ms/step - loss: 7652.6147 Epoch 10/200 2/2 [==============================] - 0s 43ms/step - loss: 7647.9399 Epoch 11/200 2/2 [==============================] - 0s 41ms/step - loss: 7643.0947 Epoch 12/200 2/2 [==============================] - 0s 41ms/step - loss: 7638.1758 Epoch 13/200 2/2 [==============================] - 0s 48ms/step - loss: 7633.1064 Epoch 14/200 2/2 [==============================] - 0s 39ms/step - loss: 7627.8647 Epoch 15/200 2/2 [==============================] - 0s 39ms/step - loss: 7622.4819 Epoch 16/200 2/2 [==============================] - 0s 42ms/step - loss: 7616.9292 Epoch 17/200 2/2 [==============================] - 0s 44ms/step - loss: 7611.2173 Epoch 18/200 2/2 [==============================] - 0s 44ms/step - loss: 7605.2847 Epoch 19/200 2/2 [==============================] - 0s 40ms/step - loss: 7599.1416 Epoch 20/200 2/2 [==============================] - 0s 40ms/step - loss: 7592.7905 Epoch 21/200 2/2 [==============================] - 0s 46ms/step - loss: 7586.2324 Epoch 22/200 2/2 [==============================] - 0s 42ms/step - loss: 7579.4341 Epoch 23/200 2/2 [==============================] - 0s 40ms/step - loss: 7572.4092 Epoch 24/200 2/2 [==============================] - 0s 41ms/step - loss: 7565.1226 Epoch 25/200 2/2 [==============================] - 0s 47ms/step - loss: 7557.5513 Epoch 26/200 2/2 [==============================] - 0s 43ms/step - loss: 7549.6802 Epoch 27/200 2/2 [==============================] - 0s 40ms/step - loss: 7541.4810 Epoch 28/200 2/2 [==============================] - 0s 41ms/step - loss: 7533.0098 Epoch 29/200 2/2 [==============================] - 0s 43ms/step - loss: 7524.2012 Epoch 30/200 2/2 [==============================] - 0s 42ms/step - loss: 7515.0493 Epoch 31/200 2/2 [==============================] - 0s 40ms/step - loss: 7505.5547 Epoch 32/200 2/2 [==============================] - 0s 41ms/step - loss: 7495.6831 Epoch 33/200 2/2 [==============================] - 0s 44ms/step - loss: 7485.4292 Epoch 34/200 2/2 [==============================] - 0s 42ms/step - loss: 7474.8052 Epoch 35/200 2/2 [==============================] - 0s 41ms/step - loss: 7463.7358 Epoch 36/200 2/2 [==============================] - 0s 40ms/step - loss: 7452.2334 Epoch 37/200 2/2 [==============================] - 0s 47ms/step - loss: 7440.3193 Epoch 38/200 2/2 [==============================] - 0s 43ms/step - loss: 7427.9673 Epoch 39/200 2/2 [==============================] - 0s 40ms/step - loss: 7415.1470 Epoch 40/200 2/2 [==============================] - 0s 40ms/step - loss: 7401.8252 Epoch 41/200 2/2 [==============================] - 0s 47ms/step - loss: 7388.0103 Epoch 42/200 2/2 [==============================] - 0s 42ms/step - loss: 7373.6465 Epoch 43/200 2/2 [==============================] - 0s 40ms/step - loss: 7358.7339 Epoch 44/200 2/2 [==============================] - 0s 39ms/step - loss: 7343.2524 Epoch 45/200 2/2 [==============================] - 0s 46ms/step - loss: 7327.1968 Epoch 46/200 2/2 [==============================] - 0s 41ms/step - loss: 7310.5498 Epoch 47/200 2/2 [==============================] - 0s 40ms/step - loss: 7293.2598 Epoch 48/200 2/2 [==============================] - 0s 40ms/step - loss: 7275.4258 Epoch 49/200 2/2 [==============================] - 0s 47ms/step - loss: 7256.8550 Epoch 50/200 2/2 [==============================] - 0s 43ms/step - loss: 7237.6069 Epoch 51/200 2/2 [==============================] - 0s 39ms/step - loss: 7217.6772 Epoch 52/200 2/2 [==============================] - 0s 39ms/step - loss: 7197.0957 Epoch 53/200 ``` -------------------------------- ### Install TensorFlow 2.9.0 Source: https://docs.ampligraph.org/en/latest/install.html Install TensorFlow version 2.9.0 using pip. This is a common step before installing AmpliGraph. ```bash pip install "tensorflow==2.9.0" ``` -------------------------------- ### Install TensorFlow for macOS Source: https://docs.ampligraph.org/en/latest/install.html If you are installing TensorFlow on macOS, use this command instead of the standard TensorFlow installation. ```bash pip install "tensorflow-macos==2.9.0" ``` -------------------------------- ### Example Metric Output Source: https://docs.ampligraph.org/en/latest/tutorials/ClusteringAndClassificationWithEmbeddings.html This is an example output showing the calculated Mean Reciprocal Rank (MRR), Mean Rank (MR), and Hits@N scores for the evaluated model. ```text MRR: 0.30 MR: 2951.93 Hits@10: 0.43 Hits@3: 0.35 Hits@1: 0.23 ``` -------------------------------- ### Model Selection with Hyperparameter Grid Search Source: https://docs.ampligraph.org/en/latest/examples.html Illustrates how to perform a grid search over various hyperparameters to select the best performing embedding model. This example sets up a parameter grid for a ComplEx model and defines training parameters. ```python from ampligraph.datasets import load_wn18 from ampligraph.evaluation import select_best_model_ranking # load Wordnet18 dataset: X_dict = load_wn18() model_class = 'ComplEx' # Use the template given below for doing grid search. param_grid = { "batches_count": [10], "seed": 0, "epochs": [300], "k": [100, 50], "eta": [5,10], "loss": ["pairwise", "nll", "self_adversarial"], # We take care of mapping the params to corresponding classes "loss_params": { #margin corresponding to both pairwise and adverserial loss "margin": [0.5, 20], #alpha corresponding to adverserial loss "alpha": [0.5] }, "embedding_model_params": { # generate corruption using all entities during training "negative_corruption_entities":"all" }, "regularizer": [None, "LP"], "regularizer_params": { "p": [2], "lambda": [1e-4, 1e-5] }, "optimizer": ["adam"], "optimizer_params":{ "lr": [0.01, 0.0001] }, "verbose": True } # Train the model on all possibile combinations of hyperparameters. # Models are validated on the validation set. ``` -------------------------------- ### Load knowledge graph from CSV Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.datasets.load_from_csv.html Example of loading a knowledge graph from a CSV file using a comma separator. ```python >>> PATH_TO_FOLDER = 'your/path/to/folder/' >>> from ampligraph.datasets import load_from_csv >>> X = load_from_csv(PATH_TO_FOLDER, 'dataset.csv', sep=',') >>> X[:3] array([['a', 'y', 'b'], ['b', 'y', 'a'], ['a', 'y', 'c']], dtype='>> from ampligraph.datasets import load_fb15k_237 >>> from ampligraph.latent_features import ScoringBasedEmbeddingModel >>> X = load_fb15k_237() >>> model = ScoringBasedEmbeddingModel(eta=5, >>> k=300, >>> scoring_type='ComplEx', >>> seed=0) >>> model.compile(optimizer='adam', loss='nll') >>> model.fit(X['train'], >>> batch_size=10000, >>> epochs=5) Epoch 1/5 29/29 [==============================] - 2s 71ms/step - loss: 67361.3047 Epoch 2/5 29/29 [==============================] - 1s 35ms/step - loss: 67318.6094 Epoch 3/5 29/29 [==============================] - 1s 37ms/step - loss: 67020.0703 Epoch 4/5 29/29 [==============================] - 1s 35ms/step - loss: 65867.3750 Epoch 5/5 29/29 [==============================] - 1s 35ms/step - loss: 63517.9062 ``` -------------------------------- ### Model Fitted Confirmation Source: https://docs.ampligraph.org/en/latest/tutorials/AmpliGraphBasicsTutorial.html Example output confirming that the restored model has been successfully fitted. ```text The model is fit! ``` -------------------------------- ### Training Progress Output Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.discovery.find_duplicates.html Example output showing the training progress of the knowledge graph embedding model, including loss values per epoch. ```text Epoch 1/10 7/7 [==============================] - 1s 122ms/step - loss: 15612.8799 Epoch 2/10 7/7 [==============================] - 0s 20ms/step - loss: 15610.5010 Epoch 3/10 7/7 [==============================] - 0s 19ms/step - loss: 15607.7412 Epoch 4/10 7/7 [==============================] - 0s 19ms/step - loss: 15604.0674 Epoch 5/10 7/7 [==============================] - 0s 20ms/step - loss: 15598.9365 Epoch 6/10 7/7 [==============================] - 0s 19ms/step - loss: 15591.7188 Epoch 7/10 7/7 [==============================] - 0s 19ms/step - loss: 15581.6055 Epoch 8/10 7/7 [==============================] - 0s 20ms/step - loss: 15567.6807 Epoch 9/10 7/7 [==============================] - 0s 20ms/step - loss: 15548.8184 Epoch 10/10 7/7 [==============================] - 0s 21ms/step - loss: 15523.8721 ``` -------------------------------- ### Create and Activate Conda Environment Source: https://docs.ampligraph.org/en/latest/install.html Create a Conda environment named 'ampligraph' with Python 3.8 and activate it. This is recommended for macOS M1 chip installations. ```bash conda create --name ampligraph python=3.8 source activate ampligraph ``` -------------------------------- ### Interpreting Evaluation Metrics Source: https://docs.ampligraph.org/en/latest/tutorials/AmpliGraphBasicsTutorial.html Provides example output values for MRR and Hits@N metrics, illustrating typical results after model evaluation. ```text MRR: 0.26 Hits@10: 0.37 Hits@3: 0.28 Hits@1: 0.20 ``` -------------------------------- ### Build AmpliGraph Documentation Source: https://docs.ampligraph.org/en/latest/dev.html Build the HTML version of the project documentation locally using Sphinx. Navigate to the docs directory and run the make clean autogen html command. ```bash cd docs make clean autogen html ``` -------------------------------- ### Get NLLMulticlass loss using get function Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.latent_features.NLLMulticlass.html Retrieve an instance of the NLLMulticlass loss function using the 'get' utility function with the identifier 'multiclass_nll'. ```python loss = lfs.get('multiclass_nll') isinstance(loss, lfs.NLLMulticlass) True ``` -------------------------------- ### Get PairwiseLoss using get function Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.latent_features.PairwiseLoss.html Retrieve an instance of PairwiseLoss using the get function with the 'pairwise' identifier. This is a convenient way to access predefined loss functions. ```python loss = lfs.get('pairwise') isinstance(loss, lfs.PairwiseLoss) True ``` -------------------------------- ### Load and inspect PPI5K dataset Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.datasets.load_ppi5k.html Demonstrates importing the loader, retrieving the dataset, and accessing the first training triple and its corresponding numeric confidence value. ```python >>> from ampligraph.datasets import load_ppi5k >>> X = load_ppi5k() >>> X["train"][0] ['4001' '5' '4176'] >>> X['train_numeric_values'][0] [0.329] ``` -------------------------------- ### Initialize and Train a ScoringBasedEmbeddingModel Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.latent_features.ScoringBasedEmbeddingModel.html Demonstrates the standard workflow of loading data, configuring a loss function, compiling the model, and fitting it to training data. ```python >>> # create model and compile using user defined optimizer settings and >>> # user defined settings of an existing loss >>> from ampligraph.datasets import load_fb15k_237 >>> from ampligraph.latent_features import ScoringBasedEmbeddingModel >>> from ampligraph.latent_features.loss_functions import SelfAdversarialLoss >>> import tensorflow as tf >>> X = load_fb15k_237() >>> loss = SelfAdversarialLoss({'margin': 0.1, 'alpha': 5, 'reduction': 'sum'}) >>> model = ScoringBasedEmbeddingModel(eta=5, >>> k=300, >>> scoring_type='ComplEx', >>> seed=0) >>> model.compile(optimizer='adam', loss=loss) >>> model.fit(X['train'], >>> batch_size=10000, >>> epochs=5) ``` -------------------------------- ### Install TensorFlow 2.9.0 with Conda Source: https://docs.ampligraph.org/en/latest/install.html Install TensorFlow version 2.9.0 using either pip or Conda within your activated environment. ```bash pip install "tensorflow==2.9.0" or conda install "tensorflow==2.9.0" ``` -------------------------------- ### Get AbsoluteMarginLoss by name Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.latent_features.AbsoluteMarginLoss.html Retrieve an instance of AbsoluteMarginLoss using the get function with the identifier 'absolute_margin'. This is a convenient way to access predefined loss functions. ```python loss = lfs.get('absolute_margin') isinstance(loss, lfs.AbsoluteMarginLoss) True ``` -------------------------------- ### Install TensorFlow for Mac OS M1 Chip Source: https://docs.ampligraph.org/en/latest/install.html Install TensorFlow dependencies and specific packages for macOS with Apple silicon chips using Conda and pip. ```bash conda install -c apple tensorflow-deps pip install --user tensorflow-macos==2.9.0 pip install --user tensorflow-metal==0.6 ``` -------------------------------- ### Load O*NET20K Dataset Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.datasets.load_onet20k.html Loads the O*NET20K dataset. The dataset is automatically downloaded if not found locally. The `_check_md5hash`, `_clean_unseen`, `_split_test_into_top_bottom`, and `_split_threshold` parameters can be used to customize the loading process. ```python >>> from ampligraph.datasets import load_onet20k >>> X = load_onet20k() >>> X["train"][0] ['Job_27-1021.00' 'has_ability_LV' '1.A.1.b.2'] >>> X['train_numeric_values'][0] [0.6257143] ``` -------------------------------- ### Reproduce Experimental Results Source: https://docs.ampligraph.org/en/latest/experiments.html Execute the predictive performance script within the experiments directory to replicate the reported model benchmarks. ```bash $ cd experiments $ python predictive_performance.py ``` -------------------------------- ### Save a trained model to disk Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.utils.save_model.html Demonstrates initializing a ComplEx model, fitting it to data, and saving the resulting model instance to a pickle file. ```python >>> import numpy as np >>> from ampligraph.latent_features import ComplEx >>> from ampligraph.utils import save_model >>> model = ComplEx(batches_count=2, seed=555, epochs=20, k=10) >>> X = np.array([['a', 'y', 'b'], >>> ['b', 'y', 'a'], >>> ['a', 'y', 'c'], >>> ['c', 'y', 'a'], >>> ['a', 'y', 'd'], >>> ['c', 'y', 'd'], >>> ['b', 'y', 'c'], >>> ['f', 'y', 'e']]) >>> model.fit(X) >>> y_pred_before = model.predict(np.array([['f', 'y', 'e'], ['b', 'y', 'd']])) >>> example_name = 'helloworld.pkl' >>> save_model(model, model_name_path=example_name) >>> print(y_pred_before) [-0.29721245, 0.07865551] ``` -------------------------------- ### GET /discovery/find_clusters Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.discovery.find_clusters.html Documentation for the find_clusters discovery method. ```APIDOC ## GET /discovery/find_clusters ### Description Identifies clusters within the knowledge graph embeddings. ### Method GET ### Endpoint /discovery/find_clusters ``` -------------------------------- ### Create and Activate venv Environment Source: https://docs.ampligraph.org/en/latest/install.html Use this command to create a virtual environment with venv and activate it. Replace PATH/TO/NEW/VIRTUAL_ENVIRONMENT with your desired path. ```bash python3.8 -m venv PATH/TO/NEW/VIRTUAL_ENVIRONMENT source PATH/TO/NEW/VIRTUAL_ENVIRONMENT/bin/activate ``` -------------------------------- ### View Predictive Performance Help Source: https://docs.ampligraph.org/en/latest/experiments.html Display the command-line interface help menu for the predictive performance script to filter by dataset or model. ```bash $ python predictive_performance.py -h usage: predictive_performance.py [-h] [-d {fb15k,fb15k-237,wn18,wn18rr,yago310}] [-m {complex,transe,distmult,hole,rotate}] optional arguments: -h, --help show this help message and exit -d {fb15k,fb15k-237,wn18,wn18rr,yago310}, --dataset {fb15k,fb15k-237,wn18,wn18rr,yago310} -m {complex,transe,distmult,hole,rotate}, --model {complex,transe,distmult,hole,rotate} ``` -------------------------------- ### Load and inspect CN15K dataset Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.datasets.load_cn15k.html Demonstrates importing the loader, retrieving the dataset dictionary, and accessing the first triple and its corresponding numeric value from the training set. ```python >>> from ampligraph.datasets import load_cn15k >>> X = load_cn15k() >>> X["train"][0] ['260' '2' '13895'] >>> X['train_numeric_values'][0] [0.8927088] ``` -------------------------------- ### Model Training with fit method Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.latent_features.ScoringBasedEmbeddingModel.html Demonstrates the basic usage of the `fit` method for training an Ampligraph model, including essential parameters like epochs and batch size. ```APIDOC ## POST /api/train/model ### Description Trains an Ampligraph embedding model using the provided dataset. ### Method POST ### Endpoint /api/train/model ### Parameters #### Request Body - **X** (object) - Required - Training data. - **batch_size** (int) - Optional - Number of samples per gradient update. Defaults to 32. - **epochs** (int) - Optional - Number of epochs to train for. Defaults to 1. - **validation_freq** (int) - Optional - How often to run validation (default: 1). - **validation_burn_in** (int) - Optional - The burn-in time after which the validation kicks in (default: 0). - **validation_filter** (bool or dict) - Optional - Validation filter to be used. - **validation_entities_subset** (list or np.array) - Optional - Subset of entities to be used for generating corruptions. - **focusE** (bool) - Optional - Specify whether to include the FocusE layer (default: False). The FocusE layer allows to inject numeric edge attributes into the scoring layer. - **focusE_params** (dict) - Optional - If FocusE layer is included, specify its hyper-parameters. Possible keys: "non_linearity", "stop_epoch", "structural_wt". - **partitioning_k** (int) - Optional - Number of partitions to use while training (default: 1). - **callbacks** (list) - Optional - List of TensorFlow callbacks to be applied during training. ### Response #### Success Response (200) - **history** (History object) - A record of training loss values, as well as validation loss and validation metrics values. ### Request Example ```python from ampligraph.datasets import load_fb15k_237 from ampligraph.latent_features import ScoringBasedEmbeddingModel X = load_fb15k_237() model = ScoringBasedEmbeddingModel(eta=5, k=300, scoring_type='ComplEx', seed=0) model.compile(optimizer='adam', loss='nll') model.fit(X['train'], batch_size=10000, epochs=5) ``` ### Response Example ```json { "history": { "loss": [ 67361.3047, 67318.6094, 67020.0703, 65867.3750, 63517.9062 ] } } ``` ``` -------------------------------- ### Load and Train with GraphDataLoader and BucketGraphPartitioner Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.datasets.GraphDataLoader.html Demonstrates loading graph data using GraphDataLoader, partitioning it with BucketGraphPartitioner, and training a ScoringBasedEmbeddingModel. It also shows how to evaluate the model using a separate DataLoader for test data with index mapping. ```python >>> from ampligraph.datasets import GraphDataLoader, BucketGraphPartitioner >>> from ampligraph.datasets.sqlite_adapter import SQLiteAdapter >>> from ampligraph.latent_features import ScoringBasedEmbeddingModel >>> AMPLIGRAPH_DATA_HOME='/your/path/to/datasets/' >>> # Graph loader - loads the data from the file, numpy array, etc and generates batches for iterating >>> path_to_training = AMPLIGRAPH_DATA_HOME + 'fb15k-237/train.txt' >>> dataset_loader = GraphDataLoader(path_to_training, >>> backend=SQLiteAdapter, # type of backend to use >>> batch_size=1000, # batch size to use while iterating over this dataset >>> dataset_type='train', # dataset type >>> use_filter=False, # Whether to use filter or not >>> use_indexer=True) # indicates that the data needs to be mapped to index >>> >>> # Choose the partitioner - in this case we choose RandomEdges partitioner >>> partitioner = BucketGraphPartitioner(dataset_loader, k=3) >>> partitioned_model = ScoringBasedEmbeddingModel(eta=2, >>> k=50, >>> scoring_type='DistMult') >>> partitioned_model.compile(optimizer='adam', loss='multiclass_nll') >>> partitioned_model.fit(partitioner, # pass the partitioner object as input to the fit function this will generate data for the model during training >>> epochs=10) # number of epochs >>> indexer = partitioned_model.data_handler.get_mapper() # get the mapper from the trained model >>> path_to_test = AMPLIGRAPH_DATA_HOME + 'fb15k-237/test.txt' >>> dataset_loader_test = GraphDataLoader(path_to_test, >>> backend=SQLiteAdapter, # type of backend to use >>> batch_size=400, # batch size to use while iterating over this dataset >>> dataset_type='test', # dataset type >>> use_indexer=indexer # mapper to map test concepts to the same indices used during training >>> ) >>> ranks = partitioned_model.evaluate(dataset_loader_test, # pass the dataloader object to generate data for the model during training >>> batch_size=400) >>> print(ranks) [[ 85 7] [ 95 9] [1074 22] ... [ 546 95] [9961 7485] [1494 2]] ``` -------------------------------- ### GET /discover_facts Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.discovery.discover_facts.html This endpoint is used to discover new facts within a knowledge graph using the Ampligraph framework. ```APIDOC ## GET /discover_facts ### Description Discovers new facts from the knowledge graph based on the provided model and input data. ### Method GET ### Endpoint /discover_facts ``` -------------------------------- ### Fit the model Source: https://docs.ampligraph.org/en/latest/tutorials/AmpliGraphBasicsTutorial.html Initiates the training process for the model with specified batch size and epoch count. ```python model.fit(X_train, batch_size=5000, epochs=200, verbose=True) ``` -------------------------------- ### Model Restoration Status Source: https://docs.ampligraph.org/en/latest/tutorials/AmpliGraphBasicsTutorial.html Example output indicating that the saved model does not include a database file and skipping its restoration. ```text Saved model does not include a db file. Skipping. 2023-02-09 13:15:34.968306: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled. 2023-02-09 13:15:34.990256: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:114] Plugin optimizer for device_type GPU is enabled. ``` -------------------------------- ### Method compile Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.latent_features.ScoringBasedEmbeddingModel.html Configures the model for training by specifying the optimizer, loss function, and initializers. ```APIDOC ## compile(optimizer='adam', loss=None, entity_relation_initializer='glorot_uniform', entity_relation_regularizer=None, **kwargs) ### Description Compile the model with specified optimizer and loss function. ### Parameters #### Request Body - **optimizer** (str or optimizer instance) - Optional - The optimizer used to minimize the loss function (e.g., 'sgd', 'adagrad', 'adam', 'rmsprop'). - **loss** (str, objective function, or Loss) - Optional - The objective function used for training (e.g., 'pairwise', 'nll', 'absolute_margin', 'self_adversarial', 'multiclass_nll'). - **entity_relation_initializer** (str) - Optional - Initializer for entities and relations. - **entity_relation_regularizer** (str) - Optional - Regularizer for entities and relations. ``` -------------------------------- ### CSV file format example Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.datasets.load_from_csv.html Visual representation of the expected CSV file structure when using a tab separator. ```text subj1 relationX obj1 subj1 relationY obj2 subj3 relationZ obj2 subj4 relationY obj2 ... ``` -------------------------------- ### TensorBoard output directory structure Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.utils.create_tensorboard_visualizations.html Shows the expected file structure generated by the visualization function. ```text tensorboard_files/ ├── checkpoint ├── embeddings_projector.tsv ├── graph_embedding.ckpt.data-00000-of-00001 ├── graph_embedding.ckpt.index ├── graph_embedding.ckpt.meta ├── metadata.tsv └── projector_config.pbtxt ``` -------------------------------- ### Get Entity and Relation Counts Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.latent_features.ScoringBasedEmbeddingModel.html Retrieves the count of entities and relations present in the trained model. Use 'e' for entities and 'r' for relations. ```python print('Entities:', model.get_count('e')) print('Relations:', model.get_count('r')) ``` -------------------------------- ### Train a model using BucketGraphPartitioner Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.datasets.BucketGraphPartitioner.html Demonstrates initializing a GraphDataLoader with a SQLite backend and using BucketGraphPartitioner to train a ScoringBasedEmbeddingModel. ```python >>> from ampligraph.datasets import load_fb15k_237, GraphDataLoader, BucketGraphPartitioner >>> from ampligraph.datasets.sqlite_adapter import SQLiteAdapter >>> from ampligraph.latent_features import ScoringBasedEmbeddingModel >>> dataset = load_fb15k_237() >>> dataset_loader = GraphDataLoader(dataset['train'], >>> backend=SQLiteAdapter, # Type of backend to use >>> batch_size=1000, # Batch size to use while iterating over the dataset >>> dataset_type='train', # Dataset type >>> use_filter=False, # Whether to use filter or not >>> use_indexer=True) # indicates that the data needs to be mapped to index >>> partitioner = BucketGraphPartitioner(dataset_loader, k=2) >>> # create and compile a model as usual >>> partitioned_model = ScoringBasedEmbeddingModel(eta=2, k=50, scoring_type='DistMult') >>> partitioned_model.compile(optimizer='adam', loss='multiclass_nll') >>> partitioned_model.fit(partitioner, # The partitioner object generate data for the model during training >>> epochs=10) # Number of epochs ``` -------------------------------- ### Perform Model Selection with select_best_model_ranking Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.evaluation.select_best_model_ranking.html Demonstrates how to configure a hyperparameter grid and execute the model selection process on the WN18 dataset. ```python >>> from ampligraph.datasets import load_wn18 >>> from ampligraph.evaluation import select_best_model_ranking >>> import numpy as np >>> >>> X = load_wn18() >>> model_class = 'ComplEx' >>> param_grid = { >>> "batch_size": [1000], >>> "seed": 0, >>> "epochs": [4000], >>> "k": [100, 200], >>> "eta": [5,10,15], >>> "loss": ["pairwise", "nll"], >>> "loss_params": { >>> "margin": [2] >>> }, >>> "regularizer": ["LP", None], >>> "regularizer_params": { >>> "p": [1, 3], >>> "lambda": [1e-4, 1e-5] >>> }, >>> "optimizer": ["adagrad", "adam"], >>> "optimizer_params":{ >>> "lr": lambda: np.random.uniform(0.0001, 0.01) >>> }, >>> "verbose": False >>> } >>> select_best_model_ranking(model_class, X['train'], X['valid'], X['test'], param_grid, >>> max_combinations=20, use_filter=True, verbose=True, >>> early_stopping=True) ``` -------------------------------- ### Model Saving Warning Source: https://docs.ampligraph.org/en/latest/tutorials/AmpliGraphBasicsTutorial.html Example of a warning message that may appear during model saving, indicating that some functions might not be directly callable after restoration. ```text WARNING - Found untraced functions such as _get_ranks while saving (showing 1 of 1). These functions will not be directly callable after loading. ``` -------------------------------- ### Import Tensorboard Visualization Utility Source: https://docs.ampligraph.org/en/latest/tutorials/AmpliGraphBasicsTutorial.html Import the necessary function to prepare embedding files for Tensorboard. ```python from ampligraph.utils import create_tensorboard_visualizations ``` -------------------------------- ### Duplicate Movies Output Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.discovery.find_duplicates.html Example output displaying a list of potential duplicate movies identified by the discovery function, showing their names and years. ```text movie_name year 5198 Duel to Death 1983 5199 Duel to Death 1983 2649 The Eliminator 2004 2650 The Eliminator 2004 3967 Lipstick Camera 1994 3968 Lipstick Camera 1994 ``` -------------------------------- ### Loss Class Initialization Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.latent_features.loss_functions.Loss.html Details on how to initialize the abstract Loss class, including available hyperparameters. ```APIDOC ## Loss Class ### Description Abstract class for the loss function. ### Methods #### `__init__`(_hyperparam_dict={}_, _verbose=False_) Initialize the loss. ### Parameters #### Request Body - **hyperparam_dict** (dict) - Dictionary of hyperparams. - **reduction** (str) - Specifies whether to "sum" or take the "mean" of loss per sample w.r.t. corruptions (default: "sum"). Other Keys are described in the hyperparameters section. ``` -------------------------------- ### Load CoDEx-M Dataset Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.datasets.load_codex.html Demonstrates loading the CoDEx-M dataset and accessing specific splits, as well as retrieving human-readable labels using the mapper option. ```python >>> from ampligraph.datasets import load_codex >>> X = load_codex() >>> X["valid"][0] array(['Q60684', 'P106', 'Q4964182'], dtype=object) >>> X = load_codex(return_mapper=True) >>> [X['mapper'][elem]['label'] for elem in X['valid'][0]] ['Novalis', 'occupation', 'philosopher'] ``` -------------------------------- ### Import Libraries for Embedding Visualization Source: https://docs.ampligraph.org/en/latest/tutorials/ClusteringAndClassificationWithEmbeddings.html Imports necessary libraries for PCA, plotting, clustering, and text adjustment. Ensure seaborn, adjustText, and incf.countryutils are installed. ```python from sklearn.decomposition import PCA import matplotlib.pyplot as plt import seaborn as sns from adjustText import adjust_text from incf.countryutils import transformations %matplotlib inline ``` -------------------------------- ### Build AmpliGraph Custom Wheel Source: https://docs.ampligraph.org/en/latest/dev.html Create a custom wheel package for AmpliGraph without its dependencies using pip wheel. The output will be placed in the dist directory. ```bash pip wheel --wheel-dir dist --no-deps . ``` -------------------------------- ### Load Dataset from CSV Source: https://docs.ampligraph.org/en/latest/tutorials/AmpliGraphBasicsTutorial.html Download and load a knowledge graph dataset from a CSV file into memory. ```python import requests from ampligraph.datasets import load_from_csv url = 'https://ampligraph.s3-eu-west-1.amazonaws.com/datasets/GoT.csv' open('GoT.csv', 'wb').write(requests.get(url).content) X = load_from_csv('.', 'GoT.csv', sep=',') X[:5, ] ``` ```text array([['Smithyton', 'SEAT_OF', 'House Shermer of Smithyton'], ['House Mormont of Bear Island', 'LED_BY', 'Maege Mormont'], ['Margaery Tyrell', 'SPOUSE', 'Joffrey Baratheon'], ['Maron Nymeros Martell', 'ALLIED_WITH', 'House Nymeros Martell of Sunspear'], ['House Gargalen of Salt Shore', 'IN_REGION', 'Dorne']], dtype=object) ``` -------------------------------- ### Get Embeddings for Specific Entities Source: https://docs.ampligraph.org/en/latest/generated/ampligraph.latent_features.ScoringBasedEmbeddingModel.html Retrieves the k-dimensional embeddings for a given list of entities or relations. Ensure entities are provided as string literals, not internal IDs. ```python from ampligraph.latent_features import ScoringBasedEmbeddingModel from ampligraph.datasets import load_fb15k_237 X = load_fb15k_237() model = ScoringBasedEmbeddingModel(eta=5, k=300, scoring_type='ComplEx', seed=0) model.compile(optimizer='adam', loss='nll') model.fit(X['train'], batch_size=10000, epochs=5, verbose=False) model.get_embeddings(['/m/027rn', '/m/06v8s0'], 'e') ```