KGEModel
¶
Bases: Module
Base class of the knowledge graph embedding models.
Subclasses implement the scoring function __call__() (higher is
more plausible) and the training objective loss(). Scoring
functions broadcast over their inputs, so test() scores many
candidate tails at once.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_nodes
|
int
|
The number of nodes (entities) in the graph. |
required |
num_relations
|
int
|
The number of relations in the graph. |
required |
hidden_features_dim
|
int
|
The size of the embeddings. |
required |
Methods:
| Name | Description |
|---|---|
__call__ |
Returns the score of the given triplets. |
loss |
Returns the loss of the given (positive) triplets, against negative |
loader |
Returns a mini-batch loader of triplets. |
test |
Evaluates the model with the Mean Rank, the MRR and the Hits@k of |
random_sample |
Samples negative triplets by replacing the head of the first half |
Source code in mlx_graphs/nn/models/kge.py
__call__
¶
Returns the score of the given triplets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
head_index
|
array
|
The head indices |
required |
rel_type
|
array
|
The relation types |
required |
tail_index
|
array
|
The tail indices |
required |
Returns:
| Type | Description |
|---|---|
array
|
The scores, with the broadcast shape of the inputs. |
Source code in mlx_graphs/nn/models/kge.py
loss
¶
Returns the loss of the given (positive) triplets, against negative
triplets drawn with random_sample().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
head_index
|
array
|
The head indices |
required |
rel_type
|
array
|
The relation types |
required |
tail_index
|
array
|
The tail indices |
required |
Source code in mlx_graphs/nn/models/kge.py
loader
¶
loader(
head_index: array, rel_type: array, tail_index: array, **kwargs
) -> KGTripletLoader
Returns a mini-batch loader of triplets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
head_index
|
array
|
The head indices |
required |
rel_type
|
array
|
The relation types |
required |
tail_index
|
array
|
The tail indices |
required |
**kwargs
|
Arguments of |
{}
|
Source code in mlx_graphs/nn/models/kge.py
test
¶
test(
head_index: array,
rel_type: array,
tail_index: array,
batch_size: int,
k: int = 10,
log: bool = True,
) -> Tuple[float, float, float]
Evaluates the model with the Mean Rank, the MRR and the Hits@k of the true tail among all the possible tail entities.
The rank of a triplet is the position (0 for the best) of the true
tail among all entities sorted by decreasing score, as in PyG
(argsort(descending=True)). Entities whose score ties with the
true tail's are ordered by index, as a stable sort would: PyG's
unstable sort orders them arbitrarily, so its ranks may differ from
these when scores tie.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
head_index
|
array
|
The head indices |
required |
rel_type
|
array
|
The relation types |
required |
tail_index
|
array
|
The tail indices |
required |
batch_size
|
int
|
Maximum number of scores computed at once. Triplets are
evaluated by chunks of |
required |
k
|
int
|
The \(k\) of Hits@\(k\). Default: |
10
|
log
|
bool
|
Whether to show a progress bar. Default: |
True
|
Returns:
| Type | Description |
|---|---|
Tuple[float, float, float]
|
A tuple |
Source code in mlx_graphs/nn/models/kge.py
random_sample
¶
Samples negative triplets by replacing the head of the first half of the triplets, and the tail of the second half, with random nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
head_index
|
array
|
The head indices |
required |
rel_type
|
array
|
The relation types |
required |
tail_index
|
array
|
The tail indices |
required |
Source code in mlx_graphs/nn/models/kge.py
from mlx_graphs.nn import KGEModel · mlx_graphs/nn/models/kge.py