pub struct TransE {
pub entity_embeddings: Vec<Vec<f64>>,
pub relation_embeddings: Vec<Vec<f64>>,
pub dim: usize,
pub norm_order: u32,
}Expand description
TransE knowledge graph embedding model.
Score: −‖ h + r − t ‖_p (negated L-p distance, higher = more likely).
Training uses margin-based loss with negative sampling.
Fields§
§entity_embeddings: Vec<Vec<f64>>Entity embedding table [n_entities, dim]
relation_embeddings: Vec<Vec<f64>>Relation embedding table [n_relations, dim]
dim: usizeEmbedding dimension
norm_order: u32Norm order (1 or 2)
Implementations§
Source§impl TransE
impl TransE
Sourcepub fn new(n_entities: usize, n_relations: usize, dim: usize) -> Result<Self>
pub fn new(n_entities: usize, n_relations: usize, dim: usize) -> Result<Self>
Create a TransE model with random initialisation.
§Arguments
n_entities– Number of entities.n_relations– Number of relation types.dim– Embedding dimension.
Sourcepub fn score(&self, h: usize, r: usize, t: usize) -> Result<f64>
pub fn score(&self, h: usize, r: usize, t: usize) -> Result<f64>
Score a single triple: −‖ h + r − t ‖ (higher = more plausible).
Sourcepub fn predict_tails(&self, h: usize, r: usize, k: usize) -> Result<Vec<usize>>
pub fn predict_tails(&self, h: usize, r: usize, k: usize) -> Result<Vec<usize>>
Return the top-k entity indices most likely to complete (h, r, ?).
Sourcepub fn link_prediction_score(&self, h: usize, r: usize, t: usize) -> Result<f64>
pub fn link_prediction_score(&self, h: usize, r: usize, t: usize) -> Result<f64>
Link prediction score for a triple (h, r, t).
Sourcepub fn train_epoch(&mut self, dataset: &KGDataset, lr: f64, margin: f64) -> f64
pub fn train_epoch(&mut self, dataset: &KGDataset, lr: f64, margin: f64) -> f64
Train for one epoch using stochastic gradient descent and negative sampling. Returns the total margin-ranking loss.
§Arguments
dataset– Training triples.lr– Learning rate.margin– Marginγfor the margin-ranking loss.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TransE
impl RefUnwindSafe for TransE
impl Send for TransE
impl Sync for TransE
impl Unpin for TransE
impl UnsafeUnpin for TransE
impl UnwindSafe for TransE
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more