sklears-neural 0.2.0

Neural network implementations for the sklears machine learning library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
//! Variational Autoencoder (VAE) implementation for generative modeling
//!
//! This module provides a Variational Autoencoder implementation that can learn
//! probabilistic representations of data and generate new samples from the learned
//! distribution.
//!
//! # Theory
//!
//! A VAE consists of:
//! - An encoder that maps input x to latent variable z ~ N(μ, σ²)
//! - A decoder that reconstructs x from z
//! - A KL divergence term that regularizes the latent space
//!
//! The loss function is: L = reconstruction_loss + β * KL_divergence
//!
//! # Example
//!
//! ```rust
//! use sklears_neural::vae::{VAE, VAEConfig};
//! use scirs2_core::ndarray::Array2;
//!
//! let config = VAEConfig::default()
//!     .latent_dim(32)
//!     .beta(1.0);
//!
//! let vae = VAE::new(config);
//! ```

use crate::activation::Activation;
use crate::utils::{initialize_weights, WeightInit};
use crate::{NeuralResult, SklearsError};
use scirs2_core::ndarray::{Array1, Array2};
use scirs2_core::random::{thread_rng, StandardNormal};
use sklears_core::{
    traits::{Estimator, Fit, Trained, Transform, Untrained},
    types::Float,
};
use std::marker::PhantomData;

/// VAE configuration parameters
#[derive(Debug, Clone)]
pub struct VAEConfig {
    /// Dimension of the latent space
    pub latent_dim: usize,
    /// Encoder hidden layer sizes
    pub encoder_layers: Vec<usize>,
    /// Decoder hidden layer sizes
    pub decoder_layers: Vec<usize>,
    /// Activation function
    pub activation: Activation,
    /// Learning rate
    pub learning_rate: Float,
    /// Number of training epochs
    pub n_epochs: usize,
    /// Batch size
    pub batch_size: usize,
    /// KL divergence weight (β parameter)
    pub beta: Float,
    /// Random seed
    pub random_state: Option<u64>,
    /// Weight initialization strategy
    pub weight_init: WeightInit,
}

impl Default for VAEConfig {
    fn default() -> Self {
        Self {
            latent_dim: 32,
            encoder_layers: vec![128, 64],
            decoder_layers: vec![64, 128],
            activation: Activation::Relu,
            learning_rate: 0.001,
            n_epochs: 100,
            batch_size: 32,
            beta: 1.0,
            random_state: None,
            weight_init: WeightInit::Xavier,
        }
    }
}

impl VAEConfig {
    /// Set latent dimension
    pub fn latent_dim(mut self, dim: usize) -> Self {
        self.latent_dim = dim;
        self
    }

    /// Set encoder layer sizes
    pub fn encoder_layers(mut self, layers: Vec<usize>) -> Self {
        self.encoder_layers = layers;
        self
    }

    /// Set decoder layer sizes
    pub fn decoder_layers(mut self, layers: Vec<usize>) -> Self {
        self.decoder_layers = layers;
        self
    }

    /// Set KL divergence weight
    pub fn beta(mut self, beta: Float) -> Self {
        self.beta = beta;
        self
    }

    /// Set learning rate
    pub fn learning_rate(mut self, lr: Float) -> Self {
        self.learning_rate = lr;
        self
    }

    /// Set number of epochs
    pub fn n_epochs(mut self, epochs: usize) -> Self {
        self.n_epochs = epochs;
        self
    }
}

/// Variational Autoencoder model
#[derive(Debug, Clone)]
#[allow(dead_code)] // n_features_in retained for input dimension validation during inference
pub struct VAE<State = Untrained> {
    config: VAEConfig,
    state: PhantomData<State>,
    // Encoder weights and biases
    encoder_weights: Option<Vec<Array2<Float>>>,
    encoder_biases: Option<Vec<Array1<Float>>>,
    // Mean and logvar layers (for reparameterization)
    mean_weights: Option<Array2<Float>>,
    mean_bias: Option<Array1<Float>>,
    logvar_weights: Option<Array2<Float>>,
    logvar_bias: Option<Array1<Float>>,
    // Decoder weights and biases
    decoder_weights: Option<Vec<Array2<Float>>>,
    decoder_biases: Option<Vec<Array1<Float>>>,
    // Model metadata
    n_features_in: Option<usize>,
}

#[allow(dead_code)] // Implementation methods pre-written for v0.2.0; fit() not yet calls them
impl VAE<Untrained> {
    /// Create a new VAE with the given configuration
    pub fn new(config: VAEConfig) -> Self {
        Self {
            config,
            state: PhantomData,
            encoder_weights: None,
            encoder_biases: None,
            mean_weights: None,
            mean_bias: None,
            logvar_weights: None,
            logvar_bias: None,
            decoder_weights: None,
            decoder_biases: None,
            n_features_in: None,
        }
    }

