Skip to main content

optirs_core/regularizers/
mod.rs

1// Regularization techniques for machine learning
2//
3// This module provides various regularization techniques commonly used in
4// machine learning to prevent overfitting, such as L1 (Lasso), L2 (Ridge),
5// ElasticNet, and Dropout.
6
7use scirs2_core::ndarray::{Array, Dimension, ScalarOperand};
8use scirs2_core::numeric::{Float, ToPrimitive};
9use std::fmt::Debug;
10
11use crate::error::{OptimError, Result};
12
13/// Fallibly converts any primitive numeric value (an `f64` literal, a `usize`
14/// count, ...) into a regularizer's generic scalar type `A`.
15///
16/// Centralizes what used to be `A::from(x).expect("unwrap failed")` /
17/// `A::from_usize(x).expect("unwrap failed")` call sites across the
18/// regularizer implementations: instead of panicking, a type that genuinely
19/// cannot represent `x` now produces an honest [`OptimError`].
20pub(crate) fn cast_scalar<A: Float, T: ToPrimitive>(value: T) -> Result<A> {
21    A::from(value).ok_or_else(|| {
22        OptimError::InvalidConfig(
23            "failed to convert a numeric value to the regularizer's scalar type".to_string(),
24        )
25    })
26}
27
28/// Trait for regularizers that can be applied to parameters and gradients
29pub trait Regularizer<A, D>
30where
31    A: Float + ScalarOperand + Debug,
32    D: Dimension,
33{
34    /// Apply regularization to parameters and gradients
35    ///
36    /// # Arguments
37    ///
38    /// * `params` - The parameters to regularize
39    /// * `gradients` - The gradients to modify
40    ///
41    /// # Returns
42    ///
43    /// The regularization penalty value
44    fn apply(&self, params: &Array<A, D>, gradients: &mut Array<A, D>) -> Result<A>;
45
46    /// Compute the regularization penalty value
47    ///
48    /// # Arguments
49    ///
50    /// * `params` - The parameters to compute the penalty for
51    ///
52    /// # Returns
53    ///
54    /// The regularization penalty value
55    fn penalty(&self, params: &Array<A, D>) -> Result<A>;
56}
57
58mod activity;
59mod dropconnect;
60mod dropout;
61mod elastic_net;
62mod entropy;
63mod group_lasso;
64mod l1;
65mod l2;
66mod label_smoothing;
67mod manifold;
68mod mixup;
69mod orthogonal;
70mod shakedrop;
71mod spatial_dropout;
72mod spectral_norm;
73mod stochastic_depth;
74mod weight_standardization;
75
76// Re-export regularizers
77pub use activity::{ActivityNorm, ActivityRegularization};
78pub use dropconnect::DropConnect;
79pub use dropout::Dropout;
80pub use elastic_net::ElasticNet;
81pub use entropy::{EntropyRegularization, EntropyRegularizerType};
82pub use group_lasso::{GroupLasso, SparsityPattern, StructuredSparsity};
83pub use l1::L1;
84pub use l2::L2;
85pub use label_smoothing::LabelSmoothing;
86pub use manifold::ManifoldRegularization;
87pub use mixup::{CutMix, MixUp};
88pub use orthogonal::OrthogonalRegularization;
89pub use shakedrop::ShakeDrop;
90pub use spatial_dropout::{FeatureDropout, SpatialDropout};
91pub use spectral_norm::SpectralNorm;
92pub use stochastic_depth::StochasticDepth;
93pub use weight_standardization::WeightStandardization;