scirs2-neural 0.4.2

Neural network building blocks module for SciRS2 (scirs2-neural) - Minimal Version
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
//! KAN layer and network implementations.
//!
//! A KAN layer replaces fixed activation functions with learnable univariate
//! functions on each edge. The output is:
//!
//! ```text
//! y_j = sum_{i=1}^{n_in}  phi_{i,j}(x_i)
//! ```
//!
//! where `phi_{i,j}` is a learned activation (B-spline or rational) on the
//! edge connecting input node `i` to output node `j`.

use scirs2_core::ndarray::{Array1, Array2};

use crate::NeuralError;

use super::{
    rational::{RationalActivation, RationalConfig},
    spline::{BSplineActivation, SplineConfig},
    KanResult,
};

// ---------------------------------------------------------------------------
// Activation type enum
// ---------------------------------------------------------------------------

/// Selects which type of learnable activation to use on each KAN edge.
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum ActivationType {
    /// B-spline activations (default; piecewise polynomial, smooth).
    BSpline(SplineConfig),
    /// Rational activations (Padé-type; globally smooth).
    Rational(RationalConfig),
}

impl Default for ActivationType {
    fn default() -> Self {
        ActivationType::BSpline(SplineConfig::default())
    }
}

// ---------------------------------------------------------------------------
// KanLayerConfig
// ---------------------------------------------------------------------------

/// Configuration for a single KAN layer.
#[derive(Debug, Clone)]
pub struct KanLayerConfig {
    /// Number of input features.
    pub n_in: usize,
    /// Number of output features.
    pub n_out: usize,
    /// Type of learnable activation to place on each edge.
    pub activation_type: ActivationType,
}

impl Default for KanLayerConfig {
    fn default() -> Self {
        Self {
            n_in: 4,
            n_out: 4,
            activation_type: ActivationType::default(),
        }
    }
}

// ---------------------------------------------------------------------------
// KanLayer
// ---------------------------------------------------------------------------

/// A single KAN layer with `n_in × n_out` learnable edge-activations.
///
/// Each activation `phi_{i,j}` acts on input dimension `i` and contributes to
/// output dimension `j`. The layer output is the column-wise sum:
/// `output[j] = sum_i phi_{i,j}(input[i])`.
pub struct KanLayer {
    config: KanLayerConfig,
    /// Flat storage: `spline_activations[i * n_out + j]` = `phi_{i,j}`.
    /// `None` when `ActivationType::Rational` is chosen.
    spline_activations: Option<Vec<BSplineActivation>>,
    /// Flat storage: `rational_activations[i * n_out + j]` = `phi_{i,j}`.
    /// `None` when `ActivationType::BSpline` is chosen.
    rational_activations: Option<Vec<RationalActivation>>,
}

impl KanLayer {
    /// Construct a new `KanLayer` from a [`KanLayerConfig`].
    ///
    /// Returns an error if any activation config is invalid or if `n_in`/`n_out`
    /// are zero.
    pub fn new(config: KanLayerConfig) -> KanResult<Self> {
        if config.n_in == 0 {
            return Err(NeuralError::InvalidArgument(
                "KanLayer: n_in must be > 0".to_string(),
            ));
        }
        if config.n_out == 0 {
            return Err(NeuralError::InvalidArgument(
                "KanLayer: n_out must be > 0".to_string(),
            ));
        }

        let n_edges = config.n_in * config.n_out;
        match &config.activation_type {
            ActivationType::BSpline(sc) => {
                let mut activations = Vec::with_capacity(n_edges);
                for _ in 0..n_edges {
                    activations.push(BSplineActivation::new(sc)?);
                }
                Ok(Self {
                    config,
                    spline_activations: Some(activations),
                    rational_activations: None,
                })
            }
            ActivationType::Rational(rc) => {
                let mut activations = Vec::with_capacity(n_edges);
                for _ in 0..n_edges {
                    activations.push(RationalActivation::new(rc)?);
                }
                Ok(Self {
                    config,
                    spline_activations: None,
                    rational_activations: Some(activations),
                })
            }
        }
    }

    /// Evaluate `phi_{i,j}(x)` for the activation at edge `(i, j)`.
    fn eval_activation(&self, i: usize, j: usize, x: f64) -> f64 {
        let idx = i * self.config.n_out + j;
        match (&self.spline_activations, &self.rational_activations) {
            (Some(splines), _) => splines[idx].evaluate(x),
            (_, Some(rationals)) => rationals[idx].evaluate(x),
            _ => 0.0, // unreachable in practice; both arms always set one
        }
    }

