pub struct VectorQuantizer {
pub config: QuantizationConfig,
pub codebooks: Vec<Codebook>,
pub trained: bool,
pub stats: QuantizationStats,
}Expand description
Product-quantization based vector compressor.
§Example
use ipfrs_semantic::vector_quantizer::{VectorQuantizer, QuantizationConfig};
let config = QuantizationConfig::new(4, 16, 50, 1e-6);
let mut vq = VectorQuantizer::new(config);
// Train on representative data (must have >= codes_per_subspace vectors)
let training_data: Vec<Vec<f64>> = (0..32)
.map(|i| (0..16).map(|d| (i * 16 + d) as f64 * 0.01).collect())
.collect();
vq.train(&training_data).unwrap();
let code = vq.encode(&vec![0.5_f64; 16]).unwrap();
let reconstructed = vq.decode(&code).unwrap();
assert_eq!(reconstructed.len(), 16);Fields§
§config: QuantizationConfigQuantization parameters.
codebooks: Vec<Codebook>One codebook per subspace; populated after train.
trained: boolWhether the quantizer has been trained.
stats: QuantizationStatsRuntime statistics.
Implementations§
Source§impl VectorQuantizer
impl VectorQuantizer
Sourcepub fn new(config: QuantizationConfig) -> Self
pub fn new(config: QuantizationConfig) -> Self
Create a new, untrained quantizer with the given configuration.
Sourcepub fn train(&mut self, vectors: &[Vec<f64>]) -> Result<(), VqError>
pub fn train(&mut self, vectors: &[Vec<f64>]) -> Result<(), VqError>
Train the quantizer by running k-means over vectors for each subspace.
§Errors
VqError::InsufficientDatawhenvectors.len() < codes_per_subspace.VqError::DimensionMismatchwhen vectors have inconsistent length.
Sourcepub fn encode(&mut self, vector: &[f64]) -> Result<QuantizerCode, VqError>
pub fn encode(&mut self, vector: &[f64]) -> Result<QuantizerCode, VqError>
Encode a vector into a compact QuantizerCode.
For each subspace the sub-vector is mapped to the index of its nearest centroid.
§Errors
VqError::NotTrainedwhen the quantizer has not been trained.VqError::DimensionMismatchwhenvector.len()does not match training dimension.
Sourcepub fn decode(&mut self, code: &QuantizerCode) -> Result<Vec<f64>, VqError>
pub fn decode(&mut self, code: &QuantizerCode) -> Result<Vec<f64>, VqError>
Decode a QuantizerCode back into an approximate full-dimensional vector.
Reconstructs the vector by concatenating the centroid vectors from each codebook.
§Errors
VqError::NotTrainedwhen the quantizer has not been trained.VqError::InvalidCodewhen the code length does not match the number of subspaces.
Sourcepub fn encode_batch(
&mut self,
vectors: &[Vec<f64>],
) -> Result<Vec<QuantizerCode>, VqError>
pub fn encode_batch( &mut self, vectors: &[Vec<f64>], ) -> Result<Vec<QuantizerCode>, VqError>
Sourcepub fn asymmetric_distance(
&self,
query: &[f64],
code: &QuantizerCode,
) -> Result<f64, VqError>
pub fn asymmetric_distance( &self, query: &[f64], code: &QuantizerCode, ) -> Result<f64, VqError>
Compute the asymmetric squared L2 distance between a raw query vector and a code.
This is more accurate than symmetric_distance
because the query is not quantized — only the database vector is approximated.
§Errors
VqError::NotTrainedwhen not trained.VqError::DimensionMismatchwhen query length is wrong.VqError::InvalidCodewhen the code is malformed.
Sourcepub fn symmetric_distance(
&mut self,
a: &QuantizerCode,
b: &QuantizerCode,
) -> Result<f64, VqError>
pub fn symmetric_distance( &mut self, a: &QuantizerCode, b: &QuantizerCode, ) -> Result<f64, VqError>
Compute the symmetric squared L2 distance between two quantizer codes.
Both codes are decoded to full vectors before computing the distance.
This is less accurate than asymmetric_distance
but useful when the query is also stored as a code.
§Errors
VqError::NotTrainedwhen not trained.VqError::InvalidCodewhen either code is malformed.
Sourcepub fn avg_error_on_batch(
&mut self,
vectors: &[Vec<f64>],
) -> Result<f64, VqError>
pub fn avg_error_on_batch( &mut self, vectors: &[Vec<f64>], ) -> Result<f64, VqError>
Compute the mean quantization error over a batch of vectors.
§Errors
Propagates the first error encountered.
Sourcepub fn codebook_stats(&self) -> Vec<(usize, usize)>
pub fn codebook_stats(&self) -> Vec<(usize, usize)>
Return (subspace_idx, num_centroids) pairs for each codebook.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for VectorQuantizer
impl RefUnwindSafe for VectorQuantizer
impl Send for VectorQuantizer
impl Sync for VectorQuantizer
impl Unpin for VectorQuantizer
impl UnsafeUnpin for VectorQuantizer
impl UnwindSafe for VectorQuantizer
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> 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.