dicee.models
Submodules
Classes
Base class for all optimizers. |
|
Base class for all neural network modules. |
|
Base class for all neural network modules. |
|
Base class for all neural network modules. |
|
Base class for all neural network modules. |
|
Embedding Entities and Relations for Learning and Inference in Knowledge Bases |
|
Translating Embeddings for Modeling |
|
A shallow neural model for relation prediction (https://arxiv.org/abs/2101.09090) |
|
A Physical Embedding Model for Knowledge Graphs |
|
Base class for all neural network modules. |
|
Convolutional ComplEx Knowledge Graph Embeddings |
|
Additive Convolutional ComplEx Knowledge Graph Embeddings |
|
Base class for all neural network modules. |
|
Base class for all neural network modules. |
|
Base class for all neural network modules. |
|
Base class for all neural network modules. |
|
Convolutional Quaternion Knowledge Graph Embeddings |
|
Additive Convolutional Quaternion Knowledge Graph Embeddings |
|
Base class for all neural network modules. |
|
Base class for all neural network modules. |
|
Base class for all neural network modules. |
|
Base class for all neural network modules. |
|
Additive Convolutional Octonion Knowledge Graph Embeddings |
|
Base class for all neural network modules. |
|
Without learning dimension scaling |
|
Base class for all neural network modules. |
|
Base class for all neural network modules. |
|
A class for using knowledge graph embedding models implemented in Pykeen |
|
Base class for all neural network modules. |
|
Learning Knowledge Neural Graphs |
|
Learning Knowledge Neural Graphs |
|
Learning Knowledge Neural Graphs |
|
Embedding with trigonometric functions. We represent all entities and relations in the complex number space as: |
|
Embedding with polynomial functions. We represent all entities and relations in the polynomial space as: |
|
Dual Quaternion Knowledge Graph Embeddings (https://ojs.aaai.org/index.php/AAAI/article/download/16850/16657) |
Functions
|
Perform quaternion multiplication |
|
|
|
|
|
Package Contents
- class dicee.models.ADOPT(params: torch.optim.optimizer.ParamsT, lr: float | torch.Tensor = 0.001, betas: Tuple[float, float] = (0.9, 0.9999), eps: float = 1e-06, clip_lambda: Callable[[int], float] | None = lambda step: ..., weight_decay: float = 0.0, decouple: bool = False, *, foreach: bool | None = None, maximize: bool = False, capturable: bool = False, differentiable: bool = False, fused: bool | None = None)[source]
Bases:
torch.optim.optimizer.Optimizer
Base class for all optimizers.
Warning
Parameters need to be specified as collections that have a deterministic ordering that is consistent between runs. Examples of objects that don’t satisfy those properties are sets and iterators over values of dictionaries.
- Parameters:
params (iterable) – an iterable of
torch.Tensor
s ordict
s. Specifies what Tensors should be optimized.defaults – (dict): a dict containing default values of optimization options (used when a parameter group doesn’t specify them).
- clip_lambda
- class dicee.models.BaseKGELightning(*args, **kwargs)[source]
Bases:
lightning.LightningModule
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- training_step_outputs = []
- training_step(batch, batch_idx=None)[source]
Here you compute and return the training loss and some additional metrics for e.g. the progress bar or logger.
- Parameters:
batch – The output of your data iterable, normally a
DataLoader
.batch_idx – The index of this batch.
dataloader_idx – The index of the dataloader that produced this batch. (only if multiple dataloaders used)
- Returns:
Tensor
- The loss tensordict
- A dictionary which can include any keys, but must include the key'loss'
in the case of automatic optimization.None
- In automatic optimization, this will skip to the next batch (but is not supported for multi-GPU, TPU, or DeepSpeed). For manual optimization, this has no special meaning, as returning the loss is not required.
In this step you’d normally do the forward pass and calculate the loss for a batch. You can also do fancier things like multiple forward passes or something model specific.
Example:
def training_step(self, batch, batch_idx): x, y, z = batch out = self.encoder(x) loss = self.loss(out, x) return loss
To use multiple optimizers, you can switch to ‘manual optimization’ and control their stepping:
def __init__(self): super().__init__() self.automatic_optimization = False # Multiple optimizers (e.g.: GANs) def training_step(self, batch, batch_idx): opt1, opt2 = self.optimizers() # do training_step with encoder ... opt1.step() # do training_step with decoder ... opt2.step()
Note
When
accumulate_grad_batches
> 1, the loss returned here will be automatically normalized byaccumulate_grad_batches
internally.
- loss_function(yhat_batch: torch.FloatTensor, y_batch: torch.FloatTensor)[source]
- Parameters:
yhat_batch
y_batch
- on_train_epoch_end(*args, **kwargs)[source]
Called in the training loop at the very end of the epoch.
To access all batch outputs at the end of the epoch, you can cache step outputs as an attribute of the
LightningModule
and access them in this hook:class MyLightningModule(L.LightningModule): def __init__(self): super().__init__() self.training_step_outputs = [] def training_step(self): loss = ... self.training_step_outputs.append(loss) return loss def on_train_epoch_end(self): # do something with all training_step outputs, for example: epoch_mean = torch.stack(self.training_step_outputs).mean() self.log("training_epoch_mean", epoch_mean) # free up the memory self.training_step_outputs.clear()
- test_dataloader() None [source]
An iterable or collection of iterables specifying test samples.
For more information about multiple dataloaders, see this section.
For data processing use the following pattern:
download in
prepare_data()
process and split in
setup()
However, the above are only necessary for distributed processing.
Warning
do not assign state in prepare_data
test()
prepare_data()
setup()
Note
Lightning tries to add the correct sampler for distributed and arbitrary hardware. There is no need to set it yourself.
Note
If you don’t need a test dataset and a
test_step()
, you don’t need to implement this method.
- val_dataloader() None [source]
An iterable or collection of iterables specifying validation samples.
For more information about multiple dataloaders, see this section.
The dataloader you return will not be reloaded unless you set :paramref:`~lightning.pytorch.trainer.trainer.Trainer.reload_dataloaders_every_n_epochs` to a positive integer.
It’s recommended that all data downloads and preparation happen in
prepare_data()
.fit()
validate()
prepare_data()
setup()
Note
Lightning tries to add the correct sampler for distributed and arbitrary hardware There is no need to set it yourself.
Note
If you don’t need a validation dataset and a
validation_step()
, you don’t need to implement this method.
- predict_dataloader() None [source]
An iterable or collection of iterables specifying prediction samples.
For more information about multiple dataloaders, see this section.
It’s recommended that all data downloads and preparation happen in
prepare_data()
.predict()
prepare_data()
setup()
Note
Lightning tries to add the correct sampler for distributed and arbitrary hardware There is no need to set it yourself.
- Returns:
A
torch.utils.data.DataLoader
or a sequence of them specifying prediction samples.
- train_dataloader() None [source]
An iterable or collection of iterables specifying training samples.
For more information about multiple dataloaders, see this section.
The dataloader you return will not be reloaded unless you set :paramref:`~lightning.pytorch.trainer.trainer.Trainer.reload_dataloaders_every_n_epochs` to a positive integer.
For data processing use the following pattern:
download in
prepare_data()
process and split in
setup()
However, the above are only necessary for distributed processing.
Warning
do not assign state in prepare_data
fit()
prepare_data()
setup()
Note
Lightning tries to add the correct sampler for distributed and arbitrary hardware. There is no need to set it yourself.
- configure_optimizers(parameters=None)[source]
Choose what optimizers and learning-rate schedulers to use in your optimization. Normally you’d need one. But in the case of GANs or similar you might have multiple. Optimization with multiple optimizers only works in the manual optimization mode.
- Returns:
Any of these 6 options.
Single optimizer.
List or Tuple of optimizers.
Two lists - The first list has multiple optimizers, and the second has multiple LR schedulers (or multiple
lr_scheduler_config
).Dictionary, with an
"optimizer"
key, and (optionally) a"lr_scheduler"
key whose value is a single LR scheduler orlr_scheduler_config
.None - Fit will run without any optimizer.
The
lr_scheduler_config
is a dictionary which contains the scheduler and its associated configuration. The default configuration is shown below.lr_scheduler_config = { # REQUIRED: The scheduler instance "scheduler": lr_scheduler, # The unit of the scheduler's step size, could also be 'step'. # 'epoch' updates the scheduler on epoch end whereas 'step' # updates it after a optimizer update. "interval": "epoch", # How many epochs/steps should pass between calls to # `scheduler.step()`. 1 corresponds to updating the learning # rate after every epoch/step. "frequency": 1, # Metric to to monitor for schedulers like `ReduceLROnPlateau` "monitor": "val_loss", # If set to `True`, will enforce that the value specified 'monitor' # is available when the scheduler is updated, thus stopping # training if not found. If set to `False`, it will only produce a warning "strict": True, # If using the `LearningRateMonitor` callback to monitor the # learning rate progress, this keyword can be used to specify # a custom logged name "name": None, }
When there are schedulers in which the
.step()
method is conditioned on a value, such as thetorch.optim.lr_scheduler.ReduceLROnPlateau
scheduler, Lightning requires that thelr_scheduler_config
contains the keyword"monitor"
set to the metric name that the scheduler should be conditioned on.Metrics can be made available to monitor by simply logging it using
self.log('metric_to_track', metric_val)
in yourLightningModule
.Note
Some things to know:
Lightning calls
.backward()
and.step()
automatically in case of automatic optimization.If a learning rate scheduler is specified in
configure_optimizers()
with key"interval"
(default “epoch”) in the scheduler configuration, Lightning will call the scheduler’s.step()
method automatically in case of automatic optimization.If you use 16-bit precision (
precision=16
), Lightning will automatically handle the optimizer.If you use
torch.optim.LBFGS
, Lightning handles the closure function automatically for you.If you use multiple optimizers, you will have to switch to ‘manual optimization’ mode and step them yourself.
If you need to control how often the optimizer steps, override the
optimizer_step()
hook.
- class dicee.models.BaseKGE(args: dict)[source]
Bases:
BaseKGELightning
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- args
- embedding_dim = None
- num_entities = None
- num_relations = None
- num_tokens = None
- learning_rate = None
- apply_unit_norm = None
- input_dropout_rate = None
- optimizer_name = None
- feature_map_dropout_rate = None
- kernel_size = None
- num_of_output_channels = None
- weight_decay = None
- loss
- selected_optimizer = None
- normalizer_class = None
- normalize_head_entity_embeddings
- normalize_relation_embeddings
- normalize_tail_entity_embeddings
- param_init
- input_dp_ent_real
- input_dp_rel_real
- loss_history = []
- byte_pair_encoding
- max_length_subword_tokens
- block_size
- forward_byte_pair_encoded_triple(x: Tuple[torch.LongTensor, torch.LongTensor])[source]
byte pair encoded neural link predictors
- Parameters:
-------
- forward(x: torch.LongTensor | Tuple[torch.LongTensor, torch.LongTensor], y_idx: torch.LongTensor = None)[source]
- Parameters:
x
y_idx
ordered_bpe_entities
- class dicee.models.IdentityClass(args=None)[source]
Bases:
torch.nn.Module
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- args = None
- class dicee.models.BaseKGE(args: dict)[source]
Bases:
BaseKGELightning
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- args
- embedding_dim = None
- num_entities = None
- num_relations = None
- num_tokens = None
- learning_rate = None
- apply_unit_norm = None
- input_dropout_rate = None
- hidden_dropout_rate = None
- optimizer_name = None
- feature_map_dropout_rate = None
- kernel_size = None
- num_of_output_channels = None
- weight_decay = None
- loss
- selected_optimizer = None
- normalizer_class = None
- normalize_head_entity_embeddings
- normalize_relation_embeddings
- normalize_tail_entity_embeddings
- hidden_normalizer
- param_init
- input_dp_ent_real
- input_dp_rel_real
- hidden_dropout
- loss_history = []
- byte_pair_encoding
- max_length_subword_tokens
- block_size
- forward_byte_pair_encoded_triple(x: Tuple[torch.LongTensor, torch.LongTensor])[source]
byte pair encoded neural link predictors
- Parameters:
-------
- forward(x: torch.LongTensor | Tuple[torch.LongTensor, torch.LongTensor], y_idx: torch.LongTensor = None)[source]
- Parameters:
x
y_idx
ordered_bpe_entities
- class dicee.models.DistMult(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Embedding Entities and Relations for Learning and Inference in Knowledge Bases https://arxiv.org/abs/1412.6575
- name = 'DistMult'
- class dicee.models.TransE(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Translating Embeddings for Modeling Multi-relational Data https://proceedings.neurips.cc/paper/2013/file/1cecc7a77928ca8133fa24680a88d2f9-Paper.pdf
- name = 'TransE'
- margin = 4
- class dicee.models.Shallom(args)[source]
Bases:
dicee.models.base_model.BaseKGE
A shallow neural model for relation prediction (https://arxiv.org/abs/2101.09090)
- name = 'Shallom'
- shallom
- class dicee.models.Pyke(args)[source]
Bases:
dicee.models.base_model.BaseKGE
A Physical Embedding Model for Knowledge Graphs
- name = 'Pyke'
- dist_func
- margin = 1.0
- class dicee.models.BaseKGE(args: dict)[source]
Bases:
BaseKGELightning
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- args
- embedding_dim = None
- num_entities = None
- num_relations = None
- num_tokens = None
- learning_rate = None
- apply_unit_norm = None
- input_dropout_rate = None
- hidden_dropout_rate = None
- optimizer_name = None
- feature_map_dropout_rate = None
- kernel_size = None
- num_of_output_channels = None
- weight_decay = None
- loss
- selected_optimizer = None
- normalizer_class = None
- normalize_head_entity_embeddings
- normalize_relation_embeddings
- normalize_tail_entity_embeddings
- hidden_normalizer
- param_init
- input_dp_ent_real
- input_dp_rel_real
- hidden_dropout
- loss_history = []
- byte_pair_encoding
- max_length_subword_tokens
- block_size
- forward_byte_pair_encoded_triple(x: Tuple[torch.LongTensor, torch.LongTensor])[source]
byte pair encoded neural link predictors
- Parameters:
-------
- forward(x: torch.LongTensor | Tuple[torch.LongTensor, torch.LongTensor], y_idx: torch.LongTensor = None)[source]
- Parameters:
x
y_idx
ordered_bpe_entities
- class dicee.models.ConEx(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Convolutional ComplEx Knowledge Graph Embeddings
- name = 'ConEx'
- conv2d
- fc_num_input
- fc1
- norm_fc1
- bn_conv2d
- feature_map_dropout
- residual_convolution(C_1: Tuple[torch.Tensor, torch.Tensor], C_2: Tuple[torch.Tensor, torch.Tensor]) torch.FloatTensor [source]
Compute residual score of two complex-valued embeddings. :param C_1: a tuple of two pytorch tensors that corresponds complex-valued embeddings :param C_2: a tuple of two pytorch tensors that corresponds complex-valued embeddings :return:
- class dicee.models.AConEx(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Additive Convolutional ComplEx Knowledge Graph Embeddings
- name = 'AConEx'
- conv2d
- fc_num_input
- fc1
- norm_fc1
- bn_conv2d
- feature_map_dropout
- residual_convolution(C_1: Tuple[torch.Tensor, torch.Tensor], C_2: Tuple[torch.Tensor, torch.Tensor]) torch.FloatTensor [source]
Compute residual score of two complex-valued embeddings. :param C_1: a tuple of two pytorch tensors that corresponds complex-valued embeddings :param C_2: a tuple of two pytorch tensors that corresponds complex-valued embeddings :return:
- class dicee.models.ComplEx(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- name = 'ComplEx'
- static score(head_ent_emb: torch.FloatTensor, rel_ent_emb: torch.FloatTensor, tail_ent_emb: torch.FloatTensor)[source]
- dicee.models.quaternion_mul(*, Q_1, Q_2) Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor] [source]
Perform quaternion multiplication :param Q_1: :param Q_2: :return:
- class dicee.models.BaseKGE(args: dict)[source]
Bases:
BaseKGELightning
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- args
- embedding_dim = None
- num_entities = None
- num_relations = None
- num_tokens = None
- learning_rate = None
- apply_unit_norm = None
- input_dropout_rate = None
- hidden_dropout_rate = None
- optimizer_name = None
- feature_map_dropout_rate = None
- kernel_size = None
- num_of_output_channels = None
- weight_decay = None
- loss
- selected_optimizer = None
- normalizer_class = None
- normalize_head_entity_embeddings
- normalize_relation_embeddings
- normalize_tail_entity_embeddings
- hidden_normalizer
- param_init
- input_dp_ent_real
- input_dp_rel_real
- hidden_dropout
- loss_history = []
- byte_pair_encoding
- max_length_subword_tokens
- block_size
- forward_byte_pair_encoded_triple(x: Tuple[torch.LongTensor, torch.LongTensor])[source]
byte pair encoded neural link predictors
- Parameters:
-------
- forward(x: torch.LongTensor | Tuple[torch.LongTensor, torch.LongTensor], y_idx: torch.LongTensor = None)[source]
- Parameters:
x
y_idx
ordered_bpe_entities
- class dicee.models.IdentityClass(args=None)[source]
Bases:
torch.nn.Module
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- args = None
- class dicee.models.QMult(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- name = 'QMult'
- explicit = True
- quaternion_multiplication_followed_by_inner_product(h, r, t)[source]
- Parameters:
h – shape: (*batch_dims, dim) The head representations.
r – shape: (*batch_dims, dim) The head representations.
t – shape: (*batch_dims, dim) The tail representations.
- Returns:
Triple scores.
- static quaternion_normalizer(x: torch.FloatTensor) torch.FloatTensor [source]
Normalize the length of relation vectors, if the forward constraint has not been applied yet.
Absolute value of a quaternion
\[|a + bi + cj + dk| = \sqrt{a^2 + b^2 + c^2 + d^2}\]L2 norm of quaternion vector:
\[\|x\|^2 = \sum_{i=1}^d |x_i|^2 = \sum_{i=1}^d (x_i.re^2 + x_i.im_1^2 + x_i.im_2^2 + x_i.im_3^2)\]- Parameters:
x – The vector.
- Returns:
The normalized vector.
- score(head_ent_emb: torch.FloatTensor, rel_ent_emb: torch.FloatTensor, tail_ent_emb: torch.FloatTensor)[source]
- k_vs_all_score(bpe_head_ent_emb, bpe_rel_ent_emb, E)[source]
- Parameters:
bpe_head_ent_emb
bpe_rel_ent_emb
E
- forward_k_vs_sample(x, target_entity_idx)[source]
Completed. Given a head entity and a relation (h,r), we compute scores for all possible triples,i.e., [score(h,r,x)|x in Entities] => [0.0,0.1,…,0.8], shape=> (1, |Entities|) Given a batch of head entities and relations => shape (size of batch,| Entities|)
- class dicee.models.ConvQ(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Convolutional Quaternion Knowledge Graph Embeddings
- name = 'ConvQ'
- entity_embeddings
- relation_embeddings
- conv2d
- fc_num_input
- fc1
- bn_conv1
- bn_conv2
- feature_map_dropout
- forward_k_vs_all(x: torch.Tensor)[source]
Given a head entity and a relation (h,r), we compute scores for all entities. [score(h,r,x)|x in Entities] => [0.0,0.1,…,0.8], shape=> (1, |Entities|) Given a batch of head entities and relations => shape (size of batch,| Entities|)
- class dicee.models.AConvQ(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Additive Convolutional Quaternion Knowledge Graph Embeddings
- name = 'AConvQ'
- entity_embeddings
- relation_embeddings
- conv2d
- fc_num_input
- fc1
- bn_conv1
- bn_conv2
- feature_map_dropout
- forward_k_vs_all(x: torch.Tensor)[source]
Given a head entity and a relation (h,r), we compute scores for all entities. [score(h,r,x)|x in Entities] => [0.0,0.1,…,0.8], shape=> (1, |Entities|) Given a batch of head entities and relations => shape (size of batch,| Entities|)
- class dicee.models.BaseKGE(args: dict)[source]
Bases:
BaseKGELightning
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- args
- embedding_dim = None
- num_entities = None
- num_relations = None
- num_tokens = None
- learning_rate = None
- apply_unit_norm = None
- input_dropout_rate = None
- hidden_dropout_rate = None
- optimizer_name = None
- feature_map_dropout_rate = None
- kernel_size = None
- num_of_output_channels = None
- weight_decay = None
- loss
- selected_optimizer = None
- normalizer_class = None
- normalize_head_entity_embeddings
- normalize_relation_embeddings
- normalize_tail_entity_embeddings
- hidden_normalizer
- param_init
- input_dp_ent_real
- input_dp_rel_real
- hidden_dropout
- loss_history = []
- byte_pair_encoding
- max_length_subword_tokens
- block_size
- forward_byte_pair_encoded_triple(x: Tuple[torch.LongTensor, torch.LongTensor])[source]
byte pair encoded neural link predictors
- Parameters:
-------
- forward(x: torch.LongTensor | Tuple[torch.LongTensor, torch.LongTensor], y_idx: torch.LongTensor = None)[source]
- Parameters:
x
y_idx
ordered_bpe_entities
- class dicee.models.IdentityClass(args=None)[source]
Bases:
torch.nn.Module
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- args = None
- class dicee.models.OMult(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- name = 'OMult'
- static octonion_normalizer(emb_rel_e0, emb_rel_e1, emb_rel_e2, emb_rel_e3, emb_rel_e4, emb_rel_e5, emb_rel_e6, emb_rel_e7)[source]
- score(head_ent_emb: torch.FloatTensor, rel_ent_emb: torch.FloatTensor, tail_ent_emb: torch.FloatTensor)[source]
- forward_k_vs_all(x)[source]
Completed. Given a head entity and a relation (h,r), we compute scores for all possible triples,i.e., [score(h,r,x)|x in Entities] => [0.0,0.1,…,0.8], shape=> (1, |Entities|) Given a batch of head entities and relations => shape (size of batch,| Entities|)
- class dicee.models.ConvO(args: dict)[source]
Bases:
dicee.models.base_model.BaseKGE
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- name = 'ConvO'
- conv2d
- fc_num_input
- fc1
- bn_conv2d
- norm_fc1
- feature_map_dropout
- static octonion_normalizer(emb_rel_e0, emb_rel_e1, emb_rel_e2, emb_rel_e3, emb_rel_e4, emb_rel_e5, emb_rel_e6, emb_rel_e7)[source]
- forward_k_vs_all(x: torch.Tensor)[source]
Given a head entity and a relation (h,r), we compute scores for all entities. [score(h,r,x)|x in Entities] => [0.0,0.1,…,0.8], shape=> (1, |Entities|) Given a batch of head entities and relations => shape (size of batch,| Entities|)
- class dicee.models.AConvO(args: dict)[source]
Bases:
dicee.models.base_model.BaseKGE
Additive Convolutional Octonion Knowledge Graph Embeddings
- name = 'AConvO'
- conv2d
- fc_num_input
- fc1
- bn_conv2d
- norm_fc1
- feature_map_dropout
- static octonion_normalizer(emb_rel_e0, emb_rel_e1, emb_rel_e2, emb_rel_e3, emb_rel_e4, emb_rel_e5, emb_rel_e6, emb_rel_e7)[source]
- forward_k_vs_all(x: torch.Tensor)[source]
Given a head entity and a relation (h,r), we compute scores for all entities. [score(h,r,x)|x in Entities] => [0.0,0.1,…,0.8], shape=> (1, |Entities|) Given a batch of head entities and relations => shape (size of batch,| Entities|)
- class dicee.models.Keci(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- name = 'Keci'
- p
- q
- r
- requires_grad_for_interactions = True
- compute_sigma_pp(hp, rp)[source]
Compute sigma_{pp} = sum_{i=1}^{p-1} sum_{k=i+1}^p (h_i r_k - h_k r_i) e_i e_k
sigma_{pp} captures the interactions between along p bases For instance, let p e_1, e_2, e_3, we compute interactions between e_1 e_2, e_1 e_3 , and e_2 e_3 This can be implemented with a nested two for loops
results = [] for i in range(p - 1):
- for k in range(i + 1, p):
results.append(hp[:, :, i] * rp[:, :, k] - hp[:, :, k] * rp[:, :, i])
sigma_pp = torch.stack(results, dim=2) assert sigma_pp.shape == (b, r, int((p * (p - 1)) / 2))
Yet, this computation would be quite inefficient. Instead, we compute interactions along all p, e.g., e1e1, e1e2, e1e3,
e2e1, e2e2, e2e3, e3e1, e3e2, e3e3
Then select the triangular matrix without diagonals: e1e2, e1e3, e2e3.
- compute_sigma_qq(hq, rq)[source]
Compute sigma_{qq} = sum_{j=1}^{p+q-1} sum_{k=j+1}^{p+q} (h_j r_k - h_k r_j) e_j e_k sigma_{q} captures the interactions between along q bases For instance, let q e_1, e_2, e_3, we compute interactions between e_1 e_2, e_1 e_3 , and e_2 e_3 This can be implemented with a nested two for loops
results = [] for j in range(q - 1):
- for k in range(j + 1, q):
results.append(hq[:, :, j] * rq[:, :, k] - hq[:, :, k] * rq[:, :, j])
sigma_qq = torch.stack(results, dim=2) assert sigma_qq.shape == (b, r, int((q * (q - 1)) / 2))
Yet, this computation would be quite inefficient. Instead, we compute interactions along all p, e.g., e1e1, e1e2, e1e3,
e2e1, e2e2, e2e3, e3e1, e3e2, e3e3
Then select the triangular matrix without diagonals: e1e2, e1e3, e2e3.
- compute_sigma_pq(*, hp, hq, rp, rq)[source]
sum_{i=1}^{p} sum_{j=p+1}^{p+q} (h_i r_j - h_j r_i) e_i e_j
results = [] sigma_pq = torch.zeros(b, r, p, q) for i in range(p):
- for j in range(q):
sigma_pq[:, :, i, j] = hp[:, :, i] * rq[:, :, j] - hq[:, :, j] * rp[:, :, i]
print(sigma_pq.shape)
- clifford_multiplication(h0, hp, hq, r0, rp, rq)[source]
Compute our CL multiplication
h = h_0 + sum_{i=1}^p h_i e_i + sum_{j=p+1}^{p+q} h_j e_j r = r_0 + sum_{i=1}^p r_i e_i + sum_{j=p+1}^{p+q} r_j e_j
ei ^2 = +1 for i =< i =< p ej ^2 = -1 for p < j =< p+q ei ej = -eje1 for i
eq j
h r = sigma_0 + sigma_p + sigma_q + sigma_{pp} + sigma_{q}+ sigma_{pq} where
sigma_0 = h_0 r_0 + sum_{i=1}^p (h_0 r_i) e_i - sum_{j=p+1}^{p+q} (h_j r_j) e_j
sigma_p = sum_{i=1}^p (h_0 r_i + h_i r_0) e_i
sigma_q = sum_{j=p+1}^{p+q} (h_0 r_j + h_j r_0) e_j
sigma_{pp} = sum_{i=1}^{p-1} sum_{k=i+1}^p (h_i r_k - h_k r_i) e_i e_k
sigma_{qq} = sum_{j=1}^{p+q-1} sum_{k=j+1}^{p+q} (h_j r_k - h_k r_j) e_j e_k
sigma_{pq} = sum_{i=1}^{p} sum_{j=p+1}^{p+q} (h_i r_j - h_j r_i) e_i e_j
- construct_cl_multivector(x: torch.FloatTensor, r: int, p: int, q: int) tuple[torch.FloatTensor, torch.FloatTensor, torch.FloatTensor] [source]
Construct a batch of multivectors Cl_{p,q}(mathbb{R}^d)
Parameter
x: torch.FloatTensor with (n,d) shape
- returns:
a0 (torch.FloatTensor with (n,r) shape)
ap (torch.FloatTensor with (n,r,p) shape)
aq (torch.FloatTensor with (n,r,q) shape)
- forward_k_vs_all(x: torch.Tensor) torch.FloatTensor [source]
Kvsall training
Retrieve real-valued embedding vectors for heads and relations mathbb{R}^d .
Construct head entity and relation embeddings according to Cl_{p,q}(mathbb{R}^d) .
Perform Cl multiplication
Inner product of (3) and all entity embeddings
forward_k_vs_with_explicit and this funcitons are identical Parameter ——— x: torch.LongTensor with (n,2) shape :rtype: torch.FloatTensor with (n, |E|) shape
- construct_batch_selected_cl_multivector(x: torch.FloatTensor, r: int, p: int, q: int) tuple[torch.FloatTensor, torch.FloatTensor, torch.FloatTensor] [source]
Construct a batch of batchs multivectors Cl_{p,q}(mathbb{R}^d)
Parameter
x: torch.FloatTensor with (n,k, d) shape
- returns:
a0 (torch.FloatTensor with (n,k, m) shape)
ap (torch.FloatTensor with (n,k, m, p) shape)
aq (torch.FloatTensor with (n,k, m, q) shape)
- class dicee.models.KeciBase(args)[source]
Bases:
Keci
Without learning dimension scaling
- name = 'KeciBase'
- requires_grad_for_interactions = False
- class dicee.models.DeCaL(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- name = 'DeCaL'
- entity_embeddings
- relation_embeddings
- p
- q
- r
- re
- forward_triples(x: torch.Tensor) torch.FloatTensor [source]
Parameter
x: torch.LongTensor with (n, ) shape
- rtype:
torch.FloatTensor with (n) shape
- cl_pqr(a: torch.tensor) torch.tensor [source]
Input: tensor(batch_size, emb_dim) —> output: tensor with 1+p+q+r components with size (batch_size, emb_dim/(1+p+q+r)) each.
1) takes a tensor of size (batch_size, emb_dim), split it into 1 + p + q +r components, hence 1+p+q+r must be a divisor of the emb_dim. 2) Return a list of the 1+p+q+r components vectors, each are tensors of size (batch_size, emb_dim/(1+p+q+r))
- compute_sigmas_single(list_h_emb, list_r_emb, list_t_emb)[source]
here we compute all the sums with no others vectors interaction taken with the scalar product with t, that is,
\[s0 = h_0r_0t_0 s1 = \sum_{i=1}^{p}h_ir_it_0 s2 = \sum_{j=p+1}^{p+q}h_jr_jt_0 s3 = \sum_{i=1}^{q}(h_0r_it_i + h_ir_0t_i) s4 = \sum_{i=p+1}^{p+q}(h_0r_it_i + h_ir_0t_i) s5 = \sum_{i=p+q+1}^{p+q+r}(h_0r_it_i + h_ir_0t_i)\]and return:
\[sigma_0t = \sigma_0 \cdot t_0 = s0 + s1 -s2 s3, s4 and s5\]
- compute_sigmas_multivect(list_h_emb, list_r_emb)[source]
Here we compute and return all the sums with vectors interaction for the same and different bases.
For same bases vectors interaction we have
\[\sigma_pp = \sum_{i=1}^{p-1}\sum_{i'=i+1}^{p}(h_ir_{i'}-h_{i'}r_i) (models the interactions between e_i and e_i' for 1 <= i, i' <= p) \sigma_qq = \sum_{j=p+1}^{p+q-1}\sum_{j'=j+1}^{p+q}(h_jr_{j'}-h_{j'} (models the interactions between e_j and e_j' for p+1 <= j, j' <= p+q) \sigma_rr = \sum_{k=p+q+1}^{p+q+r-1}\sum_{k'=k+1}^{p}(h_kr_{k'}-h_{k'}r_k) (models the interactions between e_k and e_k' for p+q+1 <= k, k' <= p+q+r)\]For different base vector interactions, we have
\[\sigma_pq = \sum_{i=1}^{p}\sum_{j=p+1}^{p+q}(h_ir_j - h_jr_i) (interactionsn between e_i and e_j for 1<=i <=p and p+1<= j <= p+q) \sigma_pr = \sum_{i=1}^{p}\sum_{k=p+q+1}^{p+q+r}(h_ir_k - h_kr_i) (interactionsn between e_i and e_k for 1<=i <=p and p+q+1<= k <= p+q+r) \sigma_qr = \sum_{j=p+1}^{p+q}\sum_{j=p+q+1}^{p+q+r}(h_jr_k - h_kr_j) (interactionsn between e_j and e_k for p+1 <= j <=p+q and p+q+1<= j <= p+q+r)\]
- forward_k_vs_all(x: torch.Tensor) torch.FloatTensor [source]
Kvsall training
Retrieve real-valued embedding vectors for heads and relations
Construct head entity and relation embeddings according to Cl_{p,q, r}(mathbb{R}^d) .
Perform Cl multiplication
Inner product of (3) and all entity embeddings
forward_k_vs_with_explicit and this funcitons are identical Parameter ——— x: torch.LongTensor with (n, ) shape :rtype: torch.FloatTensor with (n, |E|) shape
- apply_coefficients(h0, hp, hq, hk, r0, rp, rq, rk)[source]
Multiplying a base vector with its scalar coefficient
- construct_cl_multivector(x: torch.FloatTensor, re: int, p: int, q: int, r: int) tuple[torch.FloatTensor, torch.FloatTensor, torch.FloatTensor] [source]
Construct a batch of multivectors Cl_{p,q,r}(mathbb{R}^d)
Parameter
x: torch.FloatTensor with (n,d) shape
- returns:
a0 (torch.FloatTensor)
ap (torch.FloatTensor)
aq (torch.FloatTensor)
ar (torch.FloatTensor)
- compute_sigma_pp(hp, rp)[source]
Compute .. math:
\sigma_{p,p}^* = \sum_{i=1}^{p-1}\sum_{i'=i+1}^{p}(x_iy_{i'}-x_{i'}y_i)
sigma_{pp} captures the interactions between along p bases For instance, let p e_1, e_2, e_3, we compute interactions between e_1 e_2, e_1 e_3 , and e_2 e_3 This can be implemented with a nested two for loops
results = [] for i in range(p - 1):
- for k in range(i + 1, p):
results.append(hp[:, :, i] * rp[:, :, k] - hp[:, :, k] * rp[:, :, i])
sigma_pp = torch.stack(results, dim=2) assert sigma_pp.shape == (b, r, int((p * (p - 1)) / 2))
Yet, this computation would be quite inefficient. Instead, we compute interactions along all p, e.g., e1e1, e1e2, e1e3,
e2e1, e2e2, e2e3, e3e1, e3e2, e3e3
Then select the triangular matrix without diagonals: e1e2, e1e3, e2e3.
- compute_sigma_qq(hq, rq)[source]
Compute
\[\sigma_{q,q}^* = \sum_{j=p+1}^{p+q-1}\sum_{j'=j+1}^{p+q}(x_jy_{j'}-x_{j'}y_j) Eq. 16\]sigma_{q} captures the interactions between along q bases For instance, let q e_1, e_2, e_3, we compute interactions between e_1 e_2, e_1 e_3 , and e_2 e_3 This can be implemented with a nested two for loops
results = [] for j in range(q - 1):
- for k in range(j + 1, q):
results.append(hq[:, :, j] * rq[:, :, k] - hq[:, :, k] * rq[:, :, j])
sigma_qq = torch.stack(results, dim=2) assert sigma_qq.shape == (b, r, int((q * (q - 1)) / 2))
Yet, this computation would be quite inefficient. Instead, we compute interactions along all p, e.g., e1e1, e1e2, e1e3,
e2e1, e2e2, e2e3, e3e1, e3e2, e3e3
Then select the triangular matrix without diagonals: e1e2, e1e3, e2e3.
- compute_sigma_rr(hk, rk)[source]
- \[\sigma_{r,r}^* = \sum_{k=p+q+1}^{p+q+r-1}\sum_{k'=k+1}^{p}(x_ky_{k'}-x_{k'}y_k)\]
- compute_sigma_pq(*, hp, hq, rp, rq)[source]
Compute
\[\sum_{i=1}^{p} \sum_{j=p+1}^{p+q} (h_i r_j - h_j r_i) e_i e_j\]results = [] sigma_pq = torch.zeros(b, r, p, q) for i in range(p):
- for j in range(q):
sigma_pq[:, :, i, j] = hp[:, :, i] * rq[:, :, j] - hq[:, :, j] * rp[:, :, i]
print(sigma_pq.shape)
- compute_sigma_pr(*, hp, hk, rp, rk)[source]
Compute
\[\sum_{i=1}^{p} \sum_{j=p+1}^{p+q} (h_i r_j - h_j r_i) e_i e_j\]results = [] sigma_pq = torch.zeros(b, r, p, q) for i in range(p):
- for j in range(q):
sigma_pq[:, :, i, j] = hp[:, :, i] * rq[:, :, j] - hq[:, :, j] * rp[:, :, i]
print(sigma_pq.shape)
- class dicee.models.BaseKGE(args: dict)[source]
Bases:
BaseKGELightning
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- args
- embedding_dim = None
- num_entities = None
- num_relations = None
- num_tokens = None
- learning_rate = None
- apply_unit_norm = None
- input_dropout_rate = None
- hidden_dropout_rate = None
- optimizer_name = None
- feature_map_dropout_rate = None
- kernel_size = None
- num_of_output_channels = None
- weight_decay = None
- loss
- selected_optimizer = None
- normalizer_class = None
- normalize_head_entity_embeddings
- normalize_relation_embeddings
- normalize_tail_entity_embeddings
- hidden_normalizer
- param_init
- input_dp_ent_real
- input_dp_rel_real
- hidden_dropout
- loss_history = []
- byte_pair_encoding
- max_length_subword_tokens
- block_size
- forward_byte_pair_encoded_triple(x: Tuple[torch.LongTensor, torch.LongTensor])[source]
byte pair encoded neural link predictors
- Parameters:
-------
- forward(x: torch.LongTensor | Tuple[torch.LongTensor, torch.LongTensor], y_idx: torch.LongTensor = None)[source]
- Parameters:
x
y_idx
ordered_bpe_entities
- class dicee.models.PykeenKGE(args: dict)[source]
Bases:
dicee.models.base_model.BaseKGE
A class for using knowledge graph embedding models implemented in Pykeen
Notes: Pykeen_DistMult: C Pykeen_ComplEx: Pykeen_QuatE: Pykeen_MuRE: Pykeen_CP: Pykeen_HolE: Pykeen_HolE:
- model_kwargs
- name
- model
- loss_history = []
- args
- entity_embeddings = None
- relation_embeddings = None
- forward_k_vs_all(x: torch.LongTensor)[source]
# => Explicit version by this we can apply bn and dropout
# (1) Retrieve embeddings of heads and relations + apply Dropout & Normalization if given. h, r = self.get_head_relation_representation(x) # (2) Reshape (1). if self.last_dim > 0:
h = h.reshape(len(x), self.embedding_dim, self.last_dim) r = r.reshape(len(x), self.embedding_dim, self.last_dim)
# (3) Reshape all entities. if self.last_dim > 0:
t = self.entity_embeddings.weight.reshape(self.num_entities, self.embedding_dim, self.last_dim)
- else:
t = self.entity_embeddings.weight
# (4) Call the score_t from interactions to generate triple scores. return self.interaction.score_t(h=h, r=r, all_entities=t, slice_size=1)
- forward_triples(x: torch.LongTensor) torch.FloatTensor [source]
# => Explicit version by this we can apply bn and dropout
# (1) Retrieve embeddings of heads, relations and tails and apply Dropout & Normalization if given. h, r, t = self.get_triple_representation(x) # (2) Reshape (1). if self.last_dim > 0:
h = h.reshape(len(x), self.embedding_dim, self.last_dim) r = r.reshape(len(x), self.embedding_dim, self.last_dim) t = t.reshape(len(x), self.embedding_dim, self.last_dim)
# (3) Compute the triple score return self.interaction.score(h=h, r=r, t=t, slice_size=None, slice_dim=0)
- class dicee.models.BaseKGE(args: dict)[source]
Bases:
BaseKGELightning
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes:
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self) -> None: super().__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call
to()
, etc.Note
As per the example above, an
__init__()
call to the parent class must be made before assignment on the child.- Variables:
training (bool) – Boolean represents whether this module is in training or evaluation mode.
- args
- embedding_dim = None
- num_entities = None
- num_relations = None
- num_tokens = None
- learning_rate = None
- apply_unit_norm = None
- input_dropout_rate = None
- hidden_dropout_rate = None
- optimizer_name = None
- feature_map_dropout_rate = None
- kernel_size = None
- num_of_output_channels = None
- weight_decay = None
- loss
- selected_optimizer = None
- normalizer_class = None
- normalize_head_entity_embeddings
- normalize_relation_embeddings
- normalize_tail_entity_embeddings
- hidden_normalizer
- param_init
- input_dp_ent_real
- input_dp_rel_real
- hidden_dropout
- loss_history = []
- byte_pair_encoding
- max_length_subword_tokens
- block_size
- forward_byte_pair_encoded_triple(x: Tuple[torch.LongTensor, torch.LongTensor])[source]
byte pair encoded neural link predictors
- Parameters:
-------
- forward(x: torch.LongTensor | Tuple[torch.LongTensor, torch.LongTensor], y_idx: torch.LongTensor = None)[source]
- Parameters:
x
y_idx
ordered_bpe_entities
- class dicee.models.FMult(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Learning Knowledge Neural Graphs
- name = 'FMult'
- entity_embeddings
- relation_embeddings
- k
- num_sample = 50
- gamma
- roots
- weights
- class dicee.models.GFMult(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Learning Knowledge Neural Graphs
- name = 'GFMult'
- entity_embeddings
- relation_embeddings
- k
- num_sample = 250
- roots
- weights
- class dicee.models.FMult2(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Learning Knowledge Neural Graphs
- name = 'FMult2'
- n_layers = 3
- k
- n = 50
- score_func = 'compositional'
- discrete_points
- entity_embeddings
- relation_embeddings
- class dicee.models.LFMult1(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Embedding with trigonometric functions. We represent all entities and relations in the complex number space as: f(x) = sum_{k=0}^{k=d-1}wk e^{kix}. and use the three differents scoring function as in the paper to evaluate the score
- name = 'LFMult1'
- entity_embeddings
- relation_embeddings
- class dicee.models.LFMult(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Embedding with polynomial functions. We represent all entities and relations in the polynomial space as: f(x) = sum_{i=0}^{d-1} a_k x^{i%d} and use the three differents scoring function as in the paper to evaluate the score. We also consider combining with Neural Networks.
- name = 'LFMult'
- entity_embeddings
- relation_embeddings
- degree
- m
- x_values
- poly_NN(x, coefh, coefr, coeft)[source]
Constructing a 2 layers NN to represent the embeddings. h = sigma(wh^T x + bh ), r = sigma(wr^T x + br ), t = sigma(wt^T x + bt )
- scalar_batch_NN(a, b, c)[source]
element wise multiplication between a,b and c: Inputs : a, b, c ====> torch.tensor of size batch_size x m x d Output : a tensor of size batch_size x d
- tri_score(coeff_h, coeff_r, coeff_t)[source]
this part implement the trilinear scoring techniques:
score(h,r,t) = int_{0}{1} h(x)r(x)t(x) dx = sum_{i,j,k = 0}^{d-1} dfrac{a_i*b_j*c_k}{1+(i+j+k)%d}
generate the range for i,j and k from [0 d-1]
2. perform dfrac{a_i*b_j*c_k}{1+(i+j+k)%d} in parallel for every batch
take the sum over each batch
- vtp_score(h, r, t)[source]
this part implement the vector triple product scoring techniques:
score(h,r,t) = int_{0}{1} h(x)r(x)t(x) dx = sum_{i,j,k = 0}^{d-1} dfrac{a_i*c_j*b_k - b_i*c_j*a_k}{(1+(i+j)%d)(1+k)}
generate the range for i,j and k from [0 d-1]
Compute the first and second terms of the sum
Multiply with then denominator and take the sum
take the sum over each batch
- comp_func(h, r, t)[source]
this part implement the function composition scoring techniques: i.e. score = <hor, t>
- polynomial(coeff, x, degree)[source]
This function takes a matrix tensor of coefficients (coeff), a tensor vector of points x and range of integer [0,1,…d] and return a vector tensor (coeff[0][0] + coeff[0][1]x +…+ coeff[0][d]x^d,
- coeff[1][0] + coeff[1][1]x +…+ coeff[1][d]x^d)
- pop(coeff, x, degree)[source]
This function allow us to evaluate the composition of two polynomes without for loops :) it takes a matrix tensor of coefficients (coeff), a matrix tensor of points x and range of integer [0,1,…d]
- and return a tensor (coeff[0][0] + coeff[0][1]x +…+ coeff[0][d]x^d,
- coeff[1][0] + coeff[1][1]x +…+ coeff[1][d]x^d)
- class dicee.models.DualE(args)[source]
Bases:
dicee.models.base_model.BaseKGE
Dual Quaternion Knowledge Graph Embeddings (https://ojs.aaai.org/index.php/AAAI/article/download/16850/16657)
- name = 'DualE'
- entity_embeddings
- relation_embeddings
- num_ent = None
- kvsall_score(e_1_h, e_2_h, e_3_h, e_4_h, e_5_h, e_6_h, e_7_h, e_8_h, e_1_t, e_2_t, e_3_t, e_4_t, e_5_t, e_6_t, e_7_t, e_8_t, r_1, r_2, r_3, r_4, r_5, r_6, r_7, r_8) torch.tensor [source]
KvsAll scoring function
Input
x: torch.LongTensor with (n, ) shape
Output
torch.FloatTensor with (n) shape
- forward_triples(idx_triple: torch.tensor) torch.tensor [source]
Negative Sampling forward pass:
Input
x: torch.LongTensor with (n, ) shape
Output
torch.FloatTensor with (n) shape