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
//! Configuration Management for Neural Networks
//!
//! This module provides comprehensive configuration support for neural networks,
//! allowing users to define architectures, training parameters, and model settings
//! through YAML and JSON files.

use crate::activation::Activation;
use crate::weight_init::InitStrategy;
use crate::NeuralResult;
use sklears_core::error::SklearsError;
use sklears_core::types::FloatBounds;
use std::collections::HashMap;
use std::path::Path;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Configuration format for loading/saving
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ConfigFormat {
    /// JSON text format
    JSON,
    /// YAML text format
    YAML,
}

impl ConfigFormat {
    /// Determine format from file extension
    pub fn from_path<P: AsRef<Path>>(path: P) -> ConfigFormat {
        let path = path.as_ref();
        match path.extension().and_then(|s| s.to_str()) {
            Some("yaml") | Some("yml") => ConfigFormat::YAML,
            Some("json") => ConfigFormat::JSON,
            _ => ConfigFormat::JSON, // default to JSON
        }
    }
}

/// Complete neural network configuration
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "serde",
    serde(bound = "T: FloatBounds + serde::Serialize + serde::de::DeserializeOwned")
)]
pub struct NeuralNetworkConfig<T: FloatBounds> {
    /// Model architecture configuration
    pub model: ModelConfig<T>,
    /// Training configuration
    pub training: TrainingConfig<T>,
    /// Optimization configuration
    pub optimizer: OptimizerConfig<T>,
    /// Data processing configuration
    pub data: DataConfig,
    /// Evaluation configuration
    pub evaluation: EvaluationConfig,
    /// Miscellaneous settings
    pub misc: MiscConfig,
}

/// Model architecture configuration
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "serde",
    serde(bound = "T: FloatBounds + serde::Serialize + serde::de::DeserializeOwned")
)]
pub struct ModelConfig<T: FloatBounds> {
    /// Model type (mlp, transformer, rnn, etc.)
    pub model_type: String,
    /// Input dimension
    pub input_dim: usize,
    /// Output dimension
    pub output_dim: usize,
    /// Hidden layer sizes for MLP
    pub hidden_layers: Vec<usize>,
    /// Activation function
    pub activation: String,
    /// Dropout rate
    pub dropout: Option<T>,
    /// Batch normalization
    pub batch_norm: bool,
    /// Layer normalization
    pub layer_norm: bool,
    /// Weight initialization strategy
    pub weight_init: String,
    /// Architecture-specific parameters
    pub arch_params: HashMap<String, String>,
}

/// Training configuration
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "serde",
    serde(bound = "T: FloatBounds + serde::Serialize + serde::de::DeserializeOwned")
)]
pub struct TrainingConfig<T: FloatBounds> {
    /// Number of epochs
    pub epochs: usize,
    /// Batch size
    pub batch_size: usize,
    /// Learning rate
    pub learning_rate: T,
    /// Learning rate schedule
    pub lr_schedule: LearningRateSchedule<T>,
    /// Early stopping configuration
    pub early_stopping: Option<EarlyStoppingConfig<T>>,
    /// Validation split
    pub validation_split: Option<T>,
    /// Random seed
    pub random_seed: Option<u64>,
    /// Mixed precision training
    pub mixed_precision: bool,
    /// Gradient clipping
    pub gradient_clipping: Option<T>,
}

/// Learning rate schedule configuration
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "serde",
    serde(bound = "T: FloatBounds + serde::Serialize + serde::de::DeserializeOwned")
)]
pub struct LearningRateSchedule<T: FloatBounds> {
    /// Schedule type
    pub schedule_type: String,
    /// Step size for step decay
    pub step_size: Option<usize>,
    /// Decay factor
    pub decay_factor: Option<T>,
    /// Minimum learning rate
    pub min_lr: Option<T>,
    /// Maximum learning rate for cyclical schedules
    pub max_lr: Option<T>,
    /// Warmup steps
    pub warmup_steps: Option<usize>,
}

