TorchKGE Documentation Release 0.16.25

Armand Boschin

Jun 02, 2021

Tutorials:

1 TorchKGE 1

Index 49

i ii CHAPTER 1

TorchKGE

TorchKGE: Knowledge in Python and Pytorch. TorchKGE is a Python module for knowledge graph (KG) embedding relying solely on Pytorch. This package provides researchers and engineers with a clean and efficient API to design and test new models. It features a KG data structure, simple model interfaces and modules for negative sampling and model evaluation. Its main strength is a highly efficient evaluation module for the link prediction task, a central application of KG embedding. It has been observed to be up to five times faster than AmpliGraph and twenty-four times faster than OpenKE. Various KG embedding models are also already implemented. Special attention has been paid to code efficiency and simplicity, documentation and API consistency. It is distributed using PyPI under BSD license.

1.1 Citations

If you find this code useful in your research, please consider citing our paper (presented at IWKG-KDD 2020): • Free software: BSD license • Documentation: https://torchkge.readthedocs.io.

1 TorchKGE Documentation, Release 0.16.25

1.1.1 Model Training

Here are two examples of models being trained on FB15k.

Simplest training

This is the python code to train TransE without any wrapper. This script shows how all parts of TorchKGE should be used together: from import cuda from torch.optim import Adam from torchkge.models import TransEModel from torchkge.sampling import BernoulliNegativeSampler from torchkge.utils import MarginLoss, DataLoader from torchkge.utils.datasets import load_fb15k from tqdm.autonotebook import tqdm

# Load dataset kg_train, _, _= load_fb15k()

# Define some hyper-parameters for training emb_dim= 100 lr= 0.0004 n_epochs= 1000 b_size= 32768 margin= 0.5

# Define the model and criterion model= TransEModel(emb_dim, kg_train.n_ent, kg_train.n_rel, dissimilarity_type='L2') criterion= MarginLoss(margin)

# Move everything to CUDA if available if cuda.is_available(): cuda.empty_cache() model.cuda() criterion.cuda()

# Define the torch optimizer to be used optimizer= Adam(model.parameters(), lr=lr, weight_decay=1e-5) sampler= BernoulliNegativeSampler(kg_train) dataloader= DataLoader(kg_train, batch_size=b_size, use_cuda='all') iterator= tqdm(range(n_epochs), unit='epoch') for epoch in iterator: running_loss= 0.0 for i, batch in enumerate(dataloader): h, t, = batch[0], batch[1], batch[2] n_h, n_t= sampler.corrupt_batch(h, t, r)

optimizer.zero_grad()

# forward + backward + optimize pos, neg= model(h, t, n_h, n_t, r) loss= criterion(pos, neg) (continues on next page)

2 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

(continued from previous page) loss.backward() optimizer.step()

running_loss+= loss.item() iterator.set_description( 'Epoch {} | mean loss: {:.5f}'.format(epoch+1, running_loss/ len(dataloader))) model.normalize_parameters()

Shortest training

TorchKGE also provides simple utility wrappers for model training. Here is an example on how to use them: from torch.optim import Adam from torchkge.evaluation import LinkPredictionEvaluator from torchkge.models import TransEModel from torchkge.utils.datasets import load_fb15k from torchkge.utils import Trainer, MarginLoss def main(): # Define some hyper-parameters for training emb_dim= 100 lr= 0.0004 margin= 0.5 n_epochs= 1000 batch_size= 32768

# Load dataset kg_train, kg_val, kg_test= load_fb15k()

# Define the model and criterion model= TransEModel(emb_dim, kg_train.n_ent, kg_train.n_rel, dissimilarity_type='L2') criterion= MarginLoss(margin) optimizer= Adam(model.parameters(), lr=lr, weight_decay=1e-5)

trainer= Trainer(model, criterion, kg_train, n_epochs, batch_size, optimizer=optimizer, sampling_type='bern', use_cuda='all',)

trainer.run()

evaluator= LinkPredictionEvaluator(model, kg_test) evaluator.evaluate(200) evaluator.print_results() if __name__ =="__main__": main()

1.1. Citations 3 TorchKGE Documentation, Release 0.16.25

Training with Ignite

TorchKGE can be used along with the PyTorch ignite library. It makes it easy to include early stopping in the training process. Here is an example script of training a TransE model on FB15k on GPU with early stopping on evaluation MRR: import torch from ignite.engine import Engine, Events from ignite.handlers import EarlyStopping from ignite.metrics import RunningAverage from torch.optim import Adam from torchkge.evaluation import LinkPredictionEvaluator from torchkge.models import TransEModel from torchkge.sampling import BernoulliNegativeSampler from torchkge.utils import MarginLoss, DataLoader from torchkge.utils.datasets import load_fb15k def process_batch(engine, batch): h, t, r= batch[0], batch[1], batch[2] n_h, n_t= sampler.corrupt_batch(h, t, r)

optimizer.zero_grad()

pos, neg= model(h, t, n_h, n_t, r) loss= criterion(pos, neg) loss.backward() optimizer.step()

return loss.item() def linkprediction_evaluation(engine): model.normalize_parameters()

loss= engine.state.output

# validation MRR measure if engine.state.epoch% eval_epoch ==0: evaluator= LinkPredictionEvaluator(model, kg_val) evaluator.evaluate(b_size=256, verbose=False) val_mrr= evaluator.mrr()[1] else: val_mrr=0

print('Epoch {} | Train loss: {}, Validation MRR: {}'.format( engine.state.epoch, loss, val_mrr))

try: if engine.state.best_mrr< val_mrr: engine.state.best_mrr= val_mrr return val_mrr

except AttributeError as e: if engine.state.epoch ==1: engine.state.best_mrr= val_mrr return val_mrr (continues on next page)

4 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

(continued from previous page) else: raise e device= torch.device('cuda') eval_epoch= 20 # do link prediction evaluation each 20 epochs max_epochs= 1000 patience= 40 batch_size= 32768 emb_dim= 100 lr= 0.0004 margin= 0.5 kg_train, kg_val, kg_test= load_fb15k()

# Define the model, optimizer and criterion model= TransEModel(emb_dim, kg_train.n_ent, kg_train.n_rel, dissimilarity_type='L2') model.to(device) optimizer= Adam(model.parameters(), lr=lr, weight_decay=1e-5) criterion= MarginLoss(margin) sampler= BernoulliNegativeSampler(kg_train, kg_val=kg_val, kg_test=kg_test)

# Define the engine trainer= Engine(process_batch)

# Define the moving average RunningAverage(output_transform=lambda x: x).attach(trainer,'margin')

# Add early stopping handler= EarlyStopping(patience=patience, score_function=linkprediction_evaluation, trainer=trainer) trainer.add_event_handler(Events.EPOCH_COMPLETED, handler)

# Training train_iterator= DataLoader(kg_train, batch_size, use_cuda='all') trainer.run(train_iterator, epoch_length=len(train_iterator), max_epochs=max_epochs) print('Best score {:.3f} at epoch {}'.format(handler.best_score, trainer.state.epoch- handler.patience))

1.1.2 Model Evaluation

Link Prediction

To evaluate a model on link prediction: from torch import cuda from torchkge.utils.pretrained_models import load_pretrained_transe from torchkge.utils.datasets import load_fb15k from torchkge.evaluation import LinkPredictionEvaluator (continues on next page)

1.1. Citations 5 TorchKGE Documentation, Release 0.16.25

(continued from previous page)

_, _, kg_test= load_fb15k() model= load_pretrained_transe('fb15k', 100) if cuda.is_available(): model.cuda()

# Link prediction evaluation on test set. evaluator= LinkPredictionEvaluator(model, kg_test) evaluator.evaluate(b_size=32) evaluator.print_results()

Triplet Classification

To evaluate a model on triplet classification: from torch import cuda from torchkge.evaluation import TripletClassificationEvaluator from torchkge.utils.pretrained_models import load_pretrained_transe from torchkge.utils.datasets import load_fb15k

_, kg_val, kg_test= load_fb15k() model= load_pretrained_transe('fb15k', 100): if cuda.is_available(): model.cuda()

# Triplet classification evaluation on test set by learning thresholds on validation

˓→set evaluator= TripletClassificationEvaluator(model, kg_val, kg_test) evaluator.evaluate(b_size=128) print('Accuracy on test set: {}'.format(evaluator.accuracy(b_size=128)))

1.1.3 Models

Interfaces

Model class torchkge.models.interfaces.Model(n_entities, n_relations) Model interface to be used by any other class implementing a knowledge graph embedding model. It is only required to implement the methods scoring_function, normalize_parameters, lp_prep_cands and lp_scoring_function. Parameters • n_entities (int) – Number of entities to be embedded. • n_relations (int) – Number of relations to be embedded. n_ent Number of entities to be embedded. Type int

6 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

