sklears-neural 0.1.1

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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
//! Multi-layer Perceptron (MLP) regressor implementation.

use crate::{
    activation::Activation,
    solvers::{AdamSolver, LambSolver, LarsSolver, LearningRateSchedule, SgdSolver, Solver},
    utils::{
        create_batches_regression, initialize_biases, initialize_weights, EarlyStopping, WeightInit,
    },
    NeuralResult,
};
use scirs2_core::ndarray::{Array1, Array2, Axis};
use scirs2_core::random::ChaCha8Rng;
use scirs2_core::random::SeedableRng;
use sklears_core::{error::SklearsError, traits::Fit, traits::Predict};

/// Type alias for neural network weights and biases
type WeightsAndBiases = (Vec<Array2<f64>>, Vec<Array1<f64>>);

/// Multi-layer Perceptron regressor
#[derive(Debug, Clone)]
pub struct MLPRegressor<State = sklears_core::traits::Untrained> {
    /// Number of neurons in each hidden layer
    pub hidden_layer_sizes: Vec<usize>,
    /// Activation function applied to each hidden layer
    pub activation: Activation,
    /// Optimization algorithm used for weight updates
    pub solver: Solver,
    /// L2 regularization coefficient applied to all weight matrices
    pub alpha: f64,
    /// Mini-batch size; `None` uses the full training set
    pub batch_size: Option<usize>,
    /// Initial learning rate passed to the solver
    pub learning_rate_init: f64,
    /// Maximum number of training epochs
    pub max_iter: usize,
    /// Whether to shuffle the training data at the start of each epoch
    pub shuffle: bool,
    /// Optional random seed for reproducibility; `None` uses a random seed
    pub random_state: Option<u64>,
    /// Tolerance for the optimization convergence criterion
    pub tol: f64,
    /// Whether to print progress messages during training
    pub verbose: bool,
    /// Whether to reuse the solution of the previous `fit` call as the starting point
    pub warm_start: bool,
    /// Momentum coefficient for SGD and variants
    pub momentum: f64,
    /// Whether to use Nesterov's momentum with SGD
    pub nesterovs_momentum: bool,
    /// Whether to stop training early based on a held-out validation set
    pub early_stopping: bool,
    /// Fraction of training data held out for early stopping validation
    pub validation_fraction: f64,
    /// Exponential decay rate for Adam's first moment estimates
    pub beta_1: f64,
    /// Exponential decay rate for Adam's second moment estimates
    pub beta_2: f64,
    /// Numerical stability constant added to Adam's denominator
    pub epsilon: f64,
    /// Number of consecutive epochs with no improvement before triggering early stopping
    pub n_iter_no_change: usize,
    /// Maximum number of loss function evaluations (used by L-BFGS)
    pub max_fun: usize,
    /// Learning rate schedule strategy
    pub learning_rate: LearningRateSchedule,
    /// Exponent for the inverse scaling learning rate schedule
    pub power_t: f64,
    /// Weight initialization strategy
    pub weight_init: WeightInit,

    // Trained state
    state: State,
}

/// Trained MLP regressor
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TrainedMLPRegressor {
    /// Weight matrices for each layer, ordered from input to output
    pub weights: Vec<Array2<f64>>,
    /// Bias vectors for each layer, ordered from input to output
    pub biases: Vec<Array1<f64>>,
    /// Number of input features seen during `fit`
    pub n_features_in: usize,
    /// Number of regression output targets
    pub n_outputs: usize,
    /// Total number of layers including input, hidden, and output
    pub n_layers: usize,
    /// Activation function applied to hidden layers
    pub activation: Activation,
    /// Final training loss after the last epoch
    pub loss: f64,
    /// Number of training epochs completed
    pub n_iter: usize,
}