/// Early stopping configuration
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "serde",
    serde(bound = "T: FloatBounds + serde::Serialize + serde::de::DeserializeOwned")
)]
pub struct EarlyStoppingConfig<T: FloatBounds> {
    /// Metric to monitor
    pub monitor: String,
    /// Patience (number of epochs)
    pub patience: usize,
    /// Minimum improvement threshold
    pub min_delta: T,
    /// Mode (min or max)
    pub mode: String,
}

/// Optimizer configuration
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
    feature = "serde",
    serde(bound = "T: FloatBounds + serde::Serialize + serde::de::DeserializeOwned")
)]
pub struct OptimizerConfig<T: FloatBounds> {
    /// Optimizer type
    pub optimizer_type: String,
    /// Momentum for SGD/RMSprop
    pub momentum: Option<T>,
    /// Beta1 for Adam-like optimizers
    pub beta1: Option<T>,
    /// Beta2 for Adam-like optimizers
    pub beta2: Option<T>,
    /// Weight decay
    pub weight_decay: Option<T>,
    /// Epsilon for numerical stability
    pub epsilon: Option<T>,
    /// Nesterov momentum
    pub nesterov: bool,
}

/// Data processing configuration
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct DataConfig {
    /// Data normalization
    pub normalization: Option<String>,
    /// Data augmentation
    pub augmentation: Vec<DataAugmentationConfig>,
    /// Whether to shuffle data
    pub shuffle: bool,
    /// Number of data loading workers
    pub num_workers: usize,
    /// Pin memory for GPU training
    pub pin_memory: bool,
}

/// Data augmentation configuration
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct DataAugmentationConfig {
    /// Augmentation type
    pub aug_type: String,
    /// Probability of applying
    pub probability: f64,
    /// Parameters for the augmentation
    pub params: HashMap<String, String>,
}

/// Evaluation configuration
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct EvaluationConfig {
    /// Metrics to compute
    pub metrics: Vec<String>,
    /// Whether to compute confusion matrix
    pub confusion_matrix: bool,
    /// Whether to compute classification report
    pub classification_report: bool,
    /// Test batch size
    pub test_batch_size: Option<usize>,
}

/// Miscellaneous configuration
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct MiscConfig {
    /// Whether to log training progress
    pub verbose: bool,
    /// Logging frequency (every N epochs)
    pub log_frequency: usize,
    /// Checkpoint saving frequency
    pub checkpoint_frequency: Option<usize>,
    /// Directory for saving checkpoints
    pub checkpoint_dir: Option<String>,
    /// Device preference (cpu, gpu)
    pub device: String,
    /// Whether to use deterministic algorithms
    pub deterministic: bool,
}

impl<T: FloatBounds> Default for NeuralNetworkConfig<T> {
    fn default() -> Self {
        Self {
            model: ModelConfig::default(),
            training: TrainingConfig::default(),
            optimizer: OptimizerConfig::default(),
            data: DataConfig::default(),
            evaluation: EvaluationConfig::default(),
            misc: MiscConfig::default(),
        }
    }
}

impl<T: FloatBounds> Default for ModelConfig<T> {
    fn default() -> Self {
        Self {
            model_type: "mlp".to_string(),
            input_dim: 784,
            output_dim: 10,
            hidden_layers: vec![128, 64],
            activation: "relu".to_string(),
            dropout: Some(T::from(0.2).unwrap_or_else(|| T::zero())),
            batch_norm: false,
            layer_norm: false,
            weight_init: "xavier_uniform".to_string(),
            arch_params: HashMap::new(),
        }
    }
}

impl<T: FloatBounds> Default for TrainingConfig<T> {
    fn default() -> Self {
        Self {
            epochs: 100,
            batch_size: 32,
            learning_rate: T::from(0.001).unwrap_or_else(|| T::zero()),
            lr_schedule: LearningRateSchedule::default(),
            early_stopping: None,
            validation_split: Some(T::from(0.2).unwrap_or_else(|| T::zero())),
            random_seed: Some(42),
            mixed_precision: false,
            gradient_clipping: None,
        }
    }
}