n_rel Number of relations to be embedded. Type int forward(heads, tails, negative_heads, negative_tails, relations) Parameters • heads (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Integer keys of the current batch’s heads • tails (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Integer keys of the current batch’s tails. • negative_heads (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Integer keys of the current batch’s negatively sampled heads. • negative_tails (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Integer keys of the current batch’s negatively sampled tails. • relations (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Integer keys of the current batch’s relations. Returns • positive_triplets (torch.Tensor, dtype: torch.float, shape: (b_size)) – Scoring function evaluated on true triples. • negative_triplets (torch.Tensor, dtype: torch.float, shape: (b_size)) – Scoring function evaluated on negatively sampled triples. get_embeddings() Return the tensors representing entities and relations in current model. lp_compute_ranks(e_emb, candidates, r, e_idx, r_idx, true_idx, dictionary, heads=1) Link prediction evaluation helper function. Compute the ranks and the filtered ranks of true entities when doing link prediction. Note that the best rank possible is 1. Parameters • e_emb (torch.Tensor, shape: (b_size, rel_emb_dim), dtype: torch.float) – Embeddings of current entities ready for lp_scoring_function. • candidates (torch.Tensor, shape: (b_size, n_ent, emb_dim), dtype:) – torch.float Embeddings of all entities ready for lp_scoring_function. • r (torch.Tensor, shape: (b_size, emb_dim, emb_dim) or (b_size,) – emb_dim), dtype: torch.float Embeddings or matrices of current relations ready for lp_scoring_function. • e_idx (torch.Tensor, shape: (b_size), dtype: torch.long)– List of entities indices. • r_idx (torch.Tensor, shape: (b_size), dtype: torch.long)– List of relations indices. • true_idx (torch.Tensor, shape: (b_size), dtype: torch.long) – List of the indices of the true entity for each sample. • dictionary (defaultdict) – Dictionary of keys (int, int) and values list of ints giving all possible entities for the (entity, relation) pair.

1.1. Citations 7 TorchKGE Documentation, Release 0.16.25

• heads (integer) – 1 ou -1 (must be 1 if entities are heads and -1 if entities are tails). The computed score is either 푓푟(푒, 푐푎푛푑푖푑푎푡푒) (if heads is 1) or 푓푟(푐푎푛푑푖푑푎푡푒, 푒) (if heads is -1). Returns • rank_true_entities (torch.Tensor, shape: (b_size), dtype: torch.int) – List of the ranks of true entities when all candidates are sorted by decreasing order of scoring function. • filt_rank_true_entities (torch.Tensor, shape: (b_size), dtype:) – torch.int List of the ranks of true entities when only candidates which are not known to lead to a true fact are sorted by decreasing order of scoring function. lp_helper(h_idx, t_idx, r_idx, kg) Link prediction evaluation helper function. Compute the head and tail ranks and filtered ranks of the current batch. Parameters • h_idx (torch.Tensor, shape: (b_size), dtype: torch.long)– List of heads indices. • t_idx (torch.Tensor, shape: (b_size), dtype: torch.long)– List of tails indices. • r_idx (torch.Tensor, shape: (b_size), dtype: torch.long)– List of relations indices. • kg (torchkge.data_structures.KnowledgeGraph) – Knowledge graph on which the model was trained. This is used to access the dict_of_heads and dict_of_tails attributes in order to compute the filtered metrics. Returns • rank_true_tails (torch.Tensor, shape: (b_size), dtype: torch.int) – List of the ranks of true tails when all candidates are sorted by decreasing order of scoring function. • filt_rank_true_tails (torch.Tensor, shape: (b_size), dtype: torch.int) – List of the filtered ranks of true tails when candidates are sorted by decreasing order of scoring function. • rank_true_heads (torch.Tensor, shape: (b_size), dtype: torch.int) – List of the ranks of true heads when all candidates are sorted by decreasing order of scoring function. • filt_rank_true_heads (torch.Tensor, shape: (b_size), dtype: torch.int) – List of the filtered ranks of true heads when candidates are sorted by decreasing order of scoring function. lp_prep_cands(h_idx, t_idx, r_idx) Link prediction evaluation helper function. Get entities and relations embeddings, along with entity can- didates ready for (projected if needed). The output will be fed to the lp_scoring_function method of the model at hand. Parameters • h_idx (torch.Tensor, shape: (b_size), dtype: torch.long)– List of heads indices. • t_idx (torch.Tensor, shape: (b_size), dtype: torch.long)– List of tails indices. • r_idx (torch.Tensor, shape: (b_size), dtype: torch.long)– List of relations indices. Returns

8 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

• h (torch.Tensor, shape: (b_size, rel_emb_dim), dtype: torch.float) – Head vectors fed to lp_scoring_function. For translation models it is the entities embeddings projected in re- lation space, for example. • t (torch.Tensor, shape: (b_size, rel_emb_dim), dtype: torch.float) – Tail vectors fed to lp_scoring_function. For translation models it is the entities embeddings projected in re- lation space, for example. • candidates (torch.Tensor, shape: (b_size, rel_emb_dim, n_ent),) – dtype: torch.float All entities embeddings prepared from batch evaluation. Axis 0 is simply duplication. • r (torch.Tensor, shape: (b_size, rel_emb_dim), dtype: torch.float) – Relations embeddings or matrices. lp_scoring_function(h, t, r) Link prediction evaluation helper function. Compute the scores of (h, r, c) or (c, r, t) for any candidate c. The arguments should match the ones of lp_prep_cands. Parameters • h (torch.Tensor, shape: (b_size, ent_emb_dim) or (b_size, n_ent,) – ent_emb_dim), dtype: torch.float • t (torch.Tensor, shape: (b_size, ent_emb_dim) or (b_size, n_ent,) – ent_emb_dim), dtype: torch.float • r (torch.Tensor, shape: (b_size, ent_emb_dim), dtype: torch.float)– Returns scores – Scores of each candidate for each triple. Return type torch.Tensor, shape: (b_size, n_ent), dtype: torch.float normalize_parameters() Normalize some parameters. This methods should be end at the end of each training epoch and at the end of training as well. scoring_function(h_idx, t_idx, r_idx) Compute the scoring function for the triplets given as argument. Parameters • h_idx (torch.Tensor, dtype: torch.long, shape: (b_size))– Integer keys of the current batch’s heads • t_idx (torch.Tensor, dtype: torch.long, shape: (b_size))– Integer keys of the current batch’s tails. • r_idx (torch.Tensor, dtype: torch.long, shape: (b_size))– Integer keys of the current batch’s relations. Returns score – Score of each triplet. Return type torch.Tensor, dtype: torch.float, shape: (b_size)

TranslationalModels class torchkge.models.interfaces.TranslationModel(n_entities, n_relations, dissimilar- ity_type) Model interface to be used by any other class implementing a translation knowledge graph embedding model. This interface inherits from the interface torchkge.models.interfaces.Model. It is only required to implement the methods scoring_function, normalize_parameters and lp_prep_cands.

1.1. Citations 9 TorchKGE Documentation, Release 0.16.25

Parameters • n_entities (int) – Number of entities to be embedded. • n_relations (int) – Number of relations to be embedded. • dissimilarity_type (str) – One of ‘L1’, ‘L2’, ‘toruse_L1’, ‘toruse_L2’ and ‘toruse_eL2’. dissimilarity Dissimilarity function. Type function get_embeddings() See torchkge.models.interfaces.Models. lp_prep_cands(h_idx, t_idx, r_idx) See torchkge.models.interfaces.Models. lp_scoring_function(proj_h, proj_t, r) This overwrites the method declared in torchkge.models.interfaces.Models. For translation models, the computed score is the dissimilarity of between projected heads + relations and projected tails. Projections are done in relation-specific subspaces. normalize_parameters() See torchkge.models.interfaces.Models. scoring_function(h_idx, t_idx, r_idx) See torchkge.models.interfaces.Models.

Translational Models

Parameters used to train models available in pre-trained version :

Dataset Dimension Optimizer Learning Rate Batch Size Loss Margin L2 penalization TransE FB15k 100 Adam 2.1e-5 32768 Margin .651 1e-5 TransE FB15k237 100 Adam 2.1e-5 32768 Margin .651 1e-5 TransE FB15k237 150 Adam 2.7e-5 32768 Margin .648 1e-5

TransE class torchkge.models.translation.TransEModel(emb_dim, n_entities, n_relations, dissimi- larity_type=’L2’) Implementation of TransE model detailed in 2013 paper by Bordes et al.. This class inherits from the torchkge.models.interfaces.TranslationModel interface. It then has its attributes as well.

References

• Antoine Bordes, Nicolas Usunier, Alberto Garcia-Duran, Jason Weston, and Oksana Yakhnenko. Trans- lating Embeddings for Modeling Multi-relational Data. In Advances in Neural Information Processing Systems 26, pages 2787–2795, 2013.

Parameters • emb_dim (int) – Dimension of the embedding.

10 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

• n_ent (int) – Number of entities in the current data set. • n_rel (int) – Number of relations in the current data set. • dissimilarity_type (str) – Either ‘L1’ or ‘L2’.

emb_dim Dimension of the embedding. Type int ent_emb Embeddings of the entities, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_ent, emb_dim) rel_emb Embeddings of the relations, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_rel, emb_dim) get_embeddings() Return the embeddings of entities and relations. Returns • ent_emb (torch.Tensor, shape: (n_ent, emb_dim), dtype: torch.float) – Embeddings of entities. • rel_emb (torch.Tensor, shape: (n_rel, emb_dim), dtype: torch.float) – Embeddings of relations. lp_prep_cands(h_idx, t_idx, r_idx) Link prediction evaluation helper function. Get entities embeddings and relations embeddings. The output will be fed to the lp_scoring_function method. See torchkge.models.interfaces.Models for more details on the API. normalize_parameters() Normalize the entity embeddings, as explained in original paper. This methods should be called at the end of each training epoch and at the end of training as well. scoring_function(h_idx, t_idx, r_idx) 푝 Compute the scoring function for the triplets given as argument: ||ℎ + 푟 − 푡||푝 with p being the dissimilarity type (either 1 or 2). See referenced paper for more details on the score. See torchkge.models.interfaces.Models for more details on the API.

TransH class torchkge.models.translation.TransHModel(emb_dim, n_entities, n_relations) Implementation of TransH model detailed in 2014 paper by Wang et al.. This class inherits from the torchkge.models.interfaces.TranslationModel interface. It then has its attributes as well.

References

• Zhen Wang, Jianwen Zhang, Jianlin Feng, and Zheng Chen. Knowledge Graph Embedding by Translating on Hyperplanes. In Twenty-Eighth AAAI Conference on Artificial Intelligence, June 2014.

Parameters • emb_dim (int) – Dimension of the embedding space.

1.1. Citations 11 TorchKGE Documentation, Release 0.16.25

• n_ent (int) – Number of entities in the current data set. • n_rel (int) – Number of relations in the current data set.

emb_dim Dimension of the embedding. Type int ent_emb Embeddings of the entities, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_ent, emb_dim) rel_emb Embeddings of the relations, initialized with Xavier uniform distribution. Type torch.nn.Embedding, shape: (n_rel, emb_dim) norm_vect Normal vectors associated to each relation and used to compute the relation-specific hyperplanes enti- ties are projected on. See paper for more details. Initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_rel, emb_dim) get_embeddings() Return the embeddings of entities and relations along with relation normal vectors. Returns • ent_emb (torch.Tensor, shape: (n_ent, emb_dim), dtype: torch.float) – Embeddings of entities. • rel_emb (torch.Tensor, shape: (n_rel, emb_dim), dtype: torch.float) – Embeddings of relations. • norm_vect (torch.Tensor, shape: (n_rel, emb_dim), dtype: torch.float) – Normal vectors defining relation-specific hyperplanes. lp_evaluate_projections() Link prediction evaluation helper function. Project all entities according to each relation. Calling this method at the beginning of link prediction makes the process faster by computing projections only once. lp_prep_cands(h_idx, t_idx, r_idx) Link prediction evaluation helper function. Get entities embeddings and relations embeddings. The output will be fed to the lp_scoring_function method. See torchkge.models.interfaces.Models for more details on the API. normalize_parameters() Normalize the entity embeddings and relations normal vectors, as explained in original paper. This meth- ods should be called at the end of each training epoch and at the end of training as well. scoring_function(h_idx, t_idx, r_idx) 2 Compute the scoring function for the triplets given as argument: ||푝푟(ℎ) + 푟 − 푝푟(푡)||2. See referenced paper for more details on the score. See torchkge.models.interfaces.Models for more details on the API.

TransR class torchkge.models.translation.TransRModel(ent_emb_dim, rel_emb_dim, n_entities, n_relations) Implementation of TransR model detailed in 2015 paper by Lin et al.. This class inherits from the torchkge.

12 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

models.interfaces.TranslationModel interface. It then has its attributes as well.

References

• Yankai Lin, Zhiyuan Liu, Maosong Sun, Yang Liu, and Xuan Zhu. Learning Entity and Relation Embed- dings for Knowledge Graph Completion. In Twenty-Ninth AAAI Conference on Artificial Intelligence, February 2015

Parameters • ent_emb_dim (int) – Dimension of the embedding of entities. • rel_emb_dim (int) – Dimension of the embedding of relations. • n_ent (int) – Number of entities in the current data set. • n_rel (int) – Number of relations in the current data set.

ent_emb_dim Dimension nof the embedding of entities. Type int rel_emb_dim Dimension of the embedding of relations. Type int ent_emb Embeddings of the entities, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_ent, ent_emb_dim) rel_emb Embeddings of the relations, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_rel, rel_emb_dim) proj_mat Relation-specific projection matrices. See paper for more details. Type torch.nn.Embedding, shape: (n_rel, rel_emb_dim x ent_emb_dim) projected_entities Contains the projection of each entity in each relation-specific sub-space. Type torch.nn.Parameter, shape: (n_rel, n_ent, rel_emb_dim) evaluated_projections Indicates whether projected_entities has been computed. This should be set to true every time a backward pass is done in train mode. Type bool get_embeddings() Return the embeddings of entities and relations along with their projection matrices. Returns • ent_emb (torch.Tensor, shape: (n_ent, ent_emb_dim), dtype: torch.float) – Embeddings of entities. • rel_emb (torch.Tensor, shape: (n_rel, rel_emb_dim), dtype: torch.float) – Embeddings of relations.

1.1. Citations 13 TorchKGE Documentation, Release 0.16.25

• proj_mat (torch.Tensor, shape: (n_rel, rel_emb_dim, ent_emb_dim),) • dtype (torch.float) – Relation-specific projection matrices. lp_evaluate_projections() Link prediction evaluation helper function. Project all entities according to each relation. Calling this method at the beginning of link prediction makes the process faster by computing projections only once. lp_prep_cands(h_idx, t_idx, r_idx) Link prediction evaluation helper function. Get entities embeddings and relations embeddings. The output will be fed to the lp_scoring_function method. See torchkge.models.interfaces.Models for more details on the API. normalize_parameters() Normalize the entity and relation embeddings, as explained in original paper. This methods should be called at the end of each training epoch and at the end of training as well. scoring_function(h_idx, t_idx, r_idx) 2 Compute the scoring function for the triplets given as argument: ||푝푟(ℎ) + 푟 − 푝푟(푡)||2. See referenced paper for more details on the score. See torchkge.models.interfaces.Models for more details on the API.

TransD class torchkge.models.translation.TransDModel(ent_emb_dim, rel_emb_dim, n_entities, n_relations) Implementation of TransD model detailed in 2015 paper by Ji et al.. This class inherits from the torchkge. models.interfaces.TranslationModel interface. It then has its attributes as well.

References

• Guoliang Ji, Shizhu He, Liheng Xu, Kang Liu, and Jun Zhao. Knowledge Graph Embedding via Dy- namic Mapping Matrix. In Proceedings of the 53rd Annual Meeting of the Association for Computational Linguistics and the 7th International Joint Conference on Natural Language Processing (Volume 1: Long Papers) pages 687–696, Beijing, China, July 2015. Association for Computational Linguistics.