impl MLPRegressor<sklears_core::traits::Untrained> {
    /// Create a new MLP regressor with default parameters
    pub fn new() -> Self {
        Self {
            hidden_layer_sizes: vec![100],
            activation: Activation::Relu,
            solver: Solver::Adam,
            alpha: 0.0001,
            batch_size: None,
            learning_rate_init: 0.001,
            max_iter: 200,
            shuffle: true,
            random_state: None,
            tol: 1e-4,
            verbose: false,
            warm_start: false,
            momentum: 0.9,
            nesterovs_momentum: true,
            early_stopping: false,
            validation_fraction: 0.1,
            beta_1: 0.9,
            beta_2: 0.999,
            epsilon: 1e-8,
            n_iter_no_change: 10,
            max_fun: 15000,
            learning_rate: LearningRateSchedule::Constant,
            power_t: 0.5,
            weight_init: WeightInit::Xavier,
            state: sklears_core::traits::Untrained,
        }
    }

    /// Set the hidden layer sizes
    pub fn hidden_layer_sizes(mut self, sizes: &[usize]) -> Self {
        self.hidden_layer_sizes = sizes.to_vec();
        self
    }

    /// Set the activation function
    pub fn activation(mut self, activation: Activation) -> Self {
        self.activation = activation;
        self
    }

    /// Set the solver
    pub fn solver(mut self, solver: Solver) -> Self {
        self.solver = solver;
        self
    }

    /// Set the L2 regularization parameter
    pub fn alpha(mut self, alpha: f64) -> Self {
        self.alpha = alpha;
        self
    }

    /// Set the batch size
    pub fn batch_size(mut self, batch_size: Option<usize>) -> Self {
        self.batch_size = batch_size;
        self
    }

    /// Set the initial learning rate
    pub fn learning_rate_init(mut self, learning_rate: f64) -> Self {
        self.learning_rate_init = learning_rate;
        self
    }

    /// Set the maximum number of iterations
    pub fn max_iter(mut self, max_iter: usize) -> Self {
        self.max_iter = max_iter;
        self
    }

    /// Set whether to shuffle the training data
    pub fn shuffle(mut self, shuffle: bool) -> Self {
        self.shuffle = shuffle;
        self
    }

    /// Set the random state for reproducibility
    pub fn random_state(mut self, random_state: u64) -> Self {
        self.random_state = Some(random_state);
        self
    }

    /// Set the tolerance for optimization
    pub fn tol(mut self, tol: f64) -> Self {
        self.tol = tol;
        self
    }

    /// Set verbosity
    pub fn verbose(mut self, verbose: bool) -> Self {
        self.verbose = verbose;
        self
    }

    /// Set early stopping
    pub fn early_stopping(mut self, early_stopping: bool) -> Self {
        self.early_stopping = early_stopping;
        self
    }

    /// Set weight initialization strategy
    pub fn weight_init(mut self, weight_init: WeightInit) -> Self {
        self.weight_init = weight_init;
        self
    }
}

impl Fit<Array2<f64>, Array2<f64>> for MLPRegressor<sklears_core::traits::Untrained> {
    type Fitted = MLPRegressor<TrainedMLPRegressor>;

