TransE
¶
TransE(
num_nodes: int,
num_relations: int,
hidden_features_dim: int,
margin: float = 1.0,
p_norm: float = 1.0,
)
Bases: KGEModel
The TransE model from the "Translating Embeddings for Modeling Multi-Relational Data" paper.
Relations are translations from head to tail entities, \(\mathbf{e}_h + \mathbf{e}_r \approx \mathbf{e}_t\), with the score
\[
d(h, r, t) = - {\| \mathbf{e}_h + \mathbf{e}_r - \mathbf{e}_t \|}_p
\]
where the entity embeddings are normalized to unit \(p\)-norm. It is trained with a margin ranking loss.
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 |
margin
|
float
|
The margin of the ranking loss. Default: |
1.0
|
p_norm
|
float
|
The order of the embedding and distance norms. Default:
|
1.0
|
Example:
import mlx.nn as nn
import mlx.optimizers as optim
from mlx_graphs.nn import TransE
model = TransE(num_nodes, num_relations, hidden_features_dim=50)
optimizer = optim.Adam(learning_rate=0.01)
loss_and_grad = nn.value_and_grad(model, lambda m, h, r, t: m.loss(h, r, t))
for head, rel, tail in model.loader(
head_index, rel_type, tail_index, batch_size=1000, shuffle=True
):
loss, grads = loss_and_grad(model, head, rel, tail)
optimizer.update(model, grads)
mean_rank, mrr, hits_at_10 = model.test(
test_head, test_rel, test_tail, batch_size=20000, k=10
)
Source code in mlx_graphs/nn/models/kge.py
from mlx_graphs.nn import TransE · mlx_graphs/nn/models/kge.py