Parameters • ent_emb_dim (int) – Dimension of the embedding of entities. • rel_emb_dim (int) – Dimension of the embedding of relations. • n_ent (int) – Number of entities in the current data set. • n_rel (int) – Number of relations in the current data set.

ent_emb_dim Dimension of the embedding of entities. Type int rel_emb_dim Dimension of the embedding of relations. Type int ent_emb Embeddings of the entities, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_ent, ent_emb_dim)

14 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

rel_emb Embeddings of the relations, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_rel, rel_emb_dim) ent_proj_vect Entity-specific vector used to build projection matrices. See paper for more details. Initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_ent, ent_emb_dim) rel_proj_vect Relation-specific vector used to build projection matrices. See paper for more details. Initialized with Xavier uniform distribution and then normalized. Type torch..nn.Embedding, shape: (n_rel, rel_emb_dim) projected_entities Contains the projection of each entity in each relation-specific sub-space. Type torch.nn.Parameter, shape: (n_rel, n_ent, rel_emb_dim) evaluated_projections Indicates whether projected_entities has been computed. This should be set to true every time a backward pass is done in train mode. Type bool get_embeddings() Return the embeddings of entities and relations along with their projection vectors. Returns • ent_emb (torch.Tensor, shape: (n_ent, ent_emb_dim), dtype: torch.float) – Embeddings of entities. • rel_emb (torch.Tensor, shape: (n_rel, rel_emb_dim), dtype: torch.float) – Embeddings of relations. • ent_proj_vect (torch.Tensor, shape: (n_ent, ent_emb_dim),) • dtype (torch.float) – Entity projection vectors. • rel_proj_vect (torch.Tensor, shape: (n_ent, rel_emb_dim),) • dtype (torch.float) – Relation projection vectors. lp_evaluate_projections() Link prediction evaluation helper function. Project all entities according to each relation. Calling this method at the beginning of link prediction makes the process faster by computing projections only once. lp_prep_cands(h_idx, t_idx, r_idx) Link prediction evaluation helper function. Get entities embeddings and relations embeddings. The output will be fed to the lp_scoring_function method. See torchkge.models.interfaces.Models for more details on the API. normalize_parameters() Normalize the entity embeddings and relations normal vectors, as explained in original paper. This meth- ods should be called at the end of each training epoch and at the end of training as well. project(ent, e_proj_vect, r_proj_vect) 푝푇 푝 We note that 푝푟(푒)푖 = 푒 푒 × 푟푖 + 푒푖 which is more efficient to compute than the matrix formulation in the original paper.

1.1. Citations 15 TorchKGE Documentation, Release 0.16.25

scoring_function(h_idx, t_idx, r_idx) 2 Compute the scoring function for the triplets given as argument: ||푝푟(ℎ) + 푟 − 푝푟(푡)||2. See referenced paper for more details on the score. See torchkge.models.interfaces.Models for more details on the API.

TorusE class torchkge.models.translation.TorusEModel(emb_dim, n_entities, n_relations, dissimi- larity_type) Implementation of TorusE model detailed in 2018 paper by Ebisu and Ichise. This class inherits from the torchkge.models.interfaces.TranslationModel interface. It then has its attributes as well.

References

• Takuma Ebisu and Ryutaro Ichise TorusE: Knowledge Graph Embedding on a Lie Group. In Proceedings of the 32nd AAAI Conference on Artificial Intelligence (New Orleans, LA, USA, Feb. 2018), AAAI Press, pp. 1819–1826.

Parameters • emb_dim (int) – Embedding dimension. • n_ent (int) – Number of entities in the current data set. • n_rel (int) – Number of relations in the current data set. • dissimilarity_type (str) – One of ‘torus_L1’, ‘torus_L2’, ‘torus_eL2’.

emb_dim Embedding dimension. Type int ent_emb Embeddings of the entities, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_ent, emb_dim) rel_emb Embeddings of the relations, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_rel, emb_dim) get_embeddings() Return the embeddings of entities and relations. Returns • ent_emb (torch.Tensor, shape: (n_ent, emb_dim), dtype: torch.float) – Embeddings of entities. • rel_emb (torch.Tensor, shape: (n_rel, emb_dim), dtype: torch.float) – Embeddings of relations. lp_prep_cands(h_idx, t_idx, r_idx) Link prediction evaluation helper function. Get entities embeddings and relations embeddings. The output will be fed to the lp_scoring_function method. See torchkge.models.interfaces.Models for more details on the API. normalize_parameters() Project embeddings on torus.

16 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

scoring_function(h_idx, t_idx, r_idx) Compute the scoring function for the triplets given as argument: See referenced paper for more details on the score. See torchkge.models.interfaces.Models for more details on the API.

Bilinear Models

RESCAL class torchkge.models.bilinear.RESCALModel(emb_dim, n_entities, n_relations) Implementation of RESCAL model detailed in 2011 paper by Nickel et al.. In the original paper, optimization is done using Alternating Least Squares (ALS). Here we use iterative gradient descent optimization. This class inherits from the torchkge.models.interfaces.BilinearModel interface. It then has its attributes as well.

References

• Maximilian Nickel, Volker Tresp, and Hans-Peter Kriegel. A Three-way Model for Collective Learning on Multi-relational Data. In Proceedings of the 28th International Conference on , 2011.

Parameters • emb_dim (int) – Dimension of embedding space. • n_entities (int) – Number of entities in the current data set. • n_relations (int) – Number of relations in the current data set.

ent_emb Embeddings of the entities, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_ent, emb_dim) rel_mat Matrices of the relations, initialized with Xavier uniform distribution. Type torch.nn.Embedding, shape: (n_rel, emb_dim x emb_dim) get_embeddings() Return the embeddings of entities and matrices of relations. Returns • ent_emb (torch.Tensor, shape: (n_ent, emb_dim), dtype: torch.float) – Embeddings of entities. • rel_mat (torch.Tensor, shape: (n_rel, emb_dim, emb_dim),) • dtype (torch.float) – Matrices of relations. lp_prep_cands(h_idx, t_idx, r_idx) Link prediction evaluation helper function. Get entities embeddings and relations embeddings. The output will be fed to the lp_scoring_function method. See torchkge.models.interfaces.Models for more details on the API. lp_scoring_function(h, t, r) Link prediction evaluation helper function. See torchkge.models.interfaces.Models for more details of the API.

1.1. Citations 17 TorchKGE Documentation, Release 0.16.25

normalize_parameters() Normalize the entity embeddings, as explained in original paper. This methods should be called at the end of each training epoch and at the end of training as well. scoring_function(h_idx, t_idx, r_idx) 푇 Compute the scoring function for the triplets given as argument: ℎ ·푀푟 ·푡. See referenced paper for more details on the score. See torchkge.models.interfaces.Models for more details on the API.

DistMult class torchkge.models.bilinear.DistMultModel(emb_dim, n_entities, n_relations) Implementation of DistMult model detailed in 2014 paper by Yang et al.. This class inherits from the torchkge.models.interfaces.BilinearModel interface. It then has its attributes as well.

References

• Bishan Yang, Wen-tau Yih, Xiaodong He, Jianfeng Gao, and Li Deng. Embedding Entities and Relations for Learning and Inference in Knowledge Bases. arXiv :1412.6575 [cs], December 2014.

Parameters • emb_dim (int) – Dimension of embedding space. • n_entities (int) – Number of entities in the current data set. • n_relations (int) – Number of relations in the current data set.

ent_emb Embeddings of the entities, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_ent, emb_dim) rel_emb Embeddings of the relations, initialized with Xavier uniform distribution. Type torch.nn.Embedding, shape: (n_rel, emb_dim) get_embeddings() Return the embeddings of entities and relations. Returns • ent_emb (torch.Tensor, shape: (n_ent, emb_dim), dtype: torch.float) – Embeddings of entities. • rel_emb (torch.Tensor, shape: (n_rel, emb_dim), dtype: torch.float) – Embeddings of relations. lp_prep_cands(h_idx, t_idx, r_idx) Link prediction evaluation helper function. Get entities embeddings and relations embeddings. The output will be fed to the lp_scoring_function method. See torchkge.models.interfaces.Models for more details on the API. lp_scoring_function(h, t, r) Link prediction evaluation helper function. See torchkge.models.interfaces.Models for more details on the API.

18 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

normalize_parameters() Normalize the entity embeddings, as explained in original paper. This methods should be called at the end of each training epoch and at the end of training as well. scoring_function(h_idx, t_idx, r_idx) Compute the scoring function for the triplets given as argument: ℎ푇 · 푑푖푎푔(푟) · 푡. See referenced paper for more details on the score. See torchkge.models.interfaces.Models for more details on the API.

HolE class torchkge.models.bilinear.HolEModel(emb_dim, n_entities, n_relations) Implementation of HolE model detailed in 2015 paper by Nickel et al.. This class inherits from the torchkge. models.interfaces.BilinearModel interface. It then has its attributes as well.

References

• Maximilian Nickel, Lorenzo Rosasco, and Tomaso Poggio. Holographic Embeddings of Knowledge Graphs. arXiv :1510.04935 [cs, stat], October 2015.

Parameters • emb_dim (int) – Dimension of embedding space. • n_entities (int) – Number of entities in the current data set. • n_relations (int) – Number of relations in the current data set.

ent_emb Embeddings of the entities, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_ent, emb_dim) rel_emb Embeddings of the relations, initialized with Xavier uniform distribution. Type torch.nn.Embedding, shape: (n_rel, emb_dim) get_embeddings() Return the embeddings of entities and relations. Returns • ent_emb (torch.Tensor, shape: (n_ent, emb_dim), dtype: torch.float) – Embeddings of entities. • rel_emb (torch.Tensor, shape: (n_rel, emb_dim), dtype: torch.float) – Embeddings of relations. static get_rolling_matrix(x) Build a rolling matrix. Parameters x (torch.Tensor, shape: (b_size, dim))– Returns mat – Rolling matrix such that mat[i,j] = x[i - j mod(dim)] Return type torch.Tensor, shape: (b_size, dim, dim) lp_prep_cands(h_idx, t_idx, r_idx) Link prediction evaluation helper function. Get entities embeddings and relations embeddings. The output

1.1. Citations 19 TorchKGE Documentation, Release 0.16.25

will be fed to the lp_scoring_function method. See torchkge.models.interfaces.Models for more details on the API. lp_scoring_function(h, t, r) Link prediction evaluation helper function. See torchkge.models.interfaces.Models for more details on the API. normalize_parameters() Normalize the entity embeddings, as explained in original paper. This methods should be called at the end of each training epoch and at the end of training as well. scoring_function(h_idx, t_idx, r_idx) 푇 Compute the scoring function for the triplets given as argument: ℎ · 푀푟 · 푡 where 푀푟 is the rolling matrix built from the relation embedding r. See referenced paper for more details on the score. See torchkge.models.interfaces.Models for more details on the API.

ComplEx class torchkge.models.bilinear.ComplExModel(emb_dim, n_entities, n_relations) Implementation of ComplEx model detailed in 2016 paper by Trouillon et al.. This class inherits from the torchkge.models.interfaces.BilinearModel interface. It then has its attributes as well.

References

• Théo Trouillon, Johannes Welbl, Sebastian Riedel, Éric Gaussier, and Guillaume Bouchard. Complex Embeddings for Simple Link Prediction. arXiv :1606.06357 [cs, stat], June 2016.

Parameters • emb_dim (int) – Dimension of embedding space. • n_entities (int) – Number of entities in the current data set. • n_relations (int) – Number of relations in the current data set.

re_ent_emb Real part of the entities complex embeddings. Initialized with Xavier uniform distribution. Type torch.nn.Embedding, shape: (n_ent, emb_dim) im_ent_emb Imaginary part of the entities complex embeddings. Initialized with Xavier uniform distribution. Type torch.nn.Embedding, shape: (n_ent, emb_dim) re_rel_emb Real part of the relations complex embeddings. Initialized with Xavier uniform distribution. Type torch.nn.Embedding, shape: (n_rel, emb_dim) im_rel_emb Imaginary part of the relations complex embeddings. Initialized with Xavier uniform distribution. Type torch.nn.Embedding, shape: (n_rel, emb_dim) get_embeddings() Return the embeddings of entities and relations. Returns

20 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