    fn fit(
        self,
        x: &Array2<f64>,
        y: &Array2<f64>,
    ) -> NeuralResult<MLPRegressor<TrainedMLPRegressor>> {
        let (n_samples, n_features) = x.dim();
        let (n_samples_y, n_outputs) = y.dim();

        if n_samples != n_samples_y {
            return Err(SklearsError::ShapeMismatch {
                expected: format!("x.nrows()={n_samples}"),
                actual: format!("y.nrows()={n_samples_y}"),
            });
        }

        // Initialize random number generator
        let mut rng = match self.random_state {
            Some(seed) => ChaCha8Rng::seed_from_u64(seed),
            None => ChaCha8Rng::seed_from_u64(42),
        };

        // Build layer sizes
        let mut layer_sizes = vec![n_features];
        layer_sizes.extend_from_slice(&self.hidden_layer_sizes);
        layer_sizes.push(n_outputs);

        // Initialize weights and biases
        let mut weights = Vec::new();
        let mut biases = Vec::new();

        for i in 0..layer_sizes.len() - 1 {
            let w = initialize_weights(
                layer_sizes[i],
                layer_sizes[i + 1],
                &self.weight_init,
                &mut rng,
            );
            let b = initialize_biases(layer_sizes[i + 1], &self.weight_init, &mut rng);
            weights.push(w);
            biases.push(b);
        }

        // Determine batch size
        let batch_size = self.batch_size.unwrap_or(n_samples.min(200));

        // Initialize solver
        let mut solver = match self.solver {
            Solver::Sgd => SolverType::Sgd(SgdSolver::new(
                self.learning_rate_init,
                self.momentum,
                self.nesterovs_momentum,
                self.learning_rate,
                self.power_t,
                self.learning_rate_init,
            )),
            Solver::Adam => SolverType::Adam(AdamSolver::new(
                self.learning_rate_init,
                self.beta_1,
                self.beta_2,
                self.epsilon,
            )),
            Solver::AdamW => {
                return Err(SklearsError::NotImplemented(
                    "AdamW solver not yet integrated with MLP".to_string(),
                ));
            }
            Solver::RMSprop => {
                return Err(SklearsError::NotImplemented(
                    "RMSprop solver not yet integrated with MLP".to_string(),
                ));
            }
            Solver::Nadam => {
                return Err(SklearsError::NotImplemented(
                    "Nadam solver not yet integrated with MLP".to_string(),
                ));
            }
            Solver::Lbfgs => {
                return Err(SklearsError::NotImplemented(
                    "L-BFGS solver not yet implemented".to_string(),
                ));
            }
            Solver::Lars => SolverType::Lars(LarsSolver::new(
                self.learning_rate_init,
                self.momentum,
                self.alpha, // Use alpha as weight decay
                0.001,      // lars_coefficient
                1e-8,       // epsilon
                1.0,        // trust_coefficient
            )),
            Solver::Lamb => SolverType::Lamb(LambSolver::new(
                self.learning_rate_init,
                self.beta_1,
                self.beta_2,
                self.epsilon,
                self.alpha, // Use alpha as weight decay
                1.0,        // trust_coefficient
            )),
        };

        // Initialize solver state
        match &mut solver {
            SolverType::Sgd(sgd) => sgd.initialize(&weights, &biases),
            SolverType::Adam(adam) => adam.initialize(&weights, &biases),
            SolverType::Lars(lars) => lars.initialize(&weights, &biases),
            SolverType::Lamb(lamb) => lamb.initialize(&weights, &biases),
        }

        // Training loop
        let mut best_loss = f64::INFINITY;
        let mut n_iter = 0;
        let mut early_stopping = if self.early_stopping {
            Some(EarlyStopping::new(self.n_iter_no_change, self.tol, true))
        } else {
            None
        };

        for epoch in 0..self.max_iter {
            let batches = create_batches_regression(x, y, batch_size, self.shuffle, &mut rng);

            let mut total_loss = 0.0;
            let mut total_samples = 0;

            for (batch_x, batch_y) in batches {
                // Forward pass
                let (activations, loss) =
                    self.forward_pass(&batch_x, &batch_y, &weights, &biases)?;

                // Backward pass
                let (weight_grads, bias_grads) =
                    self.backward_pass(&batch_x, &batch_y, &activations, &weights, &biases)?;

                // Update parameters
                match &mut solver {
                    SolverType::Sgd(sgd) => {
                        sgd.update_params(&mut weights, &mut biases, &weight_grads, &bias_grads)?;
                    }
                    SolverType::Adam(adam) => {
                        adam.update_params(&mut weights, &mut biases, &weight_grads, &bias_grads)?;
                    }
                    SolverType::Lars(lars) => {
                        lars.update_params(&mut weights, &mut biases, &weight_grads, &bias_grads)?;
                    }
                    SolverType::Lamb(lamb) => {
                        lamb.update_params(&mut weights, &mut biases, &weight_grads, &bias_grads)?;
                    }
                }

                total_loss += loss * batch_x.nrows() as f64;
                total_samples += batch_x.nrows();
            }

            let avg_loss = total_loss / total_samples as f64;
            n_iter = epoch + 1;

            if self.verbose && epoch % 10 == 0 {
                println!("Epoch {epoch}: Loss = {avg_loss:.6}");
            }

            // Check for early stopping
            if let Some(ref mut early_stop) = early_stopping {
                if early_stop.should_stop(avg_loss, &weights, &biases) {
                    if self.verbose {
                        println!("Early stopping at epoch {epoch}");
                    }
                    if let Some((best_weights, best_biases)) = early_stop.get_best_weights() {
                        weights = best_weights.to_vec();
                        biases = best_biases.to_vec();
                    }
                    break;
                }
            }

            // Check for convergence
            if (best_loss - avg_loss).abs() < self.tol {
                if self.verbose {
                    println!("Converged at epoch {epoch}");
                }
                break;
            }

            best_loss = avg_loss;
        }

        Ok(MLPRegressor {
            hidden_layer_sizes: self.hidden_layer_sizes,
            activation: self.activation,
            solver: self.solver,
            alpha: self.alpha,
            batch_size: self.batch_size,
            learning_rate_init: self.learning_rate_init,
            max_iter: self.max_iter,
            shuffle: self.shuffle,
            random_state: self.random_state,
            tol: self.tol,
            verbose: self.verbose,
            warm_start: self.warm_start,
            momentum: self.momentum,
            nesterovs_momentum: self.nesterovs_momentum,
            early_stopping: self.early_stopping,
            validation_fraction: self.validation_fraction,
            beta_1: self.beta_1,
            beta_2: self.beta_2,
            epsilon: self.epsilon,
            n_iter_no_change: self.n_iter_no_change,
            max_fun: self.max_fun,
            learning_rate: self.learning_rate,
            power_t: self.power_t,
            weight_init: self.weight_init,
            state: TrainedMLPRegressor {
                weights,
                biases,
                n_features_in: n_features,
                n_outputs,
                n_layers: layer_sizes.len(),
                activation: self.activation,
                loss: best_loss,
                n_iter,
            },
        })
    }
}