    /// Initialize the VAE weights
    fn initialize_weights<R: scirs2_core::random::Rng>(
        &mut self,
        n_features: usize,
        rng: &mut R,
    ) -> NeuralResult<()> {
        let mut layer_sizes = vec![n_features];
        layer_sizes.extend(&self.config.encoder_layers);

        // Initialize encoder weights
        let mut encoder_weights = Vec::new();
        let mut encoder_biases = Vec::new();

        for i in 0..layer_sizes.len() - 1 {
            let weights = initialize_weights(
                layer_sizes[i + 1],
                layer_sizes[i],
                &self.config.weight_init,
                rng,
            );
            let biases = Array1::zeros(layer_sizes[i + 1]);

            encoder_weights.push(weights);
            encoder_biases.push(biases);
        }

        // Initialize mean and logvar layers
        let last_encoder_size = layer_sizes
            .last()
            .copied()
            .expect("value should be present");
        self.mean_weights = Some(initialize_weights(
            self.config.latent_dim,
            last_encoder_size,
            &self.config.weight_init,
            rng,
        ));
        self.mean_bias = Some(Array1::zeros(self.config.latent_dim));

        self.logvar_weights = Some(initialize_weights(
            self.config.latent_dim,
            last_encoder_size,
            &self.config.weight_init,
            rng,
        ));
        self.logvar_bias = Some(Array1::zeros(self.config.latent_dim));

        // Initialize decoder weights
        let mut decoder_layer_sizes = vec![self.config.latent_dim];
        decoder_layer_sizes.extend(&self.config.decoder_layers);
        decoder_layer_sizes.push(n_features);

        let mut decoder_weights = Vec::new();
        let mut decoder_biases = Vec::new();

        for i in 0..decoder_layer_sizes.len() - 1 {
            let weights = initialize_weights(
                decoder_layer_sizes[i + 1],
                decoder_layer_sizes[i],
                &self.config.weight_init,
                rng,
            );
            let biases = Array1::zeros(decoder_layer_sizes[i + 1]);

            decoder_weights.push(weights);
            decoder_biases.push(biases);
        }

        self.encoder_weights = Some(encoder_weights);
        self.encoder_biases = Some(encoder_biases);
        self.decoder_weights = Some(decoder_weights);
        self.decoder_biases = Some(decoder_biases);
        self.n_features_in = Some(n_features);

        Ok(())
    }

    /// Apply activation function
    fn apply_activation(&self, x: &Array2<Float>) -> Array2<Float> {
        match self.config.activation {
            Activation::Relu => x.mapv(|v| v.max(0.0)),
            Activation::Tanh => x.mapv(|v| v.tanh()),
            Activation::Logistic => x.mapv(|v| 1.0 / (1.0 + (-v).exp())),
            _ => x.clone(),
        }
    }

    /// Encode input to mean and logvar
    fn encode(&self, x: &Array2<Float>) -> NeuralResult<(Array2<Float>, Array2<Float>)> {
        let encoder_weights = self
            .encoder_weights
            .as_ref()
            .ok_or_else(|| SklearsError::InvalidInput("Model not initialized".to_string()))?;
        let encoder_biases = self
            .encoder_biases
            .as_ref()
            .expect("encoder_biases not available - model not fitted");

        let mut activations = x.clone();

        // Forward through encoder
        for (weights, biases) in encoder_weights.iter().zip(encoder_biases.iter()) {
            activations = activations.dot(&weights.t()) + biases;
            activations = self.apply_activation(&activations);
        }

        // Compute mean and logvar
        let mean_weights = self
            .mean_weights
            .as_ref()
            .expect("mean_weights not available - model not fitted");
        let mean_bias = self
            .mean_bias
            .as_ref()
            .expect("mean_bias not available - model not fitted");
        let mean = activations.dot(&mean_weights.t()) + mean_bias;

        let logvar_weights = self
            .logvar_weights
            .as_ref()
            .expect("logvar_weights not available - model not fitted");
        let logvar_bias = self
            .logvar_bias
            .as_ref()
            .expect("logvar_bias not available - model not fitted");
        let logvar = activations.dot(&logvar_weights.t()) + logvar_bias;

        Ok((mean, logvar))
    }

    /// Reparameterization trick: z = μ + σ * ε, where ε ~ N(0,1)
    fn reparameterize(&self, mean: &Array2<Float>, logvar: &Array2<Float>) -> Array2<Float> {
        let mut rng = thread_rng();
        let epsilon =
            Array2::from_shape_simple_fn(mean.dim(), || rng.sample::<f64, _>(StandardNormal));
        let std = logvar.mapv(|x| (x * 0.5).exp());
        mean + &std * &epsilon
    }