• re_ent_emb (torch.Tensor, shape: (n_ent, emb_dim), dtype: torch.float) – Real part of embeddings of entities. • im_ent_emb (torch.Tensor, shape: (n_ent, emb_dim), dtype: torch.float) – Imaginary part of embeddings of entities. • re_rel_emb (torch.Tensor, shape: (n_rel, emb_dim), dtype: torch.float) – Real part of embeddings of relations. • im_rel_emb (torch.Tensor, shape: (n_rel, emb_dim), dtype: torch.float) – Imaginary part of embeddings of relations. lp_prep_cands(h_idx, t_idx, r_idx) Link prediction evaluation helper function. Get entities embeddings and relations embeddings. The output will be fed to the lp_scoring_function method. See torchkge.models.interfaces.Models for more details on the API. lp_scoring_function(h, t, r) Link prediction evaluation helper function. See torchkge.models.interfaces.Models for more details one the API. normalize_parameters() According to original paper, the embeddings should not be normalized. scoring_function(h_idx, t_idx, r_idx) Compute the real part of the Hermitian product ℜ(ℎ푇 · 푑푖푎푔(푟) · 푡¯) for each sample of the batch. See referenced paper for more details on the score. See torchkge.models.interfaces.Models for more details on the API.

ANALOGY class torchkge.models.bilinear.AnalogyModel(emb_dim, n_entities, n_relations, scalar_share=0.5) Implementation of ANALOGY model detailed in 2017 paper by Liu et al.. According to their remark in the implementation details, the number of scalars on the diagonal of each relation-specific matrix is by default set to be half the embedding dimension. This class inherits from the torchkge.models.interfaces. BilinearModel interface. It then has its attributes as well.

References

• Hanxiao Liu, Yuexin Wu, and Yiming Yang. Analogical Inference for Multi-Relational Embeddings. arXiv :1705.02426 [cs], May 2017.

Parameters • emb_dim (int) – Dimension of embedding space. • n_entities (int) – Number of entities in the current data set. • n_relations (int) – Number of relations in the current data set. • scalar_share (float) – Share of the diagonal elements of the relation-specific matri- ces to be scalars. By default it is set to half according to the original paper.

scalar_dim Number of diagonal elements of the relation-specific matrices to be scalars. By default it is set to half the embedding dimension according to the original paper.

1.1. Citations 21 TorchKGE Documentation, Release 0.16.25

Type int complex_dim Number of 2x2 matrices on the diagonals of relation-specific matrices. Type int sc_ent_emb Part of the entities embeddings associated to the scalar part of the relation specific matrices. Initialized with Xavier uniform distribution. Type torch.nn.Embedding, shape: (n_ent, emb_dim) re_ent_emb Real part of the entities complex embeddings. Initialized with Xavier uniform distribution. As explained in the authors’ paper, almost diagonal matrices can be seen as complex matrices. Type torch.nn.Embedding, shape: (n_ent, emb_dim) im_ent_emb Imaginary part of the entities complex embeddings. Initialized with Xavier uniform distribution. As explained in the authors’ paper, almost diagonal matrices can be seen as complex matrices. Type torch.nn.Embedding, shape: (n_ent, emb_dim) sc_rel_emb Part of the entities embeddings associated to the scalar part of the relation specific matrices. Initialized with Xavier uniform distribution. Type torch.nn.Embedding, shape: (n_rel, emb_dim) re_rel_emb Real part of the relations complex embeddings. Initialized with Xavier uniform distribution. As explained in the authors’ paper, almost diagonal matrices can be seen as complex matrices. Type torch.nn.Embedding, shape: (n_rel, emb_dim) im_rel_emb Imaginary part of the relations complex embeddings. Initialized with Xavier uniform distribution. As explained in the authors’ paper, almost diagonal matrices can be seen as complex matrices. Type torch.nn.Embedding, shape: (n_rel, emb_dim) get_embeddings() Return the embeddings of entities and relations. Returns • sc_ent_emb (torch.Tensor, shape: (n_ent, emb_dim), dtype: torch.float) – Scalar part of embeddings of entities. • re_ent_emb (torch.Tensor, shape: (n_ent, emb_dim), dtype: torch.float) – Real part of embeddings of entities. • im_ent_emb (torch.Tensor, shape: (n_ent, emb_dim), dtype: torch.float) – Imaginary part of embeddings of entities. • sc_rel_emb (torch.Tensor, shape: (n_rel, emb_dim), dtype: torch.float) – Scalar part of embeddings of relations. • re_rel_emb (torch.Tensor, shape: (n_rel, emb_dim), dtype: torch.float) – Real part of embeddings of relations. • im_rel_emb (torch.Tensor, shape: (n_rel, emb_dim), dtype: torch.float) – Imaginary part of embeddings of relations.

22 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

lp_prep_cands(h_idx, t_idx, r_idx) Link prediction evaluation helper function. Get entities embeddings and relations embeddings. The output will be fed to the lp_scoring_function method. See torchkge.models.interfaces.Models for more details on the API. lp_scoring_function(h, t, r) Link prediction evaluation helper function. See torchkge.models.interfaces.Models for more details one the API. normalize_parameters() According to original paper, the embeddings should not be normalized. scoring_function(h_idx, t_idx, r_idx) 푇 Compute the scoring function for the triplets given as argument: ℎ푠푐 · 푑푖푎푔(푟푠푐) · 푡푠푐 + ℜ(ℎ푐표푚푝푙 · 푑푖푎푔(푟푐표푚푝푙 · 푡푐표푚푝푙)). See referenced paper for more details on the score. See torchkge.models.interfaces.Models for more details on the API.

Deep Models

ConvKB class torchkge.models.deep.ConvKBModel(emb_dim, n_filters, n_entities, n_relations) Implementation of ConvKB model detailed in 2018 paper by Nguyen et al.. This class inherits from the torchkge.models.interfaces.Model interface. It then has its attributes as well.

References

• Nguyen, D. Q., Nguyen, T. D., Nguyen, D. Q., and Phung, D. A Novel Embed- ding Model for Knowledge Base Completion Based on Convolutional Neural Network. In Proceedings of the 2018 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technolo- gies (2018), vol. 2, pp. 327–333.

Parameters • emb_dim (int) – Dimension of embedding space. • n_filters (int) – Number of filters used for convolution. • n_entities (int) – Number of entities in the current data set. • n_relations (int) – Number of relations in the current data set.

ent_emb Embeddings of the entities, initialized with Xavier uniform distribution and then normalized. Type torch.nn.Embedding, shape: (n_ent, emb_dim) rel_emb Embeddings of the relations, initialized with Xavier uniform distribution. Type torch.nn.Embedding, shape: (n_rel, emb_dim) get_embeddings() Return the embeddings of entities and relations. Returns • ent_emb (torch.Tensor, shape: (n_ent, emb_dim), dtype: torch.float) – Embeddings of entities.

1.1. Citations 23 TorchKGE Documentation, Release 0.16.25

• rel_emb (torch.Tensor, shape: (n_rel, emb_dim), dtype: torch.float) – Embeddings of relations. lp_prep_cands(h_idx, t_idx, r_idx) Link prediction evaluation helper function. Get entities embeddings and relations embeddings. The output will be fed to the lp_scoring_function method. See torchkge.models.interfaces.Models for more details on the API. lp_scoring_function(h, t, r) Link prediction evaluation helper function. See torchkge.models.interfaces.Models for more details on the API. normalize_parameters() Normalize the entity embeddings, as explained in original paper. This methods should be called at the end of each training epoch and at the end of training as well. scoring_function(h_idx, t_idx, r_idx) Compute the scoring function for the triplets given as argument: by applying convolutions to the concatenation of the embeddings. See referenced paper for more details on the score. See torchkge.models.interfaces.Models for more details on the API.

1.1.4 Evaluation

Link Prediction

To assess the performance of the link prediction evaluation module of TorchKGE, it was compared with the ones of AmpliGraph (v1.3.1) and OpenKE (version of April, 9). The computation times (in seconds) reported in the following table are averaged over 5 independent evaluation processes. Experiments were done using PyTorch 1.5, TensorFlow 1.15 and a Tesla K80 GPU. Missing values for AmpliGraph are due to missing models in the library.

Model TransE TransD RESCAL ComplEx Dataset FB15k WN18 FB15k WN18 FB15k WN18 FB15k WN18 AmpliGraph 354.8 39.8 537.2 94.9 OpenKE 235.6 42.2 258.5 43.7 789.1 178.4 354.7 63.9 TorchKGE 76.1 13.8 60.8 11.1 46.9 7.1 96.4 18.6 class torchkge.evaluation.link_prediction.LinkPredictionEvaluator(model, knowl- edge_graph) Evaluate performance of given embedding using link prediction method. Parameters • model (torchkge.models.interfaces.Model) – Embedding model inheriting from the right interface. • knowledge_graph (torchkge.data_structures.KnowledgeGraph)– Knowledge graph on which the evaluation will be done. model Embedding model inheriting from the right interface. Type torchkge.models.interfaces.Model kg Knowledge graph on which the evaluation will be done. Type torchkge.data_structures.KnowledgeGraph

24 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

rank_true_heads For each fact, this is the rank of the true head when all entities are ranked as possible replacement of the head entity. They are ranked in decreasing order of scoring function 푓푟(ℎ, 푡). Type torch.Tensor, shape: (n_facts), dtype: torch.int rank_true_tails For each fact, this is the rank of the true tail when all entities are ranked as possible replacement of the tail entity. They are ranked in decreasing order of scoring function 푓푟(ℎ, 푡). Type torch.Tensor, shape: (n_facts), dtype: torch.int filt_rank_true_heads This is the same as the rank_of_true_heads when is the filtered case. See referenced paper by Bordes et al. for more information. Type torch.Tensor, shape: (n_facts), dtype: torch.int filt_rank_true_tails This is the same as the rank_of_true_tails when is the filtered case. See referenced paper by Bordes et al. for more information. Type torch.Tensor, shape: (n_facts), dtype: torch.int evaluated Indicates if the method LinkPredictionEvaluator.evaluate has already been called. Type bool

References

• Antoine Bordes, Nicolas Usunier, Alberto Garcia-Duran, Jason Weston, and Oksana Yakhnenko. Translating Embeddings for Modeling Multi-relational Data. In Advances in Neural In- formation Processing Systems 26, pages 2787–2795, 2013. https://papers.nips.cc/paper/ 5071-translating-embeddings-for-modeling-multi-relational-data

evaluate(b_size, verbose=True) Parameters • b_size (int) – Size of the current batch. • verbose (bool) – Indicates whether a progress bar should be displayed during evalua- tion. hit_at_k(k=10) Parameters k (int) – Hit@k is the number of entities that show up in the top k that give facts present in the dataset. Returns • avg_hitatk (float) – Average of hit@k for head and tail replacement. • filt_avg_hitatk (float) – Filtered average of hit@k for head and tail replacement. mean_rank() Returns • mean_rank (float) – Mean rank of the true entity when replacing alternatively head and tail in any fact of the dataset.

1.1. Citations 25 TorchKGE Documentation, Release 0.16.25

• filt_mean_rank (float) – Filtered mean rank of the true entity when replacing alternatively head and tail in any fact of the dataset. mrr() Returns • avg_mrr (float) – Average of mean recovery rank for head and tail replacement. • filt_avg_mrr (float) – Filtered average of mean recovery rank for head and tail replace- ment. print_results(k=None, n_digits=3) Parameters • k (int or list) – k (or list of k) such that hit@k will be printed. • n_digits (int) – Number of digits to be printed for hit@k and MRR.

Triplet Classification class torchkge.evaluation.triplet_classification.TripletClassificationEvaluator(model, kg_val, kg_test) Evaluate performance of given embedding using triplet classification method.

References

• Richard Socher, Danqi Chen, Christopher D Manning, and Andrew Ng. Reasoning With Neural Tensor Networks for Knowledge Base Completion. In Advances in Neural Information Processing Systems 26, pages 926–934. 2013. https://nlp.stanford.edu/pubs/SocherChenManningNg_NIPS2013.pdf

Parameters • model (torchkge.models.interfaces.Model) – Embedding model inheriting from the right interface. • kg_val (torchkge.data_structures.KnowledgeGraph) – Knowledge graph on which the validation thresholds will be computed. • kg_test (torchkge.data_structures.KnowledgeGraph) – Knowledge graph on which the testing evaluation will be done.

model Embedding model inheriting from the right interface. Type torchkge.models.interfaces.Model kg_val Knowledge graph on which the validation thresholds will be computed. Type torchkge.data_structures.KnowledgeGraph kg_test Knowledge graph on which the evaluation will be done. Type torchkge.data_structures.KnowledgeGraph evaluated Indicate whether the evaluate function has been called.

26 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