impl MLPRegressor<sklears_core::traits::Untrained> {
    fn forward_pass(
        &self,
        x: &Array2<f64>,
        y_true: &Array2<f64>,
        weights: &[Array2<f64>],
        biases: &[Array1<f64>],
    ) -> NeuralResult<(Vec<Array2<f64>>, f64)> {
        let mut activations = vec![x.clone()];

        // Forward through hidden layers
        for i in 0..weights.len() - 1 {
            let z = activations[i].dot(&weights[i]) + &biases[i];
            let a = self.activation.apply(&z);
            activations.push(a);
        }

        // Output layer (linear activation for regression)
        let z_out = activations
            .last()
            .expect("empty collection")
            .dot(weights.last().expect("empty collection"))
            + biases.last().expect("empty collection");
        let y_pred = z_out; // Linear activation (identity)
        activations.push(y_pred.clone());

        // Compute mean squared error loss
        let loss = self.compute_loss(&y_pred, y_true, weights)?;

        Ok((activations, loss))
    }

    fn backward_pass(
        &self,
        _x: &Array2<f64>,
        y_true: &Array2<f64>,
        activations: &[Array2<f64>],
        weights: &[Array2<f64>],
        _biases: &[Array1<f64>],
    ) -> NeuralResult<WeightsAndBiases> {
        let n_layers = weights.len();
        let mut weight_grads = vec![Array2::zeros((0, 0)); n_layers];
        let mut bias_grads = vec![Array1::zeros(0); n_layers];
        let n_samples = y_true.nrows() as f64;

        // Output layer gradient (linear activation + MSE loss)
        let mut delta = (&activations[n_layers] - y_true) * (2.0 / n_samples);

        // Gradients for output layer
        weight_grads[n_layers - 1] = activations[n_layers - 1].t().dot(&delta);
        bias_grads[n_layers - 1] = delta.sum_axis(Axis(0));

        // Add L2 regularization to weight gradients
        if self.alpha > 0.0 {
            weight_grads[n_layers - 1] =
                &weight_grads[n_layers - 1] + &weights[n_layers - 1] * (2.0 * self.alpha);
        }

        // Backpropagate through hidden layers
        for i in (0..n_layers - 1).rev() {
            // Compute delta for current layer
            delta =
                delta.dot(&weights[i + 1].t()) * &self.activation.derivative(&activations[i + 1]);

            // Compute gradients
            weight_grads[i] = activations[i].t().dot(&delta);
            bias_grads[i] = delta.sum_axis(Axis(0));

            // Add L2 regularization
            if self.alpha > 0.0 {
                weight_grads[i] = &weight_grads[i] + &weights[i] * (2.0 * self.alpha);
            }
        }

        Ok((weight_grads, bias_grads))
    }

