pub struct PqCode { /* private fields */ }Expand description
A product-quantized (PQ) code: one u8 centroid index per subvector.
Produced by Quantizer::quantize on a
trained ProductQuantizer. The byte at
position m is the index (in 0..n_centroids, where n_centroids <= 256) of the centroid in subvector codebook m that best
approximates the m-th subvector of the encoded vector. Decode it
with Quantizer::dequantize (lossy)
or compare it against a query through
Quantizer::distance.
§Examples
use iqdb_quantize::{ProductQuantizer, Quantizer};
let mut pq = ProductQuantizer::with_config(2, 4, 42);
let training: Vec<Vec<f32>> = (0..8)
.map(|i| vec![i as f32, (i * 2) as f32, (i * 3) as f32, (i * 4) as f32])
.collect();
let refs: Vec<&[f32]> = training.iter().map(Vec::as_slice).collect();
pq.train(&refs).expect("training succeeds");
let code = pq.quantize(&[1.0_f32, 2.0, 3.0, 4.0]).expect("quantize");
assert_eq!(code.n_subvectors(), 2);
assert_eq!(code.dim(), 4);
assert_eq!(code.as_bytes().len(), 2);Implementations§
Source§impl PqCode
impl PqCode
Sourcepub fn dim(&self) -> usize
pub fn dim(&self) -> usize
Returns the original vector dimension this code was produced from.
§Examples
use iqdb_quantize::{ProductQuantizer, Quantizer};
let mut pq = ProductQuantizer::with_config(2, 4, 7);
let training: Vec<Vec<f32>> = (0..8)
.map(|i| vec![i as f32, (i + 1) as f32, (i + 2) as f32, (i + 3) as f32])
.collect();
let refs: Vec<&[f32]> = training.iter().map(Vec::as_slice).collect();
pq.train(&refs).expect("training succeeds");
let code = pq.quantize(&[1.0_f32, 2.0, 3.0, 4.0]).expect("quantize");
assert_eq!(code.dim(), 4);Sourcepub fn n_subvectors(&self) -> usize
pub fn n_subvectors(&self) -> usize
Returns the number of subvectors M this code was produced under.
Equal to self.as_bytes().len() and to the
ProductQuantizer::n_subvectors
the code was produced with.
§Examples
use iqdb_quantize::{ProductQuantizer, Quantizer};
let mut pq = ProductQuantizer::with_config(2, 4, 7);
let training: Vec<Vec<f32>> = (0..8)
.map(|i| vec![i as f32, (i + 1) as f32, (i + 2) as f32, (i + 3) as f32])
.collect();
let refs: Vec<&[f32]> = training.iter().map(Vec::as_slice).collect();
pq.train(&refs).expect("training succeeds");
let code = pq.quantize(&[1.0_f32, 2.0, 3.0, 4.0]).expect("quantize");
assert_eq!(code.n_subvectors(), 2);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of bytes in the code (equal to
PqCode::n_subvectors).
§Examples
use iqdb_quantize::{ProductQuantizer, Quantizer};
let mut pq = ProductQuantizer::with_config(2, 4, 7);
let training: Vec<Vec<f32>> = (0..8)
.map(|i| vec![i as f32, (i + 1) as f32, (i + 2) as f32, (i + 3) as f32])
.collect();
let refs: Vec<&[f32]> = training.iter().map(Vec::as_slice).collect();
pq.train(&refs).expect("training succeeds");
let code = pq.quantize(&[1.0_f32, 2.0, 3.0, 4.0]).expect("quantize");
assert_eq!(code.len(), 2);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the code holds no centroid indices.
A PqCode produced by Quantizer::quantize
on a trained ProductQuantizer is never
empty (empty inputs and n_subvectors == 0 are rejected at the
boundary); this method exists for API symmetry with PqCode::len.
§Examples
use iqdb_quantize::{ProductQuantizer, Quantizer};
let mut pq = ProductQuantizer::with_config(2, 4, 7);
let training: Vec<Vec<f32>> = (0..8)
.map(|i| vec![i as f32, (i + 1) as f32, (i + 2) as f32, (i + 3) as f32])
.collect();
let refs: Vec<&[f32]> = training.iter().map(Vec::as_slice).collect();
pq.train(&refs).expect("training succeeds");
let code = pq.quantize(&[1.0_f32, 2.0, 3.0, 4.0]).expect("quantize");
assert!(!code.is_empty());Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Borrow the raw centroid-index bytes.
§Examples
use iqdb_quantize::{ProductQuantizer, Quantizer};
let mut pq = ProductQuantizer::with_config(2, 4, 7);
let training: Vec<Vec<f32>> = (0..8)
.map(|i| vec![i as f32, (i + 1) as f32, (i + 2) as f32, (i + 3) as f32])
.collect();
let refs: Vec<&[f32]> = training.iter().map(Vec::as_slice).collect();
pq.train(&refs).expect("training succeeds");
let code = pq.quantize(&[1.0_f32, 2.0, 3.0, 4.0]).expect("quantize");
assert_eq!(code.as_bytes().len(), 2);