Type bool thresholds Float value of the thresholds for the scoring function to consider a triplet as true. It is defined by calling the evaluate method. Type float sampler Negative sampler. Type torchkge.sampling.NegativeSampler accuracy(b_size) Parameters b_size (int) – Batch size. Returns acc – Share of all triplets (true and negatively sampled ones) that where correctly clas- sified using the thresholds learned from the validation set. Return type float evaluate(b_size) Find relation thresholds using the validation set. As described in the paper by Socher et al., for a relation, the threshold is a value t such that if the score of a triplet is larger than t, the fact is true. If a relation is not present in any fact of the validation set, then the largest value score of all negative samples is used as threshold. Parameters b_size (int) – Batch size. get_scores(heads, tails, relations, batch_size) With head, tail and relation indexes, compute the value of the scoring function of the model. Parameters • heads (torch.Tensor, dtype: torch.long, shape: n_facts) – List of heads indices. • tails (torch.Tensor, dtype: torch.long, shape: n_facts) – List of tails indices. • relations (torch.Tensor, dtype: torch.long, shape: n_facts) – List of relation indices. • batch_size (int)– Returns scores – List of scores of each triplet. Return type torch.Tensor, dtype: torch.float, shape: n_facts

1.1.5 Negative Sampling

Uniform negative sampler class torchkge.sampling.UniformNegativeSampler(kg, kg_val=None, kg_test=None, n_neg=1) Uniform negative sampler as presented in 2013 paper by Bordes et al.. Either the head or the tail of a triplet is re- placed by another entity at random. The choice of head/tail is uniform. This class inherits from the torchkge. sampling.NegativeSampler interface. It then has its attributes as well.

1.1. Citations 27 TorchKGE Documentation, Release 0.16.25

References

• Antoine Bordes, Nicolas Usunier, Alberto Garcia-Duran, Jason Weston, and Oksana Yakhnenko. Translating Embeddings for Modeling Multi-relational Data. In Advances in Neural In- formation Processing Systems 26, pages 2787–2795, 2013. https://papers.nips.cc/paper/ 5071-translating-embeddings-for-modeling-multi-relational-data

Parameters • kg (torchkge.data_structures.KnowledgeGraph) – Main knowledge graph (usually training one). • kg_val (torchkge.data_structures.KnowledgeGraph (optional))– Validation knowledge graph. • kg_test (torchkge.data_structures.KnowledgeGraph (optional))– Test knowledge graph. • n_neg (int) – Number of negative sample to create from each fact.