impl<T: FloatBounds> Default for LearningRateSchedule<T> {
    fn default() -> Self {
        Self {
            schedule_type: "constant".to_string(),
            step_size: None,
            decay_factor: None,
            min_lr: None,
            max_lr: None,
            warmup_steps: None,
        }
    }
}

impl<T: FloatBounds> Default for OptimizerConfig<T> {
    fn default() -> Self {
        Self {
            optimizer_type: "adam".to_string(),
            momentum: None,
            beta1: Some(T::from(0.9).unwrap_or_else(|| T::zero())),
            beta2: Some(T::from(0.999).unwrap_or_else(|| T::zero())),
            weight_decay: Some(T::from(1e-4).unwrap_or_else(|| T::zero())),
            epsilon: Some(T::from(1e-8).unwrap_or_else(|| T::zero())),
            nesterov: false,
        }
    }
}

impl Default for DataConfig {
    fn default() -> Self {
        Self {
            normalization: Some("standard".to_string()),
            augmentation: Vec::new(),
            shuffle: true,
            num_workers: 1,
            pin_memory: false,
        }
    }
}

impl Default for EvaluationConfig {
    fn default() -> Self {
        Self {
            metrics: vec!["accuracy".to_string(), "loss".to_string()],
            confusion_matrix: false,
            classification_report: false,
            test_batch_size: None,
        }
    }
}

impl Default for MiscConfig {
    fn default() -> Self {
        Self {
            verbose: true,
            log_frequency: 10,
            checkpoint_frequency: None,
            checkpoint_dir: None,
            device: "cpu".to_string(),
            deterministic: false,
        }
    }
}

/// Configuration manager for loading and saving configurations
pub struct ConfigManager;

impl ConfigManager {
    /// Load configuration from file
    #[cfg(feature = "serde")]
    pub fn load_config<T: FloatBounds + for<'de> serde::Deserialize<'de>>(
        path: &str,
    ) -> NeuralResult<NeuralNetworkConfig<T>> {
        let format = ConfigFormat::from_path(path);
        let content = std::fs::read_to_string(path).map_err(|e| {
            SklearsError::InvalidInput(format!("Failed to read config file: {}", e))
        })?;

        match format {
            ConfigFormat::JSON => serde_json::from_str(&content)
                .map_err(|e| SklearsError::InvalidInput(format!("Failed to parse JSON: {}", e))),
            ConfigFormat::YAML => serde_yaml::from_str(&content)
                .map_err(|e| SklearsError::InvalidInput(format!("Failed to parse YAML: {}", e))),
        }
    }

    /// Save configuration to file
    #[cfg(feature = "serde")]
    pub fn save_config<T: FloatBounds + serde::Serialize>(
        config: &NeuralNetworkConfig<T>,
        path: &str,
    ) -> NeuralResult<()> {
        let format = ConfigFormat::from_path(path);

        let content = match format {
            ConfigFormat::JSON => serde_json::to_string_pretty(config).map_err(|e| {
                SklearsError::InvalidInput(format!("Failed to serialize to JSON: {}", e))
            })?,
            ConfigFormat::YAML => serde_yaml::to_string(config).map_err(|e| {
                SklearsError::InvalidInput(format!("Failed to serialize to YAML: {}", e))
            })?,
        };

        std::fs::write(path, content).map_err(|e| {
            SklearsError::InvalidInput(format!("Failed to write config file: {}", e))
        })?;

        Ok(())
    }

