dicee.models.quaternion
Classes
Base class for all Knowledge Graph Embedding models. |
|
Convolutional Quaternion Knowledge Graph Embeddings |
|
Additive Convolutional Quaternion Knowledge Graph Embeddings |
Functions
|
Module Contents
- class dicee.models.quaternion.QMult(args)[source]
Bases:
dicee.models.base_model.BaseKGEBase class for all Knowledge Graph Embedding models.
Inherits the Lightning training loop from
BaseKGELightningand adds the embedding tables, normalisation / dropout layers, and the routing logic that dispatchesforward()calls to the appropriate scoring method.Sub-classes must implement at minimum:
forward_triples()— score a batch of(h, r, t)triples.forward_k_vs_all()— score a(h, r)batch against every entity.
- Parameters:
args (dict) – Flat configuration dictionary produced by
vars(argparse.Namespace). Required keys:embedding_dim,num_entities,num_relations,learning_rate(orlr),optim,scoring_technique.
- 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.quaternion.ConvQ(args)[source]
Bases:
dicee.models.base_model.BaseKGEConvolutional Quaternion Knowledge Graph Embeddings
- name = 'ConvQ'
- entity_embeddings
- relation_embeddings
- conv2d
- fc_num_input
- fc1
- bn_conv1
- bn_conv2
- feature_map_dropout
- forward_triples(indexed_triple: torch.Tensor) torch.Tensor[source]
Score a batch of
(head, relation, tail)index triples.- Parameters:
x (torch.LongTensor) – Shape
(batch_size, 3)integer tensor where each row is[head_idx, relation_idx, tail_idx].- Returns:
Shape
(batch_size,)triple scores.- Return type:
torch.FloatTensor
- 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.quaternion.AConvQ(args)[source]
Bases:
dicee.models.base_model.BaseKGEAdditive Convolutional Quaternion Knowledge Graph Embeddings
- name = 'AConvQ'
- entity_embeddings
- relation_embeddings
- conv2d
- fc_num_input
- fc1
- bn_conv1
- bn_conv2
- feature_map_dropout
- forward_triples(indexed_triple: torch.Tensor) torch.Tensor[source]
Score a batch of
(head, relation, tail)index triples.- Parameters:
x (torch.LongTensor) – Shape
(batch_size, 3)integer tensor where each row is[head_idx, relation_idx, tail_idx].- Returns:
Shape
(batch_size,)triple scores.- Return type:
torch.FloatTensor
- 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|)