    /// Decode latent representation to reconstruction
    fn decode(&self, z: &Array2<Float>) -> NeuralResult<Array2<Float>> {
        let decoder_weights = self
            .decoder_weights
            .as_ref()
            .ok_or_else(|| SklearsError::InvalidInput("Model not initialized".to_string()))?;
        let decoder_biases = self
            .decoder_biases
            .as_ref()
            .expect("decoder_biases not available - model not fitted");

        let mut activations = z.clone();

        // Forward through decoder (except last layer)
        for (weights, biases) in decoder_weights
            .iter()
            .zip(decoder_biases.iter())
            .take(decoder_weights.len() - 1)
        {
            activations = activations.dot(&weights.t()) + biases;
            activations = self.apply_activation(&activations);
        }

        // Last layer (reconstruction) - typically no activation or sigmoid
        let last_weights = decoder_weights.last().expect("empty collection");
        let last_bias = decoder_biases.last().expect("empty collection");
        activations = activations.dot(&last_weights.t()) + last_bias;

        // Apply sigmoid for bounded outputs (0-1)
        activations = activations.mapv(|x| 1.0 / (1.0 + (-x).exp()));

        Ok(activations)
    }

    /// Compute VAE loss: reconstruction loss + β * KL divergence
    fn compute_loss(
        &self,
        x: &Array2<Float>,
        x_recon: &Array2<Float>,
        mean: &Array2<Float>,
        logvar: &Array2<Float>,
    ) -> Float {
        // Reconstruction loss (MSE)
        let recon_loss = (x - x_recon)
            .mapv(|x| x.powi(2))
            .mean()
            .expect("mean should not fail on non-empty array");

        // KL divergence: -0.5 * sum(1 + log(σ²) - μ² - σ²)
        let kl_div = -0.5
            * (1.0 + logvar - mean.mapv(|x| x.powi(2)) - logvar.mapv(|x| x.exp()))
                .mean()
                .expect("value should be present");

        recon_loss + self.config.beta * kl_div
    }
}

impl Estimator for VAE<Untrained> {
    type Config = VAEConfig;
    type Error = SklearsError;
    type Float = Float;

    fn config(&self) -> &Self::Config {
        &self.config
    }
}

/// # Note
///
/// Not implemented in v0.1.0. Returns `Err(NotImplemented)`. Planned for v0.2.0.
/// VAE training requires gradient-based optimization of the ELBO (evidence lower
/// bound), including backpropagation through the reparameterization trick. The
/// current implementation does not perform weight updates.
impl Fit<Array2<Float>, ()> for VAE<Untrained> {
    type Fitted = VAE<Trained>;

    fn fit(self, x: &Array2<Float>, _y: &()) -> NeuralResult<Self::Fitted> {
        let (n_samples, n_features) = x.dim();

        if n_samples == 0 || n_features == 0 {
            return Err(SklearsError::InvalidInput(
                "Input data cannot be empty".to_string(),
            ));
        }

        let _ = (n_samples, n_features);

        Err(SklearsError::NotImplemented(
            "VAE training not yet implemented: gradient-based ELBO optimization \
             with reparameterization trick is planned for v0.2.0"
                .to_string(),
        ))
    }
}

impl VAE<Trained> {
    /// Generate new samples from the learned distribution
    pub fn generate(&self, n_samples: usize) -> NeuralResult<Array2<Float>> {
        let mut rng = thread_rng();
        let z = Array2::from_shape_simple_fn((n_samples, self.config.latent_dim), || {
            rng.sample::<f64, _>(StandardNormal)
        });

        self.decode(&z)
    }

    /// Encode input data to latent space
    pub fn encode_to_latent(&self, x: &Array2<Float>) -> NeuralResult<Array2<Float>> {
        let (mean, logvar) = self.encode(x)?;
        Ok(self.reparameterize(&mean, &logvar))
    }

    /// Reconstruct input data
    pub fn reconstruct(&self, x: &Array2<Float>) -> NeuralResult<Array2<Float>> {
        let z = self.encode_to_latent(x)?;
        self.decode(&z)
    }
}

impl Transform<Array2<Float>> for VAE<Trained> {
    fn transform(&self, x: &Array2<Float>) -> NeuralResult<Array2<Float>> {
        self.encode_to_latent(x)
    }
}

