dicee.abstracts

Classes

AbstractTrainer

Abstract class for Trainer class for knowledge graph embedding models

BaseInteractiveKGE

Abstract/base class for using knowledge graph embedding models interactively.

InteractiveQueryDecomposition

AbstractCallback

Abstract class for Callback class for knowledge graph embedding models

AbstractPPECallback

Abstract class for Callback class for knowledge graph embedding models

BaseInteractiveTrainKGE

Abstract/base class for training knowledge graph embedding models interactively.

Module Contents

class dicee.abstracts.AbstractTrainer(args, callbacks)[source]

Abstract class for Trainer class for knowledge graph embedding models

Parameter

argsstr

?

callbacks: list

?

attributes
callbacks
is_global_zero = True
global_rank = 0
local_rank = 0
strategy = None
on_fit_start(*args, **kwargs)[source]

A function to call callbacks before the training starts.

Parameter

args

kwargs

rtype:

None

on_fit_end(*args, **kwargs)[source]

A function to call callbacks at the ned of the training.

Parameter

args

kwargs

rtype:

None

on_train_epoch_end(*args, **kwargs)[source]

A function to call callbacks at the end of an epoch.

Parameter

args

kwargs

rtype:

None

on_train_batch_end(*args, **kwargs)[source]

A function to call callbacks at the end of each mini-batch during training.

Parameter

args

kwargs

rtype:

None

static save_checkpoint(full_path: str, model) None[source]

A static function to save a model into disk

Parameter

full_path : str

model:

rtype:

None

class dicee.abstracts.BaseInteractiveKGE(path: str = None, url: str = None, construct_ensemble: bool = False, model_name: str = None, apply_semantic_constraint: bool = False)[source]

Abstract/base class for using knowledge graph embedding models interactively.

Parameter

path_of_pretrained_model_dirstr

?

construct_ensemble: boolean

?

model_name: str apply_semantic_constraint : boolean

construct_ensemble = False
apply_semantic_constraint = False
configs
get_eval_report() dict[source]
get_bpe_token_representation(str_entity_or_relation: List[str] | str) List[List[int]] | List[int][source]
Parameters:

str_entity_or_relation (corresponds to a str or a list of strings to be tokenized via BPE and shaped.)

Return type:

A list integer(s) or a list of lists containing integer(s)

get_padded_bpe_triple_representation(triples: List[List[str]]) Tuple[List, List, List][source]
Parameters:

triples

set_model_train_mode() None[source]

Setting the model into training mode

Parameter

set_model_eval_mode() None[source]

Setting the model into eval mode

Parameter

property name
sample_entity(n: int) List[str][source]
sample_relation(n: int) List[str][source]
is_seen(entity: str = None, relation: str = None) bool[source]
save() None[source]
get_entity_index(x: str)[source]
get_relation_index(x: str)[source]
index_triple(head_entity: List[str], relation: List[str], tail_entity: List[str]) Tuple[torch.LongTensor, torch.LongTensor, torch.LongTensor][source]

Index Triple

Parameter

head_entity: List[str]

String representation of selected entities.

relation: List[str]

String representation of selected relations.

tail_entity: List[str]

String representation of selected entities.

Returns: Tuple

pytorch tensor of triple score

add_new_entity_embeddings(entity_name: str = None, embeddings: torch.FloatTensor = None)[source]
get_entity_embeddings(items: List[str])[source]

Return embedding of an entity given its string representation

Parameter

items:

entities

get_relation_embeddings(items: List[str])[source]

Return embedding of a relation given its string representation

Parameter

items:

relations

construct_input_and_output(head_entity: List[str], relation: List[str], tail_entity: List[str], labels)[source]

Construct a data point :param head_entity: :param relation: :param tail_entity: :param labels: :return:

