pub struct ComplEx { /* private fields */ }Expand description
ComplEx: complex bilinear model.
Entities and relations are complex vectors. Score is the real part of the
Hermitian dot product: Re(sum_i h_i * r_i * conj(t_i))).
Higher scores = more likely, so the Scorer implementation returns
the negated score (lower = more likely) for compatibility with
distance-based evaluation.
Initialization: Xavier uniform Uniform(-sqrt(6/dim), sqrt(6/dim)).
Reference: Trouillon et al. (2016), “Complex Embeddings for Simple Link Prediction.”
Implementations§
Source§impl ComplEx
impl ComplEx
Sourcepub fn new(num_entities: usize, num_relations: usize, dim: usize) -> Self
pub fn new(num_entities: usize, num_relations: usize, dim: usize) -> Self
Create a new ComplEx model with random initialization.
dim is the complex dimension (each embedding stores 2 * dim floats).
Xavier uniform initialization: Uniform(-sqrt(6/dim), sqrt(6/dim)).
Sourcepub fn from_vecs(
entities: Vec<Vec<f32>>,
relations: Vec<Vec<f32>>,
dim: usize,
) -> Self
pub fn from_vecs( entities: Vec<Vec<f32>>, relations: Vec<Vec<f32>>, dim: usize, ) -> Self
Create from pre-built embedding vectors.
Both entities and relations must have inner length dim * 2.
§Panics
Panics if any dimension is wrong.
Sourcepub fn entities_flat(&self) -> &[f32]
pub fn entities_flat(&self) -> &[f32]
Flat entity embeddings (row-major, stride = dim() * 2).
Sourcepub fn relations_flat(&self) -> &[f32]
pub fn relations_flat(&self) -> &[f32]
Flat relation embeddings (row-major, stride = dim() * 2).
Sourcepub fn entity_vecs(&self) -> Vec<Vec<f32>>
pub fn entity_vecs(&self) -> Vec<Vec<f32>>
Reconstruct entity embeddings as Vec<Vec<f32>>.
Sourcepub fn relation_vecs(&self) -> Vec<Vec<f32>>
pub fn relation_vecs(&self) -> Vec<Vec<f32>>
Reconstruct relation embeddings as Vec<Vec<f32>>.
Sourcepub fn score_triple(&self, head: usize, relation: usize, tail: usize) -> f32
pub fn score_triple(&self, head: usize, relation: usize, tail: usize) -> f32
Raw score: Re(sum_i h_i * r_i * conj(t_i)).
Higher = more likely. Use Scorer::score for the negated
distance-compatible version.
Entity/relation layout: first dim floats are real, next dim are imaginary.
Trait Implementations§
Source§impl Scorer for ComplEx
impl Scorer for ComplEx
Source§fn score(&self, head: usize, relation: usize, tail: usize) -> f32
fn score(&self, head: usize, relation: usize, tail: usize) -> f32
Returns -score_triple() so that lower = more likely.
Source§fn num_entities(&self) -> usize
fn num_entities(&self) -> usize
Source§fn score_all_tails(&self, head: usize, relation: usize) -> Vec<f32>
fn score_all_tails(&self, head: usize, relation: usize) -> Vec<f32>
(head, relation, ?). Read moreSource§fn score_all_heads(&self, relation: usize, tail: usize) -> Vec<f32>
fn score_all_heads(&self, relation: usize, tail: usize) -> Vec<f32>
(?, relation, tail).Source§fn top_k_tails(
&self,
head: usize,
relation: usize,
k: usize,
) -> Vec<(usize, f32)>
fn top_k_tails( &self, head: usize, relation: usize, k: usize, ) -> Vec<(usize, f32)>
(head, relation, ?). Read moreSource§fn top_k_heads(
&self,
relation: usize,
tail: usize,
k: usize,
) -> Vec<(usize, f32)>
fn top_k_heads( &self, relation: usize, tail: usize, k: usize, ) -> Vec<(usize, f32)>
(?, relation, tail).Auto Trait Implementations§
impl Freeze for ComplEx
impl RefUnwindSafe for ComplEx
impl Send for ComplEx
impl Sync for ComplEx
impl Unpin for ComplEx
impl UnsafeUnpin for ComplEx
impl UnwindSafe for ComplEx
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
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>
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>
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