    /// Forward pass: `input` has shape `[n_in]`; returns `[n_out]`.
    pub fn forward(&self, input: &Array1<f64>) -> KanResult<Array1<f64>> {
        if input.len() != self.config.n_in {
            return Err(NeuralError::DimensionMismatch(format!(
                "KanLayer::forward expected n_in={} but got {}",
                self.config.n_in,
                input.len()
            )));
        }
        let mut output = Array1::zeros(self.config.n_out);
        for i in 0..self.config.n_in {
            let x_i = input[i];
            for j in 0..self.config.n_out {
                output[j] += self.eval_activation(i, j, x_i);
            }
        }
        Ok(output)
    }

    /// Batch forward pass: `input` has shape `[batch, n_in]`; returns `[batch, n_out]`.
    pub fn forward_batch(&self, input: &Array2<f64>) -> KanResult<Array2<f64>> {
        let (batch, n_in) = input.dim();
        if n_in != self.config.n_in {
            return Err(NeuralError::DimensionMismatch(format!(
                "KanLayer::forward_batch expected n_in={} but got {n_in}",
                self.config.n_in
            )));
        }
        let mut output = Array2::zeros((batch, self.config.n_out));
        for b in 0..batch {
            for i in 0..self.config.n_in {
                let x_i = input[(b, i)];
                for j in 0..self.config.n_out {
                    output[(b, j)] += self.eval_activation(i, j, x_i);
                }
            }
        }
        Ok(output)
    }

    /// Total number of learnable parameters across all edge activations.
    pub fn n_params(&self) -> usize {
        match (&self.spline_activations, &self.rational_activations) {
            (Some(splines), _) => splines.iter().map(|s| s.n_params()).sum(),
            (_, Some(rationals)) => rationals.iter().map(|r| r.n_params()).sum(),
            _ => 0,
        }
    }

    /// Number of input features.
    pub fn n_in(&self) -> usize {
        self.config.n_in
    }

    /// Number of output features.
    pub fn n_out(&self) -> usize {
        self.config.n_out
    }

    /// Prune low-importance edges by zeroing activations whose L1 norm is below
    /// `threshold`.
    ///
    /// Returns the number of pruned edges.
    pub fn prune_edges(&mut self, threshold: f64) -> usize {
        let mut pruned = 0;
        if let Some(ref mut splines) = self.spline_activations {
            for sp in splines.iter_mut() {
                let l1: f64 = sp.coefficients.iter().map(|c| c.abs()).sum();
                if l1 < threshold {
                    sp.coefficients.fill(0.0);
                    pruned += 1;
                }
            }
        }
        if let Some(ref mut rationals) = self.rational_activations {
            for ra in rationals.iter_mut() {
                let l1: f64 = ra
                    .p_coeffs
                    .iter()
                    .chain(ra.q_coeffs.iter())
                    .map(|c| c.abs())
                    .sum();
                if l1 < threshold {
                    ra.p_coeffs.fill(0.0);
                    ra.q_coeffs.fill(0.0);
                    pruned += 1;
                }
            }
        }
        pruned
    }

    /// Mutable access to spline activations (for parameter updates during training).
    pub fn spline_activations_mut(&mut self) -> Option<&mut Vec<BSplineActivation>> {
        self.spline_activations.as_mut()
    }

    /// Mutable access to rational activations (for parameter updates during training).
    pub fn rational_activations_mut(&mut self) -> Option<&mut Vec<RationalActivation>> {
        self.rational_activations.as_mut()
    }

    /// Read-only reference to the layer config.
    pub fn config(&self) -> &KanLayerConfig {
        &self.config
    }
}

// ---------------------------------------------------------------------------
// KanConfig
// ---------------------------------------------------------------------------

/// Configuration for a full KAN network (stack of KAN layers).
#[derive(Debug, Clone)]
pub struct KanConfig {
    /// Width of each layer including input and output.
    ///
    /// For example, `[2, 8, 4, 1]` means input dim 2, two hidden layers of
    /// widths 8 and 4, and scalar output.
    pub layer_widths: Vec<usize>,
    /// Activation type used for every layer.
    pub activation_type: ActivationType,
}

impl Default for KanConfig {
    fn default() -> Self {
        Self {
            layer_widths: vec![2, 8, 1],
            activation_type: ActivationType::default(),
        }
    }
}

// ---------------------------------------------------------------------------
// KanNetwork
// ---------------------------------------------------------------------------

/// A full KAN network consisting of stacked [`KanLayer`]s.
///
/// # Example
///
/// ```rust,ignore
/// use scirs2_neural::layers::kan::{KanConfig, KanNetwork, ActivationType, SplineConfig};
///
/// let config = KanConfig {
///     layer_widths: vec![2, 8, 1],
///     activation_type: ActivationType::BSpline(SplineConfig::default()),
/// };
/// let net = KanNetwork::new(config).unwrap();
/// ```
pub struct KanNetwork {
    layers: Vec<KanLayer>,
    config: KanConfig,
}