parameters()[source]
class dicee.abstracts.InteractiveQueryDecomposition[source]
t_norm(tens_1: torch.Tensor, tens_2: torch.Tensor, tnorm: str = 'min') torch.Tensor[source]
tensor_t_norm(subquery_scores: torch.FloatTensor, tnorm: str = 'min') torch.FloatTensor[source]

Compute T-norm over [0,1] ^{n imes d} where n denotes the number of hops and d denotes number of entities

t_conorm(tens_1: torch.Tensor, tens_2: torch.Tensor, tconorm: str = 'min') torch.Tensor[source]
negnorm(tens_1: torch.Tensor, lambda_: float, neg_norm: str = 'standard') torch.Tensor[source]
class dicee.abstracts.AbstractCallback[source]

Bases: abc.ABC, lightning.pytorch.callbacks.Callback

Abstract class for Callback class for knowledge graph embedding models

Parameter

on_init_start(*args, **kwargs)[source]

Parameter

trainer:

model:

rtype:

None

on_init_end(*args, **kwargs)[source]

Call at the beginning of the training.

Parameter

trainer:

model:

rtype:

None

on_fit_start(trainer, model)[source]

Call at the beginning of the training.

Parameter

trainer:

model:

rtype:

None

on_train_epoch_end(trainer, model)[source]

Call at the end of each epoch during training.

Parameter

trainer:

model:

rtype:

None

on_train_batch_end(*args, **kwargs)[source]

Call at the end of each mini-batch during the training.

Parameter

trainer:

model:

rtype:

None

on_fit_end(*args, **kwargs)[source]

Call at the end of the training.

Parameter

trainer:

model:

rtype:

None

class dicee.abstracts.AbstractPPECallback(num_epochs, path, epoch_to_start, last_percent_to_consider)[source]

Bases: AbstractCallback

Abstract class for Callback class for knowledge graph embedding models

Parameter

num_epochs
path
sample_counter = 0
epoch_count = 0
alphas = None
on_fit_start(trainer, model)[source]

Call at the beginning of the training.

Parameter

trainer:

model:

rtype:

None

on_fit_end(trainer, model)[source]

Call at the end of the training.

Parameter

trainer:

model:

rtype:

None

store_ensemble(param_ensemble) None[source]
class dicee.abstracts.BaseInteractiveTrainKGE[source]

Abstract/base class for training knowledge graph embedding models interactively. This class provides methods for re-training KGE models and also Literal Embedding model.

train_triples(h: List[str], r: List[str], t: List[str], labels: List[float], iteration=2, optimizer=None)[source]
train_k_vs_all(h, r, iteration=1, lr=0.001)[source]

Train k vs all :param head_entity: :param relation: :param iteration: :param lr: :return:

train(kg, lr=0.1, epoch=10, batch_size=32, neg_sample_ratio=10, num_workers=1) None[source]

Retrained a pretrain model on an input KG via negative sampling.

train_literals(train_file_path: str = None, num_epochs: int = 100, lit_lr: float = 0.001, lit_normalization_type: str = 'z-norm', batch_size: int = 1024, sampling_ratio: float = None, random_seed=1, loader_backend: str = 'pandas', freeze_entity_embeddings: bool = True, gate_residual: bool = True, device: str = None)[source]

Trains the Literal Embeddings model using literal data.

Parameters:
  • train_file_path (str) – Path to the training data file.

  • num_epochs (int) – Number of training epochs.

  • lit_lr (float) – Learning rate for the literal model.

  • norm_type (str) – Normalization type to use (‘z-norm’, ‘min-max’, or None).

  • batch_size (int) – Batch size for training.

  • sampling_ratio (float) – Ratio of training triples to use.

  • loader_backend (str) – Backend for loading the dataset (‘pandas’ or ‘rdflib’).

  • freeze_entity_embeddings (bool) – If True, freeze the entity embeddings during training.

  • gate_residual (bool) – If True, use gate residual connections in the model.

  • device (str) – Device to use for training (‘cuda’ or ‘cpu’). If None, will use available GPU or CPU.