pub struct EmbeddingFinetuner {
pub config: FinetunerConfig,
pub layer: ProjectionLayer,
pub training_history: Vec<TrainingStats>,
pub total_pairs_seen: u64,
pub rng_state: u64,
}Expand description
Contrastive embedding fine-tuner using triplet loss.
§Example
use ipfrs_semantic::embedding_finetuner::{
EmbeddingFinetuner, FinetunerConfig, TrainingPair,
};
let config = FinetunerConfig::new(4, 4);
let mut finetuner = EmbeddingFinetuner::new(config);
let pairs = vec![
TrainingPair::new(vec![1.0, 0.0, 0.0, 0.0],
vec![0.9, 0.1, 0.0, 0.0],
vec![0.0, 0.0, 1.0, 0.0]),
];
let history = finetuner.train(&pairs);
assert!(!history.is_empty());Fields§
§config: FinetunerConfigHyper-parameters.
layer: ProjectionLayerLinear projection layer.
training_history: Vec<TrainingStats>Epoch-level statistics accumulated across all train calls.
total_pairs_seen: u64Running count of training pairs consumed.
rng_state: u64Internal xorshift64 PRNG state.
Implementations§
Source§impl EmbeddingFinetuner
impl EmbeddingFinetuner
Sourcepub fn new(config: FinetunerConfig) -> Self
pub fn new(config: FinetunerConfig) -> Self
Construct a new fine-tuner, initialising weights with Xavier uniform.
Sourcepub fn project(&self, embedding: &[f64]) -> Result<Vec<f64>, FinetunerError>
pub fn project(&self, embedding: &[f64]) -> Result<Vec<f64>, FinetunerError>
Project a single embedding through the linear layer.
Sourcepub fn l2_distance_sq(a: &[f64], b: &[f64]) -> f64
pub fn l2_distance_sq(a: &[f64], b: &[f64]) -> f64
Squared L2 distance between two equal-length slices.
Panics in debug if lengths differ; silently stops at the shorter length in release (caller must ensure equal lengths).
Sourcepub fn train_step(&mut self, pairs: &[TrainingPair]) -> TrainingStats
pub fn train_step(&mut self, pairs: &[TrainingPair]) -> TrainingStats
Run a single mini-batch training step and return statistics.
For each pair in pairs:
- Project anchor, positive, negative.
- Compute triplet loss.
- If loss > 0, compute simplified gradient and update weights.
- Apply L2 regularisation:
w ← w - l2_reg * w.
Sourcepub fn train(&mut self, pairs: &[TrainingPair]) -> Vec<TrainingStats>
pub fn train(&mut self, pairs: &[TrainingPair]) -> Vec<TrainingStats>
Full training loop over max_epochs, processing batch_size pairs per step.
Pairs are shuffled at the start of each epoch using Fisher-Yates with the internal xorshift64 PRNG.
Returns the per-epoch TrainingStats (also appended to self.training_history).
Sourcepub fn encode_batch(
&self,
embeddings: &[Vec<f64>],
) -> Result<Vec<Vec<f64>>, FinetunerError>
pub fn encode_batch( &self, embeddings: &[Vec<f64>], ) -> Result<Vec<Vec<f64>>, FinetunerError>
Project a batch of embeddings.
Sourcepub fn similarity(&self, a: &[f64], b: &[f64]) -> f64
pub fn similarity(&self, a: &[f64], b: &[f64]) -> f64
Cosine similarity between the projected versions of a and b.
Falls back to the cosine of the raw vectors if projection fails.
Sourcepub fn evaluate_pairs(&self, pairs: &[TrainingPair]) -> (f64, f64)
pub fn evaluate_pairs(&self, pairs: &[TrainingPair]) -> (f64, f64)
Evaluate a set of triplets, returning (avg_loss, fraction_correct).
A triplet is “correct” when d(proj_a, proj_p) < d(proj_a, proj_n).
Sourcepub fn training_history(&self) -> &[TrainingStats]
pub fn training_history(&self) -> &[TrainingStats]
Read-only view of accumulated per-epoch statistics.
Trait Implementations§
Source§impl Clone for EmbeddingFinetuner
impl Clone for EmbeddingFinetuner
Source§fn clone(&self) -> EmbeddingFinetuner
fn clone(&self) -> EmbeddingFinetuner
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for EmbeddingFinetuner
impl RefUnwindSafe for EmbeddingFinetuner
impl Send for EmbeddingFinetuner
impl Sync for EmbeddingFinetuner
impl Unpin for EmbeddingFinetuner
impl UnsafeUnpin for EmbeddingFinetuner
impl UnwindSafe for EmbeddingFinetuner
Blanket Implementations§
impl<T> Allocation for T
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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
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.