sklears_decomposition/dictionary_learning/
ksvd_algorithms.rs1use scirs2_core::ndarray::Array2;
4#[cfg(feature = "serde")]
5use serde::{Deserialize, Serialize};
6use sklears_core::{error::Result, types::Float};
7
8#[derive(Debug, Clone)]
10#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
11pub struct KSVDConfig {
12 pub max_iter: usize,
14 pub tol: Float,
16 pub n_nonzero_coefs: usize,
18}
19
20impl Default for KSVDConfig {
21 fn default() -> Self {
22 Self {
23 max_iter: 100,
24 tol: 1e-6,
25 n_nonzero_coefs: 10,
26 }
27 }
28}
29
30#[derive(Debug, Clone)]
32#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
33pub struct KSVDResult {
34 pub dictionary: Array2<Float>,
36 pub codes: Array2<Float>,
38 pub reconstruction_error: Float,
40 pub n_iter: usize,
42}
43
44#[derive(Debug, Clone)]
46#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
47pub struct KSVDEncoder {
48 config: KSVDConfig,
49}
50
51impl KSVDEncoder {
52 pub fn new(config: KSVDConfig) -> Self {
53 Self { config }
54 }
55
56 pub fn encode(
58 &self,
59 data: &Array2<Float>,
60 initial_dictionary: &Array2<Float>,
61 ) -> Result<KSVDResult> {
62 let (n_samples, _n_features) = data.dim();
63 let n_atoms = initial_dictionary.nrows();
64
65 let dictionary = initial_dictionary.clone();
67 let codes = Array2::zeros((n_atoms, n_samples));
68 let reconstruction_error = 0.0;
69
70 Ok(KSVDResult {
71 dictionary,
72 codes,
73 reconstruction_error,
74 n_iter: 0,
75 })
76 }
77}