    fn compute_loss(
        &self,
        y_pred: &Array2<f64>,
        y_true: &Array2<f64>,
        weights: &[Array2<f64>],
    ) -> NeuralResult<f64> {
        let n_samples = y_pred.nrows() as f64;

        // Mean squared error loss
        let diff = y_pred - y_true;
        let mut loss = (&diff * &diff).sum() / (2.0 * n_samples);

        // Add L2 regularization
        if self.alpha > 0.0 {
            let mut reg_loss = 0.0;
            for weight in weights {
                reg_loss += weight.mapv(|x| x * x).sum();
            }
            loss += self.alpha * reg_loss;
        }

        Ok(loss)
    }
}

impl Predict<Array2<f64>, Array2<f64>> for MLPRegressor<TrainedMLPRegressor> {
    fn predict(&self, x: &Array2<f64>) -> NeuralResult<Array2<f64>> {
        let (_n_samples, n_features) = x.dim();

        if n_features != self.state.n_features_in {
            return Err(SklearsError::FeatureMismatch {
                expected: self.state.n_features_in,
                actual: n_features,
            });
        }

        let mut activations = x.clone();

        // Forward through hidden layers
        for i in 0..self.state.weights.len() - 1 {
            let z = activations.dot(&self.state.weights[i]) + &self.state.biases[i];
            activations = self.state.activation.apply(&z);
        }

        // Output layer (linear)
        let z_out = activations.dot(self.state.weights.last().expect("empty collection"))
            + self.state.biases.last().expect("empty collection");

        Ok(z_out)
    }
}

impl MLPRegressor<TrainedMLPRegressor> {
    /// Get the loss from training
    pub fn loss(&self) -> f64 {
        self.state.loss
    }

    /// Get the number of iterations performed during training
    pub fn n_iter(&self) -> usize {
        self.state.n_iter
    }
}