    /// Create a template configuration file
    #[cfg(feature = "serde")]
    pub fn create_template<T: FloatBounds + serde::Serialize + Default>(
        path: &str,
        model_type: &str,
    ) -> NeuralResult<()> {
        let mut config = NeuralNetworkConfig::<T>::default();
        config.model.model_type = model_type.to_string();

        // Customize defaults based on model type
        match model_type {
            "transformer" => {
                config
                    .model
                    .arch_params
                    .insert("num_heads".to_string(), "8".to_string());
                config
                    .model
                    .arch_params
                    .insert("d_model".to_string(), "512".to_string());
                config
                    .model
                    .arch_params
                    .insert("num_layers".to_string(), "6".to_string());
                config.training.learning_rate = T::from(1e-4).unwrap_or_else(|| T::zero());
            }
            "rnn" | "lstm" | "gru" => {
                config
                    .model
                    .arch_params
                    .insert("sequence_length".to_string(), "50".to_string());
                config
                    .model
                    .arch_params
                    .insert("bidirectional".to_string(), "false".to_string());
            }
            "cnn" => {
                config
                    .model
                    .arch_params
                    .insert("kernel_size".to_string(), "3".to_string());
                config
                    .model
                    .arch_params
                    .insert("stride".to_string(), "1".to_string());
                config
                    .model
                    .arch_params
                    .insert("padding".to_string(), "1".to_string());
            }
            _ => {} // MLP defaults are already set
        }

        Self::save_config(&config, path)
    }

    /// Validate configuration
    pub fn validate_config<T: FloatBounds>(config: &NeuralNetworkConfig<T>) -> NeuralResult<()> {
        // Validate model configuration
        if config.model.input_dim == 0 {
            return Err(SklearsError::InvalidParameter {
                name: "input_dim".to_string(),
                reason: "must be greater than 0".to_string(),
            });
        }

        if config.model.output_dim == 0 {
            return Err(SklearsError::InvalidParameter {
                name: "output_dim".to_string(),
                reason: "must be greater than 0".to_string(),
            });
        }

        // Validate training configuration
        if config.training.epochs == 0 {
            return Err(SklearsError::InvalidParameter {
                name: "epochs".to_string(),
                reason: "must be greater than 0".to_string(),
            });
        }

        if config.training.batch_size == 0 {
            return Err(SklearsError::InvalidParameter {
                name: "batch_size".to_string(),
                reason: "must be greater than 0".to_string(),
            });
        }

        if config.training.learning_rate <= T::zero() {
            return Err(SklearsError::InvalidParameter {
                name: "learning_rate".to_string(),
                reason: "must be greater than 0".to_string(),
            });
        }

        // Validate optimizer configuration
        if let Some(beta1) = config.optimizer.beta1 {
            if beta1 < T::zero() || beta1 >= T::one() {
                return Err(SklearsError::InvalidParameter {
                    name: "beta1".to_string(),
                    reason: "must be in range [0, 1)".to_string(),
                });
            }
        }

        if let Some(beta2) = config.optimizer.beta2 {
            if beta2 < T::zero() || beta2 >= T::one() {
                return Err(SklearsError::InvalidParameter {
                    name: "beta2".to_string(),
                    reason: "must be in range [0, 1)".to_string(),
                });
            }
        }

        Ok(())
    }

    /// Convert string representations to enums/types
    pub fn parse_activation(activation_str: &str) -> NeuralResult<Activation> {
        match activation_str.to_lowercase().as_str() {
            "relu" => Ok(Activation::Relu),
            "sigmoid" => Ok(Activation::Logistic),
            "tanh" => Ok(Activation::Tanh),
            "identity" | "linear" => Ok(Activation::Identity),
            "elu" => Ok(Activation::Elu),
            "leakyrelu" => Ok(Activation::LeakyRelu),
            "swish" | "silu" => Ok(Activation::Swish),
            "gelu" => Ok(Activation::Gelu),
            "mish" => Ok(Activation::Mish),
            _ => Err(SklearsError::InvalidInput(format!(
                "Unknown activation function: {}",
                activation_str
            ))),
        }
    }

    /// Convert string representations to weight initialization strategies
    pub fn parse_weight_init(init_str: &str) -> NeuralResult<InitStrategy> {
        match init_str.to_lowercase().as_str() {
            "zeros" => Ok(InitStrategy::Zeros),
            "uniform" => Ok(InitStrategy::Uniform {
                low: -0.1,
                high: 0.1,
            }),
            "normal" => Ok(InitStrategy::Normal {
                mean: 0.0,
                std: 0.1,
            }),
            "xavier_uniform" | "glorot_uniform" => Ok(InitStrategy::XavierUniform),
            "xavier_normal" | "glorot_normal" => Ok(InitStrategy::XavierNormal),
            "he_uniform" => Ok(InitStrategy::HeUniform),
            "he_normal" => Ok(InitStrategy::HeNormal),
            "lecun_uniform" => Ok(InitStrategy::LeCunUniform),
            "lecun_normal" => Ok(InitStrategy::LeCunNormal),
            "orthogonal" => Ok(InitStrategy::Orthogonal { gain: 1.0 }),
            _ => Err(SklearsError::InvalidInput(format!(
                "Unknown weight initialization: {}",
                init_str
            ))),
        }
    }
}

/// Create example configuration files
pub fn create_example_configs() -> NeuralResult<()> {
    // Create output directory
    std::fs::create_dir_all("configs/examples")
        .map_err(|e| SklearsError::InvalidInput(format!("Failed to create directory: {}", e)))?;

    #[cfg(feature = "serde")]
    {
        // MLP configuration
        ConfigManager::create_template::<f32>("configs/examples/mlp_config.yaml", "mlp")?;
        ConfigManager::create_template::<f32>("configs/examples/mlp_config.json", "mlp")?;

        // Transformer configuration
        ConfigManager::create_template::<f32>(
            "configs/examples/transformer_config.yaml",
            "transformer",
        )?;

        // RNN configuration
        ConfigManager::create_template::<f32>("configs/examples/lstm_config.yaml", "lstm")?;

        // CNN configuration
        ConfigManager::create_template::<f32>("configs/examples/cnn_config.yaml", "cnn")?;
    }

    println!("Example configuration files created in configs/examples/");
    Ok(())
}

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

    #[test]
    fn test_config_format_from_path() {
        assert_eq!(ConfigFormat::from_path("config.yaml"), ConfigFormat::YAML);
        assert_eq!(ConfigFormat::from_path("config.yml"), ConfigFormat::YAML);
        assert_eq!(ConfigFormat::from_path("config.json"), ConfigFormat::JSON);
        assert_eq!(ConfigFormat::from_path("config.txt"), ConfigFormat::JSON); // default
    }

    #[test]
    fn test_default_configs() {
        let config = NeuralNetworkConfig::<f32>::default();
        assert_eq!(config.model.model_type, "mlp");
        assert_eq!(config.training.epochs, 100);
        assert_eq!(config.optimizer.optimizer_type, "adam");
    }

    #[test]
    fn test_config_validation() {
        let mut config = NeuralNetworkConfig::<f32>::default();
        assert!(ConfigManager::validate_config(&config).is_ok());

        // Test invalid input_dim
        config.model.input_dim = 0;
        assert!(ConfigManager::validate_config(&config).is_err());
    }

    #[test]
    fn test_activation_parsing() {
        assert!(matches!(
            ConfigManager::parse_activation("relu"),
            Ok(Activation::Relu)
        ));
        assert!(matches!(
            ConfigManager::parse_activation("ReLU"),
            Ok(Activation::Relu)
        ));
        assert!(ConfigManager::parse_activation("unknown").is_err());
    }

    #[test]
    fn test_weight_init_parsing() {
        assert!(matches!(
            ConfigManager::parse_weight_init("xavier_uniform"),
            Ok(InitStrategy::XavierUniform)
        ));
        assert!(ConfigManager::parse_weight_init("unknown").is_err());
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_config_serialization() {
        let config = NeuralNetworkConfig::<f32>::default();

        // Test JSON serialization
        let json_str = serde_json::to_string(&config).expect("operation should succeed");
        let parsed: NeuralNetworkConfig<f32> =
            serde_json::from_str(&json_str).expect("operation should succeed");
        assert_eq!(parsed.model.model_type, config.model.model_type);

        // Test YAML serialization
        let yaml_str = serde_yaml::to_string(&config).expect("operation should succeed");
        let parsed: NeuralNetworkConfig<f32> =
            serde_yaml::from_str(&yaml_str).expect("operation should succeed");
        assert_eq!(parsed.model.model_type, config.model.model_type);
    }
}