pub struct ContrastiveLossEngine { /* private fields */ }Expand description
Contrastive loss function engine.
Implementations§
Source§impl ContrastiveLossEngine
impl ContrastiveLossEngine
Sourcepub fn new(config: ContrastiveConfig) -> Self
pub fn new(config: ContrastiveConfig) -> Self
Create a new contrastive loss engine.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create with default configuration.
Sourcepub fn info_nce_loss(
&mut self,
anchors: &[Vec<f64>],
positives: &[Vec<f64>],
negatives: &[Vec<f64>],
) -> ContrastiveLossResult
pub fn info_nce_loss( &mut self, anchors: &[Vec<f64>], positives: &[Vec<f64>], negatives: &[Vec<f64>], ) -> ContrastiveLossResult
Compute InfoNCE (Noise Contrastive Estimation) loss.
L = -log( exp(sim(anchor, positive) / τ) / Σ exp(sim(anchor, neg_i) / τ) )
Sourcepub fn triplet_loss(
&mut self,
anchors: &[Vec<f64>],
positives: &[Vec<f64>],
negatives: &[Vec<f64>],
) -> ContrastiveLossResult
pub fn triplet_loss( &mut self, anchors: &[Vec<f64>], positives: &[Vec<f64>], negatives: &[Vec<f64>], ) -> ContrastiveLossResult
Compute triplet margin loss.
L = max(0, d(anchor, positive) - d(anchor, negative) + margin)
Sourcepub fn nt_xent_loss(
&mut self,
embeddings_a: &[Vec<f64>],
embeddings_b: &[Vec<f64>],
) -> ContrastiveLossResult
pub fn nt_xent_loss( &mut self, embeddings_a: &[Vec<f64>], embeddings_b: &[Vec<f64>], ) -> ContrastiveLossResult
Compute NT-Xent (Normalised Temperature-Scaled Cross-Entropy) loss.
SimCLR-style loss over a batch of augmented pairs.
Sourcepub fn mine_semi_hard(
&self,
anchor: &[f64],
positive: &[f64],
negative_pool: &[Vec<f64>],
) -> Vec<usize>
pub fn mine_semi_hard( &self, anchor: &[f64], positive: &[f64], negative_pool: &[Vec<f64>], ) -> Vec<usize>
Mine semi-hard negatives from a pool.
Returns indices of negatives that are farther from anchor than positive but within the margin boundary.
Sourcepub fn mine_hardest(
&self,
anchor: &[f64],
negative_pool: &[Vec<f64>],
) -> Option<usize>
pub fn mine_hardest( &self, anchor: &[f64], negative_pool: &[Vec<f64>], ) -> Option<usize>
Mine the hardest negative from a pool (closest to anchor).
Sourcepub fn stats(&self) -> &ContrastiveTrainingStats
pub fn stats(&self) -> &ContrastiveTrainingStats
Get training statistics.
Sourcepub fn reset_stats(&mut self)
pub fn reset_stats(&mut self)
Reset training statistics.
Sourcepub fn config(&self) -> &ContrastiveConfig
pub fn config(&self) -> &ContrastiveConfig
Get the configuration.
Auto Trait Implementations§
impl Freeze for ContrastiveLossEngine
impl RefUnwindSafe for ContrastiveLossEngine
impl Send for ContrastiveLossEngine
impl Sync for ContrastiveLossEngine
impl Unpin for ContrastiveLossEngine
impl UnsafeUnpin for ContrastiveLossEngine
impl UnwindSafe for ContrastiveLossEngine
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> 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>
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 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,
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>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
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
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.