impl KanNetwork {
    /// Build a new `KanNetwork` from a [`KanConfig`].
    ///
    /// Returns an error if `layer_widths` has fewer than 2 entries or if any
    /// per-layer construction fails.
    pub fn new(config: KanConfig) -> KanResult<Self> {
        if config.layer_widths.len() < 2 {
            return Err(NeuralError::InvalidArgument(
                "KanNetwork: layer_widths must have at least 2 entries (input and output)"
                    .to_string(),
            ));
        }
        for (i, &w) in config.layer_widths.iter().enumerate() {
            if w == 0 {
                return Err(NeuralError::InvalidArgument(format!(
                    "KanNetwork: layer_widths[{i}] must be > 0"
                )));
            }
        }

        let mut layers = Vec::with_capacity(config.layer_widths.len() - 1);
        for pair in config.layer_widths.windows(2) {
            let layer_cfg = KanLayerConfig {
                n_in: pair[0],
                n_out: pair[1],
                activation_type: config.activation_type.clone(),
            };
            layers.push(KanLayer::new(layer_cfg)?);
        }

        Ok(Self { layers, config })
    }

    /// Forward pass through all layers: `input` shape `[layer_widths[0]]`;
    /// returns `[layer_widths.last()]`.
    pub fn forward(&self, input: &Array1<f64>) -> KanResult<Array1<f64>> {
        let mut x = input.clone();
        for layer in &self.layers {
            x = layer.forward(&x)?;
        }
        Ok(x)
    }

    /// Batch forward pass: `input` shape `[batch, layer_widths[0]]`;
    /// returns `[batch, layer_widths.last()]`.
    pub fn forward_batch(&self, input: &Array2<f64>) -> KanResult<Array2<f64>> {
        let mut x = input.clone();
        for layer in &self.layers {
            x = layer.forward_batch(&x)?;
        }
        Ok(x)
    }

    /// Total number of learnable parameters across all layers.
    pub fn n_params(&self) -> usize {
        self.layers.iter().map(|l| l.n_params()).sum()
    }

    /// Number of KAN layers (= `layer_widths.len() - 1`).
    pub fn n_layers(&self) -> usize {
        self.layers.len()
    }

    /// Prune low-importance edges across all layers.
    ///
    /// Returns the total number of pruned edges.
    pub fn prune_edges(&mut self, threshold: f64) -> usize {
        self.layers
            .iter_mut()
            .map(|l| l.prune_edges(threshold))
            .sum()
    }

    /// Read-only access to the underlying layers.
    pub fn layers(&self) -> &[KanLayer] {
        &self.layers
    }

    /// Mutable access to the underlying layers (for training).
    pub fn layers_mut(&mut self) -> &mut Vec<KanLayer> {
        &mut self.layers
    }