// Re-implement encoding methods for trained model
impl VAE<Trained> {
    fn encode(&self, x: &Array2<Float>) -> NeuralResult<(Array2<Float>, Array2<Float>)> {
        let encoder_weights = self
            .encoder_weights
            .as_ref()
            .ok_or_else(|| SklearsError::InvalidInput("Model not trained".to_string()))?;
        let encoder_biases = self
            .encoder_biases
            .as_ref()
            .expect("encoder_biases not available - model not fitted");

        let mut activations = x.clone();

        // Forward through encoder
        for (weights, biases) in encoder_weights.iter().zip(encoder_biases.iter()) {
            activations = activations.dot(&weights.t()) + biases;
            activations = self.apply_activation(&activations);
        }

        // Compute mean and logvar
        let mean_weights = self
            .mean_weights
            .as_ref()
            .expect("mean_weights not available - model not fitted");
        let mean_bias = self
            .mean_bias
            .as_ref()
            .expect("mean_bias not available - model not fitted");
        let mean = activations.dot(&mean_weights.t()) + mean_bias;

        let logvar_weights = self
            .logvar_weights
            .as_ref()
            .expect("logvar_weights not available - model not fitted");
        let logvar_bias = self
            .logvar_bias
            .as_ref()
            .expect("logvar_bias not available - model not fitted");
        let logvar = activations.dot(&logvar_weights.t()) + logvar_bias;

        Ok((mean, logvar))
    }

    fn apply_activation(&self, x: &Array2<Float>) -> Array2<Float> {
        match self.config.activation {
            Activation::Relu => x.mapv(|v| v.max(0.0)),
            Activation::Tanh => x.mapv(|v| v.tanh()),
            Activation::Logistic => x.mapv(|v| 1.0 / (1.0 + (-v).exp())),
            _ => x.clone(),
        }
    }

    fn reparameterize(&self, mean: &Array2<Float>, logvar: &Array2<Float>) -> Array2<Float> {
        let mut rng = thread_rng();
        let epsilon =
            Array2::from_shape_simple_fn(mean.dim(), || rng.sample::<f64, _>(StandardNormal));
        let std = logvar.mapv(|x| (x * 0.5).exp());
        mean + &std * &epsilon
    }

    fn decode(&self, z: &Array2<Float>) -> NeuralResult<Array2<Float>> {
        let decoder_weights = self
            .decoder_weights
            .as_ref()
            .ok_or_else(|| SklearsError::InvalidInput("Model not trained".to_string()))?;
        let decoder_biases = self
            .decoder_biases
            .as_ref()
            .expect("decoder_biases not available - model not fitted");

        let mut activations = z.clone();

        // Forward through decoder (except last layer)
        for (weights, biases) in decoder_weights
            .iter()
            .zip(decoder_biases.iter())
            .take(decoder_weights.len() - 1)
        {
            activations = activations.dot(&weights.t()) + biases;
            activations = self.apply_activation(&activations);
        }

        // Last layer (reconstruction)
        let last_weights = decoder_weights.last().expect("empty collection");
        let last_bias = decoder_biases.last().expect("empty collection");
        activations = activations.dot(&last_weights.t()) + last_bias;

        // Apply sigmoid for bounded outputs (0-1)
        activations = activations.mapv(|x| 1.0 / (1.0 + (-x).exp()));

        Ok(activations)
    }
}

#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::ndarray::Array2;

    #[test]
    fn test_vae_config_builder() {
        let config = VAEConfig::default()
            .latent_dim(64)
            .beta(0.5)
            .learning_rate(0.01);

        assert_eq!(config.latent_dim, 64);
        assert_eq!(config.beta, 0.5);
        assert_eq!(config.learning_rate, 0.01);
    }

    #[test]
    fn test_vae_creation() {
        let config = VAEConfig::default().latent_dim(16);
        let vae = VAE::new(config);

        assert_eq!(vae.config.latent_dim, 16);
        assert!(vae.encoder_weights.is_none());
    }

    #[test]
    fn test_vae_fit_returns_not_implemented() {
        use scirs2_core::random::essentials::Uniform;
        let mut rng = thread_rng();
        let dist = Uniform::new(0.0, 1.0).expect("construction should succeed");

        // Create simple test data
        let x = Array2::from_shape_simple_fn((10, 4), || rng.sample(dist));

        let config = VAEConfig::default()
            .latent_dim(2)
            .encoder_layers(vec![8])
            .decoder_layers(vec![8])
            .n_epochs(2);

        let vae = VAE::new(config);
        let result = vae.fit(&x, &());
        assert!(
            result.is_err(),
            "VAE fit should return NotImplemented error"
        );
        let err = result.unwrap_err();
        let err_msg = format!("{}", err);
        assert!(
            err_msg.contains("not yet implemented") || err_msg.contains("NotImplemented"),
            "Error should indicate not implemented, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_vae_fit_rejects_empty_input() {
        let x = Array2::<Float>::zeros((0, 3));
        let config = VAEConfig::default().latent_dim(2).n_epochs(1);
        let vae = VAE::new(config);
        let result = vae.fit(&x, &());
        assert!(result.is_err(), "VAE fit should reject empty input");
    }
}