pub struct GatEmbedder { /* private fields */ }Expand description
Multi-head graph attention network embedder trained on RDF triple lists.
Architecture follows Veličković et al. (2018): for each node the model
attends over its in-neighbourhood using learned Q/K/V projections,
applies LeakyReLU-gated softmax attention, concatenates the num_heads
outputs, projects through W_out, applies ReLU, and L2-normalises.
Training uses a margin-ranking loss and sign-SGD with gradient clipping.
Implementations§
Source§impl GatEmbedder
impl GatEmbedder
Sourcepub fn new(config: GatEmbedderConfig) -> Self
pub fn new(config: GatEmbedderConfig) -> Self
Create a new, un-trained GAT embedder.
Sourcepub fn fit(
&mut self,
triples: &[(String, String, String)],
) -> Result<(), EmbeddingError>
pub fn fit( &mut self, triples: &[(String, String, String)], ) -> Result<(), EmbeddingError>
Train on (subject_iri, predicate_iri, object_iri) triples.
Steps:
- Build entity index and adjacency map (undirected: s→o and o→s).
- Xavier-initialise all Q/K/V and W_out weight matrices.
- Initialise random L2-normalised entity feature vectors.
- For each epoch: attention forward-pass → margin-ranking loss → sign-SGD weight update.
- Cache final embeddings.
Sourcepub fn embed_entity(&self, entity: &str) -> Vec<f64>
pub fn embed_entity(&self, entity: &str) -> Vec<f64>
Return the embedding for a known entity IRI. Returns a zero vector for unseen entities (inductive fallback — never panics).
Sourcepub fn attention_forward(
&self,
entity_idx: usize,
adj: &HashMap<usize, Vec<usize>>,
embeddings: &[Vec<f64>],
layer_idx: usize,
) -> Vec<f64>
pub fn attention_forward( &self, entity_idx: usize, adj: &HashMap<usize, Vec<usize>>, embeddings: &[Vec<f64>], layer_idx: usize, ) -> Vec<f64>
Multi-head attention forward pass for a single entity.
For each head h:
Q_h = W_query[h] · e_i
K_h = W_key[h] · e_j for each neighbour j
V_h = W_value[h] · e_j
score_j = LeakyReLU(Q_h · K_h_j / √head_dim)
α_j = softmax({score_j})
head_out_h = Σ_j α_j · V_h_j
Heads are concatenated → projected by W_out → ReLU → L2-normalised.
Sourcepub fn is_trained(&self) -> bool
pub fn is_trained(&self) -> bool
Whether fit has been called successfully.
Sourcepub fn num_entities(&self) -> usize
pub fn num_entities(&self) -> usize
Number of distinct entities seen during training.
Sourcepub fn embedding_dim(&self) -> usize
pub fn embedding_dim(&self) -> usize
Dimension of each output embedding.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for GatEmbedder
impl RefUnwindSafe for GatEmbedder
impl Send for GatEmbedder
impl Sync for GatEmbedder
impl Unpin for GatEmbedder
impl UnsafeUnpin for GatEmbedder
impl UnwindSafe for GatEmbedder
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.