impl Default for MLPRegressor<sklears_core::traits::Untrained> {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug)]
enum SolverType {
    Sgd(SgdSolver),
    Adam(AdamSolver),
    Lars(LarsSolver),
    Lamb(LambSolver),
}

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

    #[test]
    fn test_mlp_regressor_creation() {
        let mlp = MLPRegressor::new()
            .hidden_layer_sizes(&[10, 5])
            .activation(Activation::Relu)
            .max_iter(100)
            .random_state(42);

        assert_eq!(mlp.hidden_layer_sizes, vec![10, 5]);
        assert_eq!(mlp.activation, Activation::Relu);
        assert_eq!(mlp.max_iter, 100);
        assert_eq!(mlp.random_state, Some(42));
    }

    #[test]
    fn test_mlp_regressor_fit_predict_single_output() {
        let x = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0],];
        let y = array![
            [3.0], // 1*1 + 2*1 = 3
            [5.0], // 2*1 + 3*1 = 5
            [7.0], // 3*1 + 4*1 = 7
            [9.0], // 4*1 + 5*1 = 9
        ];

        let mlp = MLPRegressor::new()
            .hidden_layer_sizes(&[5])
            .max_iter(100)
            .random_state(42);

        let trained_mlp = mlp.fit(&x, &y).expect("model fitting should succeed");
        let predictions = trained_mlp.predict(&x).expect("prediction should succeed");

        assert_eq!(predictions.dim(), (4, 1));

        // Check that predictions are reasonable (finite values)
        for &pred in predictions.iter() {
            assert!(pred.is_finite());
        }
    }

    #[test]
    fn test_mlp_regressor_fit_predict_multi_output() {
        let x = array![[1.0, 2.0], [2.0, 3.0], [3.0, 4.0], [4.0, 5.0],];
        let y = array![[3.0, 6.0], [5.0, 10.0], [7.0, 14.0], [9.0, 18.0],];

        let mlp = MLPRegressor::new()
            .hidden_layer_sizes(&[5])
            .max_iter(50)
            .random_state(42);

        let trained_mlp = mlp.fit(&x, &y).expect("model fitting should succeed");
        let predictions = trained_mlp.predict(&x).expect("prediction should succeed");

        assert_eq!(predictions.dim(), (4, 2));

        // Check that predictions are reasonable (finite values)
        for &pred in predictions.iter() {
            assert!(pred.is_finite());
        }
    }

    #[test]
    fn test_mlp_regressor_dimension_mismatch() {
        let x = array![[1.0, 2.0], [3.0, 4.0]];
        let y = array![[1.0]]; // Wrong number of samples

        let mlp = MLPRegressor::new().random_state(42);
        let result = mlp.fit(&x, &y);

        assert!(result.is_err());
    }

    #[test]
    fn test_mlp_regressor_simple_linear_function() {
        // Test with a simple linear function: y = 2*x1 + 3*x2
        let x = array![[1.0, 1.0], [2.0, 1.0], [1.0, 2.0], [2.0, 2.0],];
        let y = array![
            [5.0],  // 2*1 + 3*1 = 5
            [7.0],  // 2*2 + 3*1 = 7
            [8.0],  // 2*1 + 3*2 = 8
            [10.0], // 2*2 + 3*2 = 10
        ];

        let mlp = MLPRegressor::new()
            .hidden_layer_sizes(&[10])
            .max_iter(200)
            .learning_rate_init(0.01)
            .random_state(42);

        let trained_mlp = mlp.fit(&x, &y).expect("model fitting should succeed");
        let predictions = trained_mlp.predict(&x).expect("prediction should succeed");

        // The network should be able to learn this simple linear function reasonably well
        // We don't expect perfect fit, but should be better than random
        let mut total_error = 0.0;
        for i in 0..y.nrows() {
            total_error += (predictions[[i, 0]] - y[[i, 0]]).abs();
        }
        let mean_absolute_error = total_error / y.nrows() as f64;

        // Should have some learning (error should be less than the range of y values)
        assert!(mean_absolute_error < 5.0);
    }

    #[test]
    fn test_mlp_regressor_reproducibility() {
        // Test that random_state ensures reproducible results
        let x = array![
            [1.0, 2.0],
            [2.0, 3.0],
            [3.0, 4.0],
            [4.0, 5.0],
            [5.0, 6.0],
            [6.0, 7.0],
        ];
        let y = array![[3.0], [5.0], [7.0], [9.0], [11.0], [13.0]];

        // Train first model
        let mlp1 = MLPRegressor::new()
            .hidden_layer_sizes(&[10, 5])
            .max_iter(50)
            .random_state(42)
            .verbose(false);
        let trained1 = mlp1.fit(&x, &y).expect("model fitting should succeed");
        let pred1 = trained1.predict(&x).expect("prediction should succeed");

        // Train second model with same random_state
        let mlp2 = MLPRegressor::new()
            .hidden_layer_sizes(&[10, 5])
            .max_iter(50)
            .random_state(42)
            .verbose(false);
        let trained2 = mlp2.fit(&x, &y).expect("model fitting should succeed");
        let pred2 = trained2.predict(&x).expect("prediction should succeed");

        // Predictions should be identical
        assert_eq!(pred1.dim(), pred2.dim());
        for i in 0..pred1.len() {
            assert_abs_diff_eq!(
                pred1[[i / pred1.ncols(), i % pred1.ncols()]],
                pred2[[i / pred2.ncols(), i % pred2.ncols()]],
                epsilon = 1e-10
            );
        }

        // Train third model with different random_state
        let mlp3 = MLPRegressor::new()
            .hidden_layer_sizes(&[10, 5])
            .max_iter(50)
            .random_state(123)
            .verbose(false);
        let trained3 = mlp3.fit(&x, &y).expect("model fitting should succeed");
        let pred3 = trained3.predict(&x).expect("prediction should succeed");

        // Predictions should be different from the first two
        let mut differences = 0;
        for i in 0..pred1.len() {
            if (pred1[[i / pred1.ncols(), i % pred1.ncols()]]
                - pred3[[i / pred3.ncols(), i % pred3.ncols()]])
            .abs()
                > 1e-10
            {
                differences += 1;
            }
        }
        assert!(
            differences > 0,
            "Different random seeds should produce different results"
        );
    }
}