corrupt_batch(heads, tails, relations=None, n_neg=None) For each true triplet, produce a corrupted one not different from any other true triplet. If heads and tails are cuda objects , then the returned tensors are on the GPU. Parameters • heads (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of heads of the relations in the current batch. • tails (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of tails of the relations in the current batch. • relations (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of relations in the current batch. This is optional here and mainly present because of the interface with other NegativeSampler objects. • n_neg (int (opt)) – Number of negative sample to create from each fact. It over- writes the value set at the construction of the sampler. Returns • neg_heads (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of negatively sampled heads of the relations in the current batch. • neg_tails (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of negatively sampled tails of the relations in the current batch.

Bernoulli negative sampler class torchkge.sampling.BernoulliNegativeSampler(kg, kg_val=None, kg_test=None, n_neg=1) Bernoulli negative sampler as presented in 2014 paper by Wang et al.. Either the head or the tail of a triplet is replaced by another entity at random. The choice of head/tail is done using probabilities taking into account profiles of the relations. See the paper for more details. This class inherits from the torchkge.sampling. NegativeSampler interface. It then has its attributes as well.

28 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

References

• Zhen Wang, Jianwen Zhang, Jianlin Feng, and Zheng Chen. Knowledge Graph Embedding by Translating on Hyperplanes. In Twenty-Eighth AAAI Conference on Artificial Intelligence, June 2014. https://www. aaai.org/ocs/index.php/AAAI/AAAI14/paper/view/8531

Parameters • kg (torchkge.data_structures.KnowledgeGraph) – Main knowledge graph (usually training one). • kg_val (torchkge.data_structures.KnowledgeGraph (optional))– Validation knowledge graph. • kg_test (torchkge.data_structures.KnowledgeGraph (optional))– Test knowledge graph. • n_neg (int) – Number of negative sample to create from each fact.

bern_probs Bernoulli sampling probabilities. See paper for more details. Type torch.Tensor, dtype: torch.float, shape: (kg.n_rel) corrupt_batch(heads, tails, relations, n_neg=None) For each true triplet, produce a corrupted one different from any other true triplet. If heads and tails are cuda objects , then the returned tensors are on the GPU. Parameters • heads (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of heads of the relations in the current batch. • tails (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of tails of the relations in the current batch. • relations (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of relations in the current batch. • n_neg (int (opt)) – Number of negative sample to create from each fact. It over- writes the value set at the construction of the sampler. Returns • neg_heads (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of negatively sampled heads of the relations in the current batch. • neg_tails (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of negatively sampled tails of the relations in the current batch. evaluate_probabilities() Evaluate the Bernoulli probabilities for negative sampling as in the TransH original paper by Wang et al. (2014).

Positional negative sampler class torchkge.sampling.PositionalNegativeSampler(kg, kg_val=None, kg_test=None) Positional negative sampler as presented in 2011 paper by Socher et al.. Either the head or the tail of a triplet

1.1. Citations 29 TorchKGE Documentation, Release 0.16.25

is replaced by another entity chosen among entities that have already appeared at the same place in a triplet (involving the same relation). It is not clear in the paper how the choice of head/tail is done. We chose to use Bernoulli sampling as in 2014 paper by Wang et al. as we believe it serves the same purpose as the original paper. This class inherits from the torchkge.sampling.BernouilliNegativeSampler class seen as an interface. It then has its attributes as well.

References

• Richard Socher, Danqi Chen, Christopher D Manning, and Andrew Ng. Reasoning With Neural Tensor Networks for Knowledge Base Completion. In Advances in Neural Information Processing Systems 26, pages 926–934., 2013. https://nlp.stanford.edu/pubs/SocherChenManningNg_NIPS2013.pdf • Zhen Wang, Jianwen Zhang, Jianlin Feng, and Zheng Chen. Knowledge Graph Embedding by Translating on Hyperplanes. In Twenty-Eighth AAAI Conference on Artificial Intelligence, June 2014. https://www. aaai.org/ocs/index.php/AAAI/AAAI14/paper/view/8531

Parameters • kg (torchkge.data_structures.KnowledgeGraph) – Main knowledge graph (usually training one). • kg_val (torchkge.data_structures.KnowledgeGraph (optional))– Validation knowledge graph. • kg_test (torchkge.data_structures.KnowledgeGraph (optional))– Test knowledge graph.

possible_heads keys : relations, values : list of possible heads for each relation. Type dict possible_tails keys : relations, values : list of possible tails for each relation. Type dict n_poss_heads List of number of possible heads for each relation. Type list n_poss_tails List of number of possible tails for each relation. Type list corrupt_batch(heads, tails, relations, n_neg=None) For each true triplet, produce a corrupted one not different from any other golden triplet. If heads and tails are cuda objects, then the returned tensors are on the GPU. Parameters • heads (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of heads of the relations in the current batch. • tails (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of tails of the relations in the current batch.

30 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

• relations (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of relations in the current batch. This is optional here and mainly present because of the interface with other NegativeSampler objects. Returns • neg_heads (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of negatively sampled heads of the relations in the current batch. • neg_tails (torch.Tensor, dtype: torch.long, shape: (batch_size)) – Tensor containing the integer key of negatively sampled tails of the relations in the current batch. find_possibilities() For each relation of the knowledge graph (and possibly the validation graph but not the test graph) find all the possible heads and tails in the sense of Wang et al., e.g. all entities that occupy once this position in another triplet. Returns • possible_heads (dict) – keys : relation index, values : list of possible heads • possible tails (dict) – keys : relation index, values : list of possible tails • n_poss_heads (torch.Tensor, dtype: torch.long, shape: (n_relations)) – Number of possi- ble heads for each relation. • n_poss_tails (torch.Tensor, dtype: torch.long, shape: (n_relations)) – Number of possible tails for each relation.

1.1.6 Data Structure

Knowledge Graph class torchkge.data_structures.KnowledgeGraph(df=None, kg=None, ent2ix=None, rel2ix=None, dict_of_heads=None, dict_of_tails=None) Knowledge graph representation. At least one of df and kg parameters should be passed. Parameters • df (pandas.DataFrame, optional) – Data frame containing three columns [from, to, rel]. • kg (dict, optional) – Dictionary with keys (‘heads’, ‘tails’, ‘relations’) and values the corresponding torch long tensors. • ent2ix (dict, optional) – Dictionary mapping entity labels to their integer key. This is computed if not passed as argument. • rel2ix (dict, optional) – Dictionary mapping relation labels to their integer key. This is computed if not passed as argument. • dict_of_heads (dict, optional) – Dictionary of possible heads ℎ so that the triple (ℎ, 푟, 푡) gives a true fact. The keys are tuples (t, r). This is computed if not passed as argument. • dict_of_tails (dict, optional) – Dictionary of possible tails 푡 so that the triple (ℎ, 푟, 푡) gives a true fact. The keys are tuples (h, r). This is computed if not passed as argument.

1.1. Citations 31 TorchKGE Documentation, Release 0.16.25

ent2ix Dictionary mapping entity labels to their integer key. Type dict rel2ix Dictionary mapping relation labels to their integer key. Type dict n_ent Number of distinct entities in the data set. Type int n_rel Number of distinct entities in the data set. Type int n_facts Number of samples in the data set. A sample is a fact: a triplet (h, r, l). Type int head_idx List of the int key of heads for each fact. Type torch.Tensor, dtype = torch.long, shape: (n_facts) tail_idx List of the int key of tails for each fact. Type torch.Tensor, dtype = torch.long, shape: (n_facts) relations List of the int key of relations for each fact. Type torch.Tensor, dtype = torch.long, shape: (n_facts) evaluate_dicts() Evaluates dicts of possible alternatives to an entity in a fact that still gives a true fact in the entire knowledge graph. get_df() Returns a Pandas DataFrame with columns [‘from’, ‘to’, ‘rel’]. get_mask(share, validation=False) Returns masks to split knowledge graph into train, test and optionally validation sets. The mask is first created by dividing samples between subsets based on relation equilibrium. Then if any entity is not present in the training subset it is manually added by assigning a share of the sample involving the missing entity either as head or tail. Parameters • share (float)– • validation (bool)– Returns • mask (torch.Tensor, shape: (n), dtype: torch.bool) • mask_val (torch.Tensor, shape: (n), dtype: torch.bool (optional)) • mask_te (torch.Tensor, shape: (n), dtype: torch.bool)

32 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

static get_sizes(count, share, validation=False) With count samples, returns how many should go to train and test split_kg(share=0.8, sizes=None, validation=False) Split the knowledge graph into train and test. If sizes is provided then it is used to split the samples as explained below. If only share is provided, the split is done at random but it assures to keep at least one fact involving each type of entity and relation in the training subset. Parameters • share (float) – Percentage to allocate to train set. • sizes (tuple) – Tuple of ints of length 2 or 3. – If len(sizes) == 2, then the first sizes[0] values of the knowledge graph will be used as training set and the rest as test set. – If len(sizes) == 3, then the first sizes[0] values of the knowledge graph will be used as training set, the following sizes[1] as validation set and the last sizes[2] as testing set. • validation (bool) – Indicate if a validation set should be produced along with train and test sets. Returns • train_kg (torchkge.data_structures.KnowledgeGraph) • val_kg (torchkge.data_structures.KnowledgeGraph, optional) • test_kg (torchkge.data_structures.KnowledgeGraph)

Small KG class torchkge.data_structures.SmallKG(heads, tails, relations) Minimalist version of a knowledge graph. Built with tensors of heads, tails and relations.

1.1.7 Utils

Datasets loaders torchkge.utils.datasets.load_fb13(data_home=None) Load FB13 dataset. Parameters data_home (str, optional) – Path to the torchkge_data directory (containing data folders). If files are not present on disk in this directory, they are downloaded and then placed in the right place. Returns • kg_train (torchkge.data_structures.KnowledgeGraph) • kg_val (torchkge.data_structures.KnowledgeGraph) • kg_test (torchkge.data_structures.KnowledgeGraph) torchkge.utils.datasets.load_fb15k(data_home=None) Load FB15k dataset. See here for paper by Bordes et al. originally presenting the dataset. Parameters data_home (str, optional) – Path to the torchkge_data directory (containing data folders). If files are not present on disk in this directory, they are downloaded and then placed in the right place.

1.1. Citations 33 TorchKGE Documentation, Release 0.16.25

Returns • kg_train (torchkge.data_structures.KnowledgeGraph) • kg_val (torchkge.data_structures.KnowledgeGraph) • kg_test (torchkge.data_structures.KnowledgeGraph) torchkge.utils.datasets.load_fb15k237(data_home=None) Load FB15k237 dataset. See here for paper by Toutanova et al. originally presenting the dataset. Parameters data_home (str, optional) – Path to the torchkge_data directory (containing data folders). If files are not present on disk in this directory, they are downloaded and then placed in the right place. Returns • kg_train (torchkge.data_structures.KnowledgeGraph) • kg_val (torchkge.data_structures.KnowledgeGraph) • kg_test (torchkge.data_structures.KnowledgeGraph) torchkge.utils.datasets.load_wn18(data_home=None) Load WN18 dataset. Parameters data_home (str, optional) – Path to the torchkge_data directory (containing data folders). If files are not present on disk in this directory, they are downloaded and then placed in the right place. Returns • kg_train (torchkge.data_structures.KnowledgeGraph) • kg_val (torchkge.data_structures.KnowledgeGraph) • kg_test (torchkge.data_structures.KnowledgeGraph) torchkge.utils.datasets.load_wn18rr(data_home=None) Load WN18RR dataset. See here for paper by Dettmers et al. originally presenting the dataset. Parameters data_home (str, optional) – Path to the torchkge_data directory (containing data folders). If files are not present on disk in this directory, they are downloaded and then placed in the right place. Returns • kg_train (torchkge.data_structures.KnowledgeGraph) • kg_val (torchkge.data_structures.KnowledgeGraph) • kg_test (torchkge.data_structures.KnowledgeGraph) torchkge.utils.datasets.load_yago3_10(data_home=None) Load YAGO3-10 dataset. See here for paper by Dettmers et al. originally presenting the dataset. Parameters data_home (str, optional) – Path to the torchkge_data directory (containing data folders). If files are not present on disk in this directory, they are downloaded and then placed in the right place. Returns • kg_train (torchkge.data_structures.KnowledgeGraph) • kg_val (torchkge.data_structures.KnowledgeGraph) • kg_test (torchkge.data_structures.KnowledgeGraph)

34 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

torchkge.utils.datasets.load_wikidatasets(which, limit_=0, data_home=None) Load WikiDataSets dataset. See here for paper by Boschin et al. originally presenting the dataset. Parameters • which (str) – String indicating which subset of Wikidata should be loaded. Available ones are humans, companies, animals, countries and films. • limit (int, optional (default=0)) – This indicates a lower limit on the number of neighbors an entity should have in the graph to be kept. • data_home (str, optional) – Path to the torchkge_data directory (containing data folders). If files are not present on disk in this directory, they are downloaded and then placed in the right place. Returns kg Return type torchkge.data_structures.KnowledgeGraph torchkge.utils.datasets.load_wikidata_vitals(level=5, data_home=None) Load knowledge graph extracted from Wikidata using the entities corresponding to Wikipedia pages contained in Wikivitals. See here for details on Wikivitals and Wikivitals+ datasets. Parameters • level (int (default=5)) – Either 4 or 5. • data_home (str, optional) – Path to the torchkge_data directory (containing data folders). If files are not present on disk in this directory, they are downloaded and then placed in the right place. Returns • kg (torchkge.data_structures.KnowledgeGraph) • kg_attr (torchkge.data_structures.KnowledgeGraph)

Pre-trained models

TransE model

Model Dataset Dimension Test MRR Filtered Test MRR TransE FB15k 100 0.250 0.420 TransE FB15k237 150 0.187 0.287 TransE WDV5 150 0.258 0.305 TransE WN18RR 100 0.201 0.236 TransE Yago3-10 200 0.143 0.261

torchkge.utils.pretrained_models.load_pretrained_transe(dataset, emb_dim, data_home=None) Load a pretrained version of TransE model. Parameters • dataset (str)– • emb_dim (int) – Embedding dimension • data_home (str (opt, default None)) – Path to the torchkge_data directory (containing data folders). Useful for pre-trained model loading.

1.1. Citations 35 TorchKGE Documentation, Release 0.16.25

Returns model – Pretrained version of TransE model. Return type TorchKGE.model.translation.TransEModel

ComplEx Model

Model Dataset Dimension Test MRR Filtered Test MRR ComplEx FB15k237 200 0.180 0.308 ComplEx WN18RR 200 0.290 0.455 ComplEx WDV5 200 0.283 0.371

torchkge.utils.pretrained_models.load_pretrained_complex(dataset, emb_dim, data_home=None) Load a pretrained version of ComplEx model. Parameters • dataset (str)– • emb_dim (int) – Embedding dimension • data_home (str (opt, default None)) – Path to the torchkge_data directory (containing data folders). Useful for pre-trained model loading. Returns model – Pretrained version of ComplEx model. Return type TorchKGE.model.translation.ComplExModel

Data redundancy torchkge.utils.data_redundancy.duplicates(kg_tr, kg_val, kg_te, theta1=0.8, theta2=0.8, verbose=False, counts=False, reverses=[]) Return the duplicate and reverse duplicate relations as explained in paper by Akrami et al.

References

• Farahnaz Akrami, Mohammed Samiul Saeef, Quingheng Zhang. Realistic Re-evaluation of Knowledge Graph Completion Methods: An Experimental Study. SIGMOD’20, June 14–19, 2020, Portland, OR, USA

Parameters • kg_tr (torchkge.data_structures.KnowledgeGraph) – Train set • kg_val (torchkge.data_structures.KnowledgeGraph) – Validation set • kg_te (torchkge.data_structures.KnowledgeGraph) – Test set • theta1 (float) – First threshold (see paper). • theta2 (float) – Second threshold (see paper). • verbose (bool)–

36 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

• counts (bool) – Should the triplets involving (reverse) duplicate relations be counted in all sets. • reverses (list) – List of known reverse relations. Returns • duplicates (list) – List of pairs giving duplicate relations. • rev_duplicates (list) – List of pairs giving reverse duplicate relations. torchkge.utils.data_redundancy.count_triplets(kg1, kg2, duplicates, rev_duplicates) Parameters • kg1 (torchkge.data_structures.KnowledgeGraph)– • kg2 (torchkge.data_structures.KnowledgeGraph)– • duplicates (list) – List returned by torchkge.utils.data_redundancy.duplicates. • rev_duplicates (list) – List returned by torchkge.utils.data_redundancy.duplicates. Returns • n_duplicates (int) – Number of triplets in kg2 that have their duplicate triplet in kg1 • n_rev_duplicates (int) – Number of triplets in kg2 that have their reverse duplicate triplet in kg1. torchkge.utils.data_redundancy.cartesian_product_relations(kg_tr, kg_val, kg_te, theta=0.8) Return the cartesian product relations as explained in paper by Akrami et al.

References

• Farahnaz Akrami, Mohammed Samiul Saeef, Quingheng Zhang. Realistic Re-evaluation of Knowledge Graph Completion Methods: An Experimental Study. SIGMOD’20, June 14–19, 2020, Portland, OR, USA

Parameters • kg_tr (torchkge.data_structures.KnowledgeGraph) – Train set • kg_val (torchkge.data_structures.KnowledgeGraph) – Validation set • kg_te (torchkge.data_structures.KnowledgeGraph) – Test set • theta (float) – Threshold used to compute the cartesian product relations. Returns selected_relations – List of relations index that are cartesian product relations (see paper for details). Return type list

Dissimilarities torchkge.utils.dissimilarities.l1_dissimilarity(a, b) Compute dissimilarity between rows of a and b as ||푎 − 푏||1. torchkge.utils.dissimilarities.l2_dissimilarity(a, b) 2 Compute dissimilarity between rows of a and b as ||푎 − 푏||2.

1.1. Citations 37 TorchKGE Documentation, Release 0.16.25 torchkge.utils.dissimilarities.l1_torus_dissimilarity(a, b) See paper by Ebisu et al. for details about the definition of this dissimilarity function. torchkge.utils.dissimilarities.l2_torus_dissimilarity(a, b) See paper by Ebisu et al. for details about the definition of this dissimilarity function. torchkge.utils.dissimilarities.el2_torus_dissimilarity(a, b) See paper by Ebisu et al. for details about the definition of this dissimilarity function.

Losses class torchkge.utils.losses.MarginLoss(margin) Margin loss as it was defined in TransE paper by Bordes et al. in 2013. This class implements torch.nn. Module interface. forward(positive_triplets, negative_triplets) Parameters • positive_triplets (torch.Tensor, dtype: torch.float, shape: (b_size)) – Scores of the true triplets as returned by the forward methods of the models. • negative_triplets (torch.Tensor, dtype: torch.float, shape: (b_size)) – Scores of the negative triplets as returned by the forward methods of the models. Returns loss – Loss of the form max{0, 훾 − 푓(ℎ, 푟, 푡) + 푓(ℎ′, 푟′, 푡′)} where 훾 is the margin (defined at initialization), 푓(ℎ, 푟, 푡) is the score of a true fact and 푓(ℎ′, 푟′, 푡′) is the score of the associated negative fact. Return type torch.Tensor, shape: (n_facts, dim), dtype: torch.float class torchkge.utils.losses.LogisticLoss Logistic loss as it was defined in TransE paper by Bordes et al. in 2013. This class implements torch.nn. Module interface. forward(positive_triplets, negative_triplets) Parameters • positive_triplets (torch.Tensor, dtype: torch.float, shape: (b_size)) – Scores of the true triplets as returned by the forward methods of the models. • negative_triplets (torch.Tensor, dtype: torch.float, shape: (b_size)) – Scores of the negative triplets as returned by the forward methods of the models. Returns loss – Loss of the form log(1 + exp(휂 × 푓(ℎ, 푟, 푡)) where 푓(ℎ, 푟, 푡) is the score of the fact and 휂 is either 1 or -1 if the fact is true or false. Return type torch.Tensor, shape: (n_facts, dim), dtype: torch.float class torchkge.utils.losses.BinaryCrossEntropyLoss This class implements torch.nn.Module interface. forward(positive_triplets, negative_triplets) Parameters • positive_triplets (torch.Tensor, dtype: torch.float, shape: (b_size)) – Scores of the true triplets as returned by the forward methods of the models.

38 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

• negative_triplets (torch.Tensor, dtype: torch.float, shape: (b_size)) – Scores of the negative triplets as returned by the forward methods of the models. Returns loss – Loss of the form −휂 ·log(푓(ℎ, 푟, 푡))+(1−휂)·log(1−푓(ℎ, 푟, 푡)) where 푓(ℎ, 푟, 푡) is the score of the fact and 휂 is either 1 or 0 if the fact is true or false. Return type torch.Tensor, shape: (n_facts, dim), dtype: torch.float

Training wrappers

class torchkge.utils.training.TrainDataLoader(kg, batch_size, sampling_type, use_cuda=None) Dataloader providing the training process with batches of true and negatively sampled facts. Parameters • kg (torchkge.data_structures.KnowledgeGraph) – Dataset to be divided in batches. • batch_size (int) – Size of the batches. • sampling_type (str) – Either ‘unif’ (uniform negative sampling) or ‘bern’ (Bernoulli negative sampling). • use_cuda (str (opt, default = None)) – Can be either None (no use of cuda at all), ‘all’ to move all the dataset to cuda and then split in batches or ‘batch’ to simply move the batches to cuda before they are returned. class torchkge.utils.training.Trainer(model, criterion, kg_train, n_epochs, batch_size, opti- mizer, sampling_type=’bern’, use_cuda=None) This class simply wraps a simple training procedure. Parameters • model (torchkge.models.interfaces.Model) – Model to be trained. • criterion – Criteria which should differentiate positive and negative scores. Can be an elements of torchkge.utils.losses • kg_train (torchkge.data_structures.KnowledgeGraph) – KG used for training. • n_epochs (int) – Number of epochs in the training procedure. • n_batches (int) – Number of batches to use. • sampling_type (str) – Either ‘unif’ (uniform negative sampling) or ‘bern’ (Bernoulli negative sampling). • use_cuda (str (opt, default = None)) – Can be either None (no use of cuda at all), ‘all’ to move all the dataset to cuda and then split in batches or ‘batch’ to simply move the batches to cuda before they are returned.

1.1.8 Stable release

To install TorchKGE, run this command in your terminal:

$ pip install torchkge

1.1. Citations 39 TorchKGE Documentation, Release 0.16.25

This is the preferred method to install TorchKGE, as it will always install the most recent stable release. If you don’t have pip installed, this Python installation guide can guide you through the process.

1.1.9 From sources

The sources for TorchKGE can be downloaded from the Github repo. You can either clone the public repository:

$ git clone git://github.com/torchkge/torchkge

Or download the tarball:

$ curl -OL https://github.com/torchkge/torchkge/tarball/master

Once you have a copy of the source, you can install it with:

$ python setup.py install

1.1.10 Contributing

Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given. You can contribute in many ways:

Types of Contributions

Report Bugs

Report bugs at https://github.com/torchkge-team/torchkge/issues. If you are reporting a bug, please include: • Your operating system name and version. • Any details about your local setup that might be helpful in troubleshooting. • Detailed steps to reproduce the bug.

Fix Bugs

Look through the GitHub issues for bugs. Anything tagged with “bug” and “help wanted” is open to whoever wants to implement it.

Implement Features

Look through the GitHub issues for features. Anything tagged with “enhancement” and “help wanted” is opento whoever wants to implement it.

40 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

Write Documentation

TorchKGE could always use more documentation, whether as part of the official TorchKGE docs, in docstrings, or even on the web in blog posts, articles, and such.

Submit Feedback

The best way to send feedback is to file an issue at https://github.com/torchkge-team/torchkge/issues. If you are proposing a feature: • Explain in detail how it would work. • Keep the scope as narrow as possible, to make it easier to implement. • Remember that this is a volunteer-driven project, and that contributions are welcome :)

Get Started!

Ready to contribute? Here’s how to set up torchkge for local development. 1. Fork the torchkge repo on GitHub. 2. Clone your fork locally:

$ git clone [email protected]:your_name_here/torchkge.git

3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:

$ mkvirtualenv torchkge $ cd torchkge/ $ python setup.py develop

4. Create a branch for local development:

$ git checkout -b dev/name-of-your-bugfix-or-feature

Now you can make your changes locally. 5. When you’re done making changes, check that your changes pass tests, including testing other Python versions with tox:

$ flake8 torchkge tests $ python setup.py test or py.test $ tox

To get tox, just pip install it into your virtualenv.

6. Commit your changes and push your branch to GitHub:

$ git add . $ git commit -m "Your detailed description of your changes." $ git push origin dev/name-of-your-bugfix-or-feature

7. Submit a pull request through the GitHub website.

1.1. Citations 41 TorchKGE Documentation, Release 0.16.25

Pull Request Guidelines

Before you submit a pull request, check that it meets these guidelines: 1. The pull request should include tests. 2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst. 3. The pull request should work for Python 3.6, 3.7, 3.8, and for PyPi. Check https://travis-ci.com/torchkge-team/ torchkge/pull_requests and make sure that the tests pass for all supported Python versions.

Deploying

A reminder for the maintainers on how to deploy. Make sure all your changes are committed (including an entry in HISTORY.rst). Then run:

$ bumpversion patch # possible: major / minor / patch $ git push $ git push --tags

Travis will then deploy to PyPI if tests pass.

1.1.11 Credits

Development Lead

• Armand Boschin

Contributors

None yet. Why not be the first?

1.1.12 History

0.16.25 (2021-03-01)

• Update in available pretrained models

0.16.24 (2021-02-16)

• Fix deployment

0.16.23 (2021-02-16)

• Removed useless k_max parameter in link-prediction evaluation method

0.16.22 (2021-02-05)

• Add pretrained version of TransE for yago310 and ComplEx for fb15k237 and wdv5.

42 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

0.16.21 (2021-02-02)

• Add pretrained version of TransE for Wikidata-Vitals level 5

0.16.20 (2021-01-22)

• Add support for Python 3.8 • Clean up loading process for kgs • Fix deprecation warning

0.16.19 (2021-01-20)

• Fix release

0.16.18 (2021-01-20)

• Add data loader for wikidata vitals knowledge graphs

0.16.17 (2020-11-03)

• Bug fix get_ranks method

0.16.16 (2020-10-07)

• Bug fix in KG split method

0.16.15 (2020-10-07)

• Fix WikiDataSets loader (again)

0.16.14 (2020-09-21)

• Fix WikiDataSets loader

0.16.13 (2020-08-06)

• Fix reduction in BCE loss • Add pretrained models

0.16.12 (2020-07-07)

• Release patch

0.16.11 (2020-07-07)

• Fix bug in pre-trained models loading that made all models being redownloaded every time

1.1. Citations 43 TorchKGE Documentation, Release 0.16.25

0.16.10 (2020-07-02)

• Minor bug patch

0.16.9 (2020-07-02)

• Update urls to retrieve datasets and pre-trained models.

0.16.8 (2020-07-01)

• Add binary cross-entropy loss

0.16.7 (2020-06-23)

• Change API for pre-trained models

0.16.6 (2020-06-09)

• Patch in pre-trained model loading • Added pre-trained loading for TransE on FB15k237 in dimension 100.

0.16.5 (2020-06-02)

• Release patch

0.16.4 (2020-06-02)

• Add parameter in data redundancy to exclude know reverse triplets from duplicate search.

0.16.3 (2020-05-29)

• Release patch

0.16.2 (2020-05-29)

• Add methods to compute data redundancy in knowledge graphs as in 2020 paper by Akrami et al (see references in concerned methods).

0.16.1 (2020-05-28)

• Patch an awkward import • Add dataset loaders for WN18RR and YAGO3-10

44 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

0.16.0 (2020-04-27)

• Redefinition of the models’ API (simplified interfaces, renamed LP methods and added get_embeddings method) • Implementation of the new API for all models • TorusE implementation fixed • TransD reimplementation to avoid matmul usage (costly in back-propagation) • Added feature to negative samplers to generate several negative samples from each fact. Those can be fed directly to the models. • Added some wrappers for training to utils module. • Progress bars now make the most of tqdm’s possibilities • Code reformatting • Docstrings update

0.15.5 (2020-04-23)

• Defined a new homemade and simpler DataLoader class.

0.15.4 (2020-04-22)

• Removed the use of torch DataLoader object.

0.15.3 (2020-04-02)

• Added a method to print results in link prediction evaluator

0.15.2 (2020-04-01)

• Fixed a misfit test

0.15.1 (2020-04-01)

• Cleared the definition of rank in link prediction

0.15.0 (2020-04-01)

• Improved use of tqdm progress bars

0.14.0 (2020-04-01)

• Change in the API of loss functions (margin and logistic loss) • Documentation update

1.1. Citations 45 TorchKGE Documentation, Release 0.16.25

0.13.0 (2020-02-10)

• Added ConvKB model

0.12.1 (2020-01-10)

• Minor patch in interfaces • Comment additions

0.12.0 (2019-12-05)

• Various bug fixes • New KG splitting method enforcing all entities and relations to appear at least once in the training set.

0.11.3 (2019-11-15)

• Minor bug fixes

0.11.2 (2019-11-11)

• Minor bug fixes

0.11.1 (2019-10-21)

• Fixed requirements conflicts

0.11.0 (2019-10-21)

• Added TorusE model • Added dataloaders • Fixed some bugs

0.10.4 (2019-10-07)

• Fixed error in bilinear models.

0.10.3 (2019-07-23)

• Added intermediate function for hit@k metric in link prediction.

0.10.2 (2019-07-22)

• Fixed assertion error in Analogy model

46 Chapter 1. TorchKGE TorchKGE Documentation, Release 0.16.25

0.10.0 (2019-07-19)

• Implemented Triplet Classification evaluation method • Added Negative Sampler objects to standardize negative sampling methods.

0.9.0 (2019-07-17)

• Implemented HolE model (Nickel et al.) • Implemented ComplEx model (Trouillon et al.) • Implemented ANALOGY model (Liu et al.) • Added knowledge graph splitting into train, validation and test instead of just train and test.

0.8.0 (2019-07-09)

• Implemented Bernoulli negative sampling as in Wang et al. paper on TransH (2014).

0.7.0 (2019-07-01)

• Implemented Mean Reciprocal Rank measure of performance. • Implemented Logistic Loss. • Changed implementation of margin loss to use torch methods.

0.6.0 (2019-06-25)

• Implemented DistMult

0.5.0 (2019-06-24)

• Changed implementation of LinkPrediction ranks by moving functions to model methods. • Implemented RESCAL.

0.4.0 (2019-05-15)

• Fixed a major bug/problem in the Evaluation protocol of LinkPrediction.

0.3.1 (2019-05-10)

• Minor bug fixes in the various normalization functions.

0.3.0 (2019-05-09)

• Fixed CUDA support.

1.1. Citations 47 TorchKGE Documentation, Release 0.16.25

0.2.0 (2019-05-07)

• Added support for filtered performance measures.

0.1.7 (2019-04-03)

• First real release on PyPI.

0.1.0 (2019-04-01)

• First release on PyPI.

48 Chapter 1. TorchKGE Index

A emb_dim (torchkge.models.translation.TorusEModel at- accuracy() (torchkge.evaluation.triplet_classification.TripletClassificationEvaluatortribute), 16 method), 27 emb_dim (torchkge.models.translation.TransEModel at- AnalogyModel (class in torchkge.models.bilinear), 21 tribute), 11 emb_dim (torchkge.models.translation.TransHModel at- B tribute), 12 bern_probs (torchkge.sampling.BernoulliNegativeSamplerent2ix (torchkge.data_structures.KnowledgeGraph at- attribute), 29 tribute), 31 BernoulliNegativeSampler (class in ent_emb (torchkge.models.bilinear.DistMultModel at- torchkge.sampling), 28 tribute), 18 BinaryCrossEntropyLoss (class in ent_emb (torchkge.models.bilinear.HolEModel at- torchkge.utils.losses), 38 tribute), 19 ent_emb (torchkge.models.bilinear.RESCALModel at- C tribute), 17 cartesian_product_relations() (in module ent_emb (torchkge.models.deep.ConvKBModel at- torchkge.utils.data_redundancy), 37 tribute), 23 complex_dim (torchkge.models.bilinear.AnalogyModel ent_emb (torchkge.models.translation.TorusEModel at- attribute), 22 tribute), 16 ComplExModel (class in torchkge.models.bilinear), 20 ent_emb (torchkge.models.translation.TransDModel at- ConvKBModel (class in torchkge.models.deep), 23 tribute), 14 corrupt_batch() (torchkge.sampling.BernoulliNegativeSamplerent_emb (torchkge.models.translation.TransEModel at- method), 29 tribute), 11 corrupt_batch() (torchkge.sampling.PositionalNegativeSamplerent_emb (torchkge.models.translation.TransHModel at- method), 30 tribute), 12 corrupt_batch() (torchkge.sampling.UniformNegativeSamplerent_emb (torchkge.models.translation.TransRModel at- method), 28 tribute), 13 count_triplets() (in module ent_emb_dim (torchkge.models.translation.TransDModel torchkge.utils.data_redundancy), 37 attribute), 14 ent_emb_dim (torchkge.models.translation.TransRModel D attribute), 13 dissimilarity (torchkge.models.interfaces.TranslationModelent_proj_vect (torchkge.models.translation.TransDModel attribute), 10 attribute), 15 DistMultModel (class in torchkge.models.bilinear), evaluate() (torchkge.evaluation.link_prediction.LinkPredictionEvaluator 18 method), 25 duplicates() (in module evaluate() (torchkge.evaluation.triplet_classification.TripletClassificationEvaluator torchkge.utils.data_redundancy), 36 method), 27 evaluate_dicts() (torchkge.data_structures.KnowledgeGraph E method), 32 el2_torus_dissimilarity() (in module evaluate_probabilities() torchkge.utils.dissimilarities), 38 (torchkge.sampling.BernoulliNegativeSampler

49 TorchKGE Documentation, Release 0.16.25

method), 29 get_embeddings() (torchkge.models.translation.TransEModel evaluated (torchkge.evaluation.link_prediction.LinkPredictionEvaluatormethod), 11 attribute), 25 get_embeddings() (torchkge.models.translation.TransHModel evaluated (torchkge.evaluation.triplet_classification.TripletClassificationEvaluatormethod), 12 attribute), 26 get_embeddings() (torchkge.models.translation.TransRModel evaluated_projections method), 13 (torchkge.models.translation.TransDModel get_mask() (torchkge.data_structures.KnowledgeGraph attribute), 15 method), 32 evaluated_projections get_rolling_matrix() (torchkge.models.translation.TransRModel (torchkge.models.bilinear.HolEModel static attribute), 13 method), 19 get_scores() (torchkge.evaluation.triplet_classification.TripletClassificationEvaluator F method), 27 filt_rank_true_heads get_sizes() (torchkge.data_structures.KnowledgeGraph (torchkge.evaluation.link_prediction.LinkPredictionEvaluatorstatic method), 32 attribute), 25 filt_rank_true_tails H (torchkge.evaluation.link_prediction.LinkPredictionEvaluatorhead_idx (torchkge.data_structures.KnowledgeGraph attribute), 25 attribute), 32 find_possibilities() hit_at_k() (torchkge.evaluation.link_prediction.LinkPredictionEvaluator (torchkge.sampling.PositionalNegativeSampler method), 25 method), 31 HolEModel (class in torchkge.models.bilinear), 19 forward() (torchkge.models.interfaces.Model method),7 I forward() (torchkge.utils.losses.BinaryCrossEntropyLossim_ent_emb (torchkge.models.bilinear.AnalogyModel method), 38 attribute), 22 forward() (torchkge.utils.losses.LogisticLoss im_ent_emb (torchkge.models.bilinear.ComplExModel method), 38 attribute), 20 forward() (torchkge.utils.losses.MarginLoss method), im_rel_emb (torchkge.models.bilinear.AnalogyModel 38 attribute), 22 im_rel_emb (torchkge.models.bilinear.ComplExModel G attribute), 20 get_df() (torchkge.data_structures.KnowledgeGraph method), 32 K get_embeddings() (torchkge.models.bilinear.AnalogyModelkg (torchkge.evaluation.link_prediction.LinkPredictionEvaluator method), 22 attribute), 24 get_embeddings() (torchkge.models.bilinear.ComplExModelkg_test (torchkge.evaluation.triplet_classification.TripletClassificationEvaluator method), 20 attribute), 26 get_embeddings() (torchkge.models.bilinear.DistMultModelkg_val (torchkge.evaluation.triplet_classification.TripletClassificationEvaluator method), 18 attribute), 26 get_embeddings() (torchkge.models.bilinear.HolEModelKnowledgeGraph (class in torchkge.data_structures), method), 19 31 get_embeddings() (torchkge.models.bilinear.RESCALModel method), 17 L get_embeddings() (torchkge.models.deep.ConvKBModell1_dissimilarity() (in module method), 23 torchkge.utils.dissimilarities), 37 get_embeddings() (torchkge.models.interfaces.Model l1_torus_dissimilarity() (in module method),7 torchkge.utils.dissimilarities), 37 get_embeddings() (torchkge.models.interfaces.TranslationModell2_dissimilarity() (in module method), 10 torchkge.utils.dissimilarities), 37 get_embeddings() (torchkge.models.translation.TorusEModell2_torus_dissimilarity() (in module method), 16 torchkge.utils.dissimilarities), 38 get_embeddings() (torchkge.models.translation.TransDModelLinkPredictionEvaluator (class in method), 15 torchkge.evaluation.link_prediction), 24

50 Index TorchKGE Documentation, Release 0.16.25

load_fb13() (in module torchkge.utils.datasets), 33 method), 12 load_fb15k() (in module torchkge.utils.datasets), 33 lp_prep_cands() (torchkge.models.translation.TransRModel load_fb15k237() (in module method), 14 torchkge.utils.datasets), 34 lp_scoring_function() load_pretrained_complex() (in module (torchkge.models.bilinear.AnalogyModel torchkge.utils.pretrained_models), 36 method), 23 load_pretrained_transe() (in module lp_scoring_function() torchkge.utils.pretrained_models), 35 (torchkge.models.bilinear.ComplExModel load_wikidata_vitals() (in module method), 21 torchkge.utils.datasets), 35 lp_scoring_function() load_wikidatasets() (in module (torchkge.models.bilinear.DistMultModel torchkge.utils.datasets), 34 method), 18 load_wn18() (in module torchkge.utils.datasets), 34 lp_scoring_function() load_wn18rr() (in module torchkge.utils.datasets), (torchkge.models.bilinear.HolEModel method), 34 20 load_yago3_10() (in module lp_scoring_function() torchkge.utils.datasets), 34 (torchkge.models.bilinear.RESCALModel LogisticLoss (class in torchkge.utils.losses), 38 method), 17 lp_compute_ranks() lp_scoring_function() (torchkge.models.interfaces.Model method),7 (torchkge.models.deep.ConvKBModel method), lp_evaluate_projections() 24 (torchkge.models.translation.TransDModel lp_scoring_function() method), 15 (torchkge.models.interfaces.Model method),9 lp_evaluate_projections() lp_scoring_function() (torchkge.models.translation.TransHModel (torchkge.models.interfaces.TranslationModel method), 12 method), 10 lp_evaluate_projections() (torchkge.models.translation.TransRModel M method), 14 MarginLoss (class in torchkge.utils.losses), 38 lp_helper() (torchkge.models.interfaces.Model mean_rank() (torchkge.evaluation.link_prediction.LinkPredictionEvaluator method),8 method), 25 lp_prep_cands() (torchkge.models.bilinear.AnalogyModelModel (class in torchkge.models.interfaces),6 method), 22 model (torchkge.evaluation.link_prediction.LinkPredictionEvaluator lp_prep_cands() (torchkge.models.bilinear.ComplExModel attribute), 24 method), 21 model (torchkge.evaluation.triplet_classification.TripletClassificationEvaluator lp_prep_cands() (torchkge.models.bilinear.DistMultModel attribute), 26 method), 18 mrr() (torchkge.evaluation.link_prediction.LinkPredictionEvaluator lp_prep_cands() (torchkge.models.bilinear.HolEModel method), 26 method), 19 lp_prep_cands() (torchkge.models.bilinear.RESCALModelN method), 17 n_ent (torchkge.data_structures.KnowledgeGraph at- lp_prep_cands() (torchkge.models.deep.ConvKBModel tribute), 32 method), 24 n_ent (torchkge.models.interfaces.Model attribute),6 lp_prep_cands() (torchkge.models.interfaces.Model n_facts (torchkge.data_structures.KnowledgeGraph method),8 attribute), 32 lp_prep_cands() (torchkge.models.interfaces.TranslationModeln_poss_heads (torchkge.sampling.PositionalNegativeSampler method), 10 attribute), 30 lp_prep_cands() (torchkge.models.translation.TorusEModeln_poss_tails (torchkge.sampling.PositionalNegativeSampler method), 16 attribute), 30 lp_prep_cands() (torchkge.models.translation.TransDModeln_rel (torchkge.data_structures.KnowledgeGraph at- method), 15 tribute), 32 lp_prep_cands() (torchkge.models.translation.TransEModeln_rel (torchkge.models.interfaces.Model attribute),7 method), 11 norm_vect (torchkge.models.translation.TransHModel lp_prep_cands() (torchkge.models.translation.TransHModel attribute), 12

Index 51 TorchKGE Documentation, Release 0.16.25 normalize_parameters() attribute), 15 (torchkge.models.bilinear.AnalogyModel projected_entities method), 23 (torchkge.models.translation.TransRModel normalize_parameters() attribute), 13 (torchkge.models.bilinear.ComplExModel method), 21 R normalize_parameters() rank_true_heads (torchkge.evaluation.link_prediction.LinkPredictionEvaluator (torchkge.models.bilinear.DistMultModel attribute), 24 method), 18 rank_true_tails (torchkge.evaluation.link_prediction.LinkPredictionEvaluator normalize_parameters() attribute), 25 (torchkge.models.bilinear.HolEModel method), re_ent_emb (torchkge.models.bilinear.AnalogyModel 20 attribute), 22 normalize_parameters() re_ent_emb (torchkge.models.bilinear.ComplExModel (torchkge.models.bilinear.RESCALModel attribute), 20 method), 17 re_rel_emb (torchkge.models.bilinear.AnalogyModel normalize_parameters() attribute), 22 (torchkge.models.deep.ConvKBModel method), re_rel_emb (torchkge.models.bilinear.ComplExModel 24 attribute), 20 normalize_parameters() rel2ix (torchkge.data_structures.KnowledgeGraph at- (torchkge.models.interfaces.Model method),9 tribute), 32 normalize_parameters() rel_emb (torchkge.models.bilinear.DistMultModel at- (torchkge.models.interfaces.TranslationModel tribute), 18 method), 10 rel_emb (torchkge.models.bilinear.HolEModel at- normalize_parameters() tribute), 19 (torchkge.models.translation.TorusEModel rel_emb (torchkge.models.deep.ConvKBModel at- method), 16 tribute), 23 normalize_parameters() rel_emb (torchkge.models.translation.TorusEModel at- (torchkge.models.translation.TransDModel tribute), 16 method), 15 rel_emb (torchkge.models.translation.TransDModel at- normalize_parameters() tribute), 14 (torchkge.models.translation.TransEModel rel_emb (torchkge.models.translation.TransEModel at- method), 11 tribute), 11 normalize_parameters() rel_emb (torchkge.models.translation.TransHModel at- (torchkge.models.translation.TransHModel tribute), 12 method), 12 rel_emb (torchkge.models.translation.TransRModel at- normalize_parameters() tribute), 13 (torchkge.models.translation.TransRModel rel_emb_dim (torchkge.models.translation.TransDModel method), 14 attribute), 14 rel_emb_dim (torchkge.models.translation.TransRModel P attribute), 13 PositionalNegativeSampler (class in rel_mat (torchkge.models.bilinear.RESCALModel at- torchkge.sampling), 29 tribute), 17 possible_heads (torchkge.sampling.PositionalNegativeSamplerrel_proj_vect (torchkge.models.translation.TransDModel attribute), 30 attribute), 15 possible_tails (torchkge.sampling.PositionalNegativeSamplerrelations (torchkge.data_structures.KnowledgeGraph attribute), 30 attribute), 32 print_results() (torchkge.evaluation.link_prediction.LinkPredictionEvaluatorRESCALModel (class in torchkge.models.bilinear), 17 method), 26 proj_mat (torchkge.models.translation.TransRModel S attribute), 13 sampler (torchkge.evaluation.triplet_classification.TripletClassificationEvaluator project() (torchkge.models.translation.TransDModel attribute), 27 method), 15 sc_ent_emb (torchkge.models.bilinear.AnalogyModel projected_entities attribute), 22 (torchkge.models.translation.TransDModel

52 Index TorchKGE Documentation, Release 0.16.25 sc_rel_emb (torchkge.models.bilinear.AnalogyModel TrainDataLoader (class in torchkge.utils.training), attribute), 22 39 scalar_dim (torchkge.models.bilinear.AnalogyModel Trainer (class in torchkge.utils.training), 39 attribute), 21 TransDModel (class in torchkge.models.translation), scoring_function() 14 (torchkge.models.bilinear.AnalogyModel TransEModel (class in torchkge.models.translation), method), 23 10 scoring_function() TransHModel (class in torchkge.models.translation), (torchkge.models.bilinear.ComplExModel 11 method), 21 TranslationModel (class in scoring_function() torchkge.models.interfaces),9 (torchkge.models.bilinear.DistMultModel TransRModel (class in torchkge.models.translation), method), 19 12 scoring_function() TripletClassificationEvaluator (class in (torchkge.models.bilinear.HolEModel method), torchkge.evaluation.triplet_classification), 26 20 scoring_function() U (torchkge.models.bilinear.RESCALModel UniformNegativeSampler (class in method), 18 torchkge.sampling), 27 scoring_function() (torchkge.models.deep.ConvKBModel method), 24 scoring_function() (torchkge.models.interfaces.Model method),9 scoring_function() (torchkge.models.interfaces.TranslationModel method), 10 scoring_function() (torchkge.models.translation.TorusEModel method), 16 scoring_function() (torchkge.models.translation.TransDModel method), 15 scoring_function() (torchkge.models.translation.TransEModel method), 11 scoring_function() (torchkge.models.translation.TransHModel method), 12 scoring_function() (torchkge.models.translation.TransRModel method), 14 SmallKG (class in torchkge.data_structures), 33 split_kg() (torchkge.data_structures.KnowledgeGraph method), 33 T tail_idx (torchkge.data_structures.KnowledgeGraph attribute), 32 thresholds (torchkge.evaluation.triplet_classification.TripletClassificationEvaluator attribute), 27 TorusEModel (class in torchkge.models.translation), 16

Index 53