Skip to main content

sklears_decomposition/dictionary_learning/
minibatch_dictionary_learning.rs

1//! Mini-batch Dictionary Learning algorithms
2
3use scirs2_core::ndarray::Array2;
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6use sklears_core::{
7    error::{Result, SklearsError},
8    traits::{Estimator, Fit, Trained, Transform, Untrained},
9    types::Float,
10};
11
12/// Mini-batch dictionary learning configuration
13#[derive(Debug, Clone)]
14#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
15pub struct MiniBatchConfig {
16    pub n_components: usize,
17    pub batch_size: usize,
18    pub max_iter: usize,
19}
20
21impl Default for MiniBatchConfig {
22    fn default() -> Self {
23        Self {
24            n_components: 100,
25            batch_size: 10,
26            max_iter: 1000,
27        }
28    }
29}
30
31/// Mini-batch dictionary learning result
32#[derive(Debug, Clone)]
33#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
34pub struct MiniBatchResult {
35    pub components: Array2<Float>,
36    pub n_iter: usize,
37}
38
39/// Mini-batch Dictionary Learning estimator
40#[derive(Debug, Clone)]
41#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
42pub struct MiniBatchDictionaryLearning<State = Untrained> {
43    config: MiniBatchConfig,
44    state: std::marker::PhantomData<State>,
45    /// Dictionary components - only available when trained
46    components_: Option<Array2<Float>>,
47}
48
49impl MiniBatchDictionaryLearning<Untrained> {
50    pub fn new(config: MiniBatchConfig) -> Self {
51        Self {
52            config,
53            state: std::marker::PhantomData,
54            components_: None,
55        }
56    }
57}
58
59impl MiniBatchDictionaryLearning<Trained> {
60    /// Get the dictionary components
61    pub fn components(&self) -> &Array2<Float> {
62        self.components_.as_ref().expect("Model is trained")
63    }
64}
65
66impl Estimator for MiniBatchDictionaryLearning<Untrained> {
67    type Config = MiniBatchConfig;
68    type Error = SklearsError;
69    type Float = Float;
70
71    fn config(&self) -> &Self::Config {
72        &self.config
73    }
74}
75
76impl Fit<Array2<Float>, ()> for MiniBatchDictionaryLearning<Untrained> {
77    type Fitted = MiniBatchDictionaryLearning<Trained>;
78
79    fn fit(self, x: &Array2<Float>, _y: &()) -> Result<Self::Fitted> {
80        let (_n_samples, n_features) = x.dim();
81        let components = Array2::zeros((self.config.n_components, n_features));
82
83        Ok(MiniBatchDictionaryLearning {
84            config: self.config,
85            state: std::marker::PhantomData,
86            components_: Some(components),
87        })
88    }
89}
90
91impl Transform<Array2<Float>, Array2<Float>> for MiniBatchDictionaryLearning<Trained> {
92    fn transform(&self, x: &Array2<Float>) -> Result<Array2<Float>> {
93        let (n_samples, _n_features) = x.dim();
94        let codes = Array2::zeros((n_samples, self.config.n_components));
95        Ok(codes)
96    }
97}
98
99// Type alias for backward compatibility
100pub type TrainedMiniBatchDictionaryLearning = MiniBatchDictionaryLearning<Trained>;