pub struct SparseMap { /* private fields */ }Expand description
A SparseMap layer: structured prediction with sparse marginals.
Solves the QP:
max θᵀμ - ½ μᵀ μ s.t. μ ∈ M
where M is the marginal polytope (e.g., the simplex for unstructured classification). The solution is a sparse probability distribution.
The backward pass uses the KKT conditions of the QP.
Implementations§
Source§impl SparseMap
impl SparseMap
Sourcepub fn new(a_marginal: Vec<Vec<f64>>, b_marginal: Vec<f64>) -> Self
pub fn new(a_marginal: Vec<Vec<f64>>, b_marginal: Vec<f64>) -> Self
Create a new SparseMap layer for a given marginal polytope.
§Arguments
a_marginal– equality constraints defining the polytope (Ax = b, x ≥ 0).b_marginal– equality rhs.
Sourcepub fn with_config(
a_marginal: Vec<Vec<f64>>,
b_marginal: Vec<f64>,
config: SparseMapConfig,
) -> Self
pub fn with_config( a_marginal: Vec<Vec<f64>>, b_marginal: Vec<f64>, config: SparseMapConfig, ) -> Self
Create a SparseMap with custom configuration.
Sourcepub fn forward(&mut self, theta: &[f64]) -> OptimizeResult<Vec<f64>>
pub fn forward(&mut self, theta: &[f64]) -> OptimizeResult<Vec<f64>>
Forward pass: solve the QP on the marginal polytope.
μ* = argmax_{μ ∈ M} θᵀμ - ½ ||μ||² = argmin_{μ ∈ M} ½ ||μ - θ||² = Π_M(θ) (Euclidean projection onto M)
Uses iterative projected gradient on the Lagrangian.
§Arguments
theta– score vector (length n).
Sourcepub fn backward(&self, dl_dmu: &[f64]) -> OptimizeResult<Vec<f64>>
pub fn backward(&self, dl_dmu: &[f64]) -> OptimizeResult<Vec<f64>>
Backward pass: compute dL/dθ via KKT sensitivity.
At the optimal μ*, the KKT conditions of the QP are:
μ* - θ + Aᵀν* + s = 0 (stationarity, s = -min(μ*, 0)) Aμ* = b (equality) μ* ≥ 0, s ≥ 0, s⊙μ* = 0 (complementarity)
For the active variables (μ*_i > 0), we have s_i = 0, and the KKT system reduces to an equality system on the support.
§Arguments
dl_dmu– upstream gradient dL/dμ (length n).
Sourcepub fn project_simplex(v: &[f64]) -> Vec<f64>
pub fn project_simplex(v: &[f64]) -> Vec<f64>
Project a vector onto the probability simplex.
Solves: argmin_{μ ≥ 0, Σμ = 1} ||μ - v||²
Uses the O(n log n) sorting algorithm.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for SparseMap
impl RefUnwindSafe for SparseMap
impl Send for SparseMap
impl Sync for SparseMap
impl Unpin for SparseMap
impl UnsafeUnpin for SparseMap
impl UnwindSafe for SparseMap
Blanket Implementations§
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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
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.