Expand description
Conservation Metrics for Bounded Forgetting
Provides metrics to validate that memory operations preserve important properties. Inspired by RCP’s conservation laws.
§Conservation Laws
| Law | Formula | Meaning |
|---|---|---|
| Magnitude | ‖C‖ = const | Context doesn’t disappear |
| Energy | E = ½Σᵢⱼ aᵢaⱼ cos(eᵢ, eⱼ) | Attention capacity conserved |
| Information | H = -Σ aᵢ log(aᵢ) | Shannon entropy of attention |
§Usage
use rag_plusplus_core::trajectory::ConservationMetrics;
// Example embeddings (3 vectors of dimension 4)
let embeddings_before: Vec<&[f32]> = vec![
&[1.0, 0.0, 0.0, 0.0],
&[0.0, 1.0, 0.0, 0.0],
&[0.0, 0.0, 1.0, 0.0],
];
let attention_before = vec![0.5, 0.3, 0.2];
let embeddings_after: Vec<&[f32]> = vec![
&[0.9, 0.1, 0.0, 0.0],
&[0.1, 0.9, 0.0, 0.0],
&[0.0, 0.0, 1.0, 0.0],
];
let attention_after = vec![0.5, 0.3, 0.2];
// Compute metrics before and after an operation
let before = ConservationMetrics::compute(&embeddings_before, &attention_before);
let after = ConservationMetrics::compute(&embeddings_after, &attention_after);
// Check if conservation is preserved
if before.is_conserved(&after, 0.1) {
println!("Operation preserved conservation laws");
} else {
println!("Warning: Conservation violation detected");
}Structs§
- Conservation
Config - Configuration for conservation checking.
- Conservation
Metrics - Conservation metrics for a set of embeddings with attention weights.
- Conservation
Tracker - Track conservation over time.
- Conservation
Violation - Details of a conservation violation.
Functions§
- weighted_
centroid - Compute the attention-weighted centroid of embeddings.
- weighted_
covariance - Compute the attention-weighted covariance matrix (flattened).