-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompressed_scoring.rs
More file actions
56 lines (48 loc) · 1.95 KB
/
Copy pathcompressed_scoring.rs
File metadata and controls
56 lines (48 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//! Compressed-vs-compressed scoring: compare two vector groups without decompressing.
//!
//! Run with: `cargo run --example compressed_scoring`
use qjl_sketch::sketch::QJLSketch;
use rand::SeedableRng;
use rand_chacha::ChaCha20Rng;
use rand_distr::{Distribution, StandardNormal};
fn random_vec(d: usize, rng: &mut ChaCha20Rng) -> Vec<f32> {
let normal: StandardNormal = StandardNormal;
(0..d)
.map(|_| {
let v: f64 = normal.sample(rng);
v as f32
})
.collect()
}
fn main() {
let d = 64;
let s = 256;
let sketch = QJLSketch::new(d, s, s, 42).unwrap();
let mut rng = ChaCha20Rng::seed_from_u64(200);
// Compress two sets of vectors (e.g. two vector groups)
let num = 8;
let group_a: Vec<f32> = (0..num).flat_map(|_| random_vec(d, &mut rng)).collect();
let group_b: Vec<f32> = (0..num).flat_map(|_| random_vec(d, &mut rng)).collect();
let outliers = vec![0u8];
let ca = sketch.quantize(&group_a, num, &outliers).unwrap();
let cb = sketch.quantize(&group_b, num, &outliers).unwrap();
// Batch scoring: a[i] vs b[i]
let batch_scores = sketch.score_compressed(&ca, &cb).unwrap();
println!("Batch scores (a[i] vs b[i]):");
for (i, s) in batch_scores.iter().enumerate() {
println!(" pair {i}: {s:+.4}");
}
// Cross-pair scoring: any a[i] vs any b[j]
println!("\nCross-pair scores (a[0] vs each b[j]):");
for j in 0..num {
let s = sketch.score_compressed_pair(&ca, 0, &cb, j).unwrap();
println!(" a[0] vs b[{j}]: {s:+.4}");
}
// Self-similarity: a[i] vs a[i] should be high (≈ ||v||²)
let self_scores = sketch.score_compressed(&ca, &ca).unwrap();
println!("\nSelf-similarity (a[i] vs a[i]):");
for (i, s) in self_scores.iter().enumerate() {
let exact_norm_sq: f32 = group_a[i * d..(i + 1) * d].iter().map(|x| x * x).sum();
println!(" vec {i}: score={s:.4}, ||v||²={exact_norm_sq:.4}");
}
}