    /// Read-only reference to the network config.
    pub fn config(&self) -> &KanConfig {
        &self.config
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::layers::kan::rational::RationalConfig;
    use crate::layers::kan::spline::SplineConfig;

    fn default_spline_layer(n_in: usize, n_out: usize) -> KanLayer {
        KanLayer::new(KanLayerConfig {
            n_in,
            n_out,
            activation_type: ActivationType::BSpline(SplineConfig::default()),
        })
        .expect("valid layer config")
    }

    // ------------------------------------------------------------------
    // KanLayer tests
    // ------------------------------------------------------------------

    /// `forward` output has the correct length.
    #[test]
    fn kan_layer_forward_shape() {
        let layer = default_spline_layer(3, 5);
        let input = Array1::zeros(3);
        let output = layer.forward(&input).expect("forward ok");
        assert_eq!(output.len(), 5, "Output length mismatch");
    }

    /// `forward_batch` output has the correct shape.
    #[test]
    fn kan_layer_batch_forward_shape() {
        let layer = default_spline_layer(4, 6);
        let input = Array2::zeros((8, 4));
        let output = layer.forward_batch(&input).expect("batch forward ok");
        assert_eq!(output.dim(), (8, 6), "Batch output shape mismatch");
    }

    /// With zero spline coefficients all outputs are zero.
    #[test]
    fn kan_layer_zero_coeffs_outputs_zero() {
        let layer = default_spline_layer(3, 4);
        let input = Array1::from_vec(vec![0.5, -0.3, 0.9]);
        let output = layer.forward(&input).expect("forward ok");
        for (j, &v) in output.iter().enumerate() {
            assert!(
                v.abs() < 1e-14,
                "Expected 0 at output[{j}] but got {v}"
            );
        }
    }

    /// Mismatched input dimension returns an error.
    #[test]
    fn kan_layer_dimension_mismatch() {
        let layer = default_spline_layer(3, 4);
        let bad_input = Array1::zeros(5);
        assert!(
            layer.forward(&bad_input).is_err(),
            "Should return error on dimension mismatch"
        );
    }

    /// Single-input single-output layer works.
    #[test]
    fn kan_layer_single_input_single_output() {
        let layer = default_spline_layer(1, 1);
        let input = Array1::from_vec(vec![0.1]);
        let output = layer.forward(&input).expect("forward ok");
        assert_eq!(output.len(), 1);
    }

    /// Rational-config layer computes forward correctly.
    #[test]
    fn kan_rational_layer() {
        let config = KanLayerConfig {
            n_in: 3,
            n_out: 2,
            activation_type: ActivationType::Rational(RationalConfig::default()),
        };
        let layer = KanLayer::new(config).expect("valid rational layer");
        let input = Array1::from_vec(vec![0.1, -0.5, 0.8]);
        let output = layer.forward(&input).expect("forward ok");
        assert_eq!(output.len(), 2);
        // Zero coefficients → output is 0
        for (j, &v) in output.iter().enumerate() {
            assert!(v.abs() < 1e-14, "Expected 0 at output[{j}] got {v}");
        }
    }

    /// `prune_edges` returns the count of zeroed edges and output changes.
    #[test]
    fn kan_prune_edges() {
        let mut layer = default_spline_layer(2, 3);
        // All coefficients are zero => L1 = 0 < 1.0 => all edges pruned
        let pruned = layer.prune_edges(1.0);
        assert_eq!(pruned, 6, "All 2×3=6 edges should be pruned");

        // Now set some coefficients to be large enough to survive pruning
        if let Some(splines) = layer.spline_activations_mut() {
            splines[0].coefficients[0] = 10.0;
        }
        let pruned2 = layer.prune_edges(1.0);
        // Edge 0 has L1 = 10 >= 1, so it survives; the rest (5) are pruned
        assert_eq!(pruned2, 5);
    }

    // ------------------------------------------------------------------
    // KanNetwork tests
    // ------------------------------------------------------------------

    fn default_network() -> KanNetwork {
        KanNetwork::new(KanConfig {
            layer_widths: vec![2, 4, 3, 1],
            activation_type: ActivationType::BSpline(SplineConfig::default()),
        })
        .expect("valid network config")
    }

    /// `forward` output has the correct shape for a multi-layer network.
    #[test]
    fn kan_network_forward() {
        let net = default_network();
        let input = Array1::from_vec(vec![0.3, -0.7]);
        let output = net.forward(&input).expect("network forward ok");
        assert_eq!(output.len(), 1, "Output should be scalar (width=1)");
    }

    /// `forward_batch` output has the correct shape.
    #[test]
    fn kan_network_batch_forward() {
        let net = default_network();
        let input = Array2::zeros((5, 2));
        let output = net.forward_batch(&input).expect("batch forward ok");
        assert_eq!(output.dim(), (5, 1), "Batch output shape mismatch");
    }

    /// `n_params` is non-zero and proportional to the total number of activations.
    #[test]
    fn kan_network_n_params() {
        let net = default_network();
        let n = net.n_params();
        // widths [2,4,3,1]: (2*4 + 4*3 + 3*1) * 8 basis = (8 + 12 + 3) * 8 = 184
        assert_eq!(n, 184, "n_params mismatch: got {n}");
    }

    /// Config with fewer than 2 widths returns an error.
    #[test]
    fn kan_invalid_config_single_width() {
        let result = KanNetwork::new(KanConfig {
            layer_widths: vec![4],
            activation_type: ActivationType::default(),
        });
        assert!(result.is_err(), "Should fail with single-element layer_widths");
    }

    /// `n_layers` returns the correct number of layers.
    #[test]
    fn kan_network_n_layers() {
        let net = default_network();
        // widths [2,4,3,1] → 3 layers
        assert_eq!(net.n_layers(), 3);
    }

    /// Full prune with threshold > 0 zeroes all edges (all coefficients start at 0).
    #[test]
    fn kan_network_prune_all_zero() {
        let mut net = default_network();
        let pruned = net.prune_edges(1.0);
        // (2*4 + 4*3 + 3*1) = 23 edges, all with L1=0 < 1.0 → all pruned
        assert_eq!(pruned, 23, "All 23 edges should be pruned; got {pruned}");
    }
}