rustyml 0.11.0

A high-performance machine learning & deep learning library in pure Rust, offering ML algorithms and neural network support
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
#![cfg(feature = "neural_network")]

use approx::assert_abs_diff_eq;
use ndarray::Array;
use rustyml::neural_network::layer::activation_layer::linear::Linear;
use rustyml::neural_network::layer::dense::Dense;
use rustyml::neural_network::layer::layer_weight::LayerWeight;
use rustyml::neural_network::layer::regularization_layer::normalization_layer::batch_normalization::BatchNormalization;
use rustyml::neural_network::layer::regularization_layer::normalization_layer::layer_normalization::{LayerNormalization, LayerNormalizationAxis};
use rustyml::neural_network::layer::serialize_weight::SerializableLayerWeight;
use rustyml::neural_network::layer::TrainingParameters;
use rustyml::neural_network::loss_function::mean_squared_error::MeanSquaredError;
use rustyml::neural_network::neural_network_trait::Layer;
use rustyml::neural_network::optimizer::sgd::SGD;
use rustyml::neural_network::sequential::Sequential;

#[test]
fn test_layer_normalization_forward_pass_dimensions() {
    // Test forward propagation dimension correctness with default axis
    let mut ln =
        LayerNormalization::new(vec![4, 8], LayerNormalizationAxis::Default, 1e-5).unwrap();
    let input = Array::ones((4, 8)).into_dyn(); // batch_size=4, features=8

    let output = ln.forward(&input).unwrap();
    assert_eq!(output.shape(), &[4, 8]);
    println!(
        "Forward pass dimension test passed: {:?} -> {:?}",
        input.shape(),
        output.shape()
    );
}

#[test]
fn test_layer_normalization_default_axis() {
    // Test that layer normalization properly normalizes along the last axis (default)
    let mut ln =
        LayerNormalization::new(vec![3, 4], LayerNormalizationAxis::Default, 1e-5).unwrap();

    // Create input with non-zero mean and variance
    let input = Array::from_shape_vec(
        (3, 4),
        vec![
            1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
        ],
    )
    .unwrap()
    .into_dyn();

    ln.set_training(true);
    let output = ln.forward(&input).unwrap();

    // For layer normalization, each sample should be normalized independently
    let output_2d = output
        .as_standard_layout()
        .into_dimensionality::<ndarray::Ix2>()
        .unwrap();

    // Check that each sample (row) has approximately zero mean and unit variance
    for sample_idx in 0..3 {
        let sample_row = output_2d.row(sample_idx);
        let mean: f32 = sample_row.mean().unwrap();
        let variance: f32 = sample_row.mapv(|x| (x - mean).powi(2)).mean().unwrap();

        assert_abs_diff_eq!(mean, 0.0, epsilon = 1e-5);
        assert_abs_diff_eq!(variance, 1.0, epsilon = 1e-4);
    }

    println!("Default axis normalization test passed");
}

#[test]
fn test_layer_normalization_custom_axis() {
    // Test layer normalization with custom axis (axis 0)
    let mut ln =
        LayerNormalization::new(vec![3, 4], LayerNormalizationAxis::Custom(0), 1e-5).unwrap();

    let input = Array::from_shape_vec(
        (3, 4),
        vec![
            1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
        ],
    )
    .unwrap()
    .into_dyn();

    ln.set_training(true);
    let output = ln.forward(&input).unwrap();

    // When normalizing along axis 0, each column should be normalized
    let output_2d = output
        .as_standard_layout()
        .into_dimensionality::<ndarray::Ix2>()
        .unwrap();

    for col_idx in 0..4 {
        let col = output_2d.column(col_idx);
        let mean: f32 = col.mean().unwrap();
        let variance: f32 = col.mapv(|x| (x - mean).powi(2)).mean().unwrap();

        assert_abs_diff_eq!(mean, 0.0, epsilon = 1e-5);
        assert_abs_diff_eq!(variance, 1.0, epsilon = 1e-4);
    }

    println!("Custom axis normalization test passed");
}

#[test]
fn test_layer_normalization_invalid_axis() {
    // Test that invalid axis returns an error
    let mut ln =
        LayerNormalization::new(vec![3, 4], LayerNormalizationAxis::Custom(5), 1e-5).unwrap();

    let input = Array::ones((3, 4)).into_dyn();

    let result = ln.forward(&input);
    assert!(result.is_err(), "Should return error for invalid axis");
    println!("Invalid axis error handling test passed");
}

#[test]
fn test_layer_normalization_training_mode() {
    // Test that layer normalization works in training mode
    let mut ln =
        LayerNormalization::new(vec![2, 6], LayerNormalizationAxis::Default, 1e-5).unwrap();

    let input = Array::from_shape_vec(
        (2, 6),
        vec![
            10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 15.0, 25.0, 35.0, 45.0, 55.0, 65.0,
        ],
    )
    .unwrap()
    .into_dyn();

    ln.set_training(true);
    let output = ln.forward(&input).unwrap();

    assert_eq!(output.shape(), &[2, 6]);

    // Verify normalization properties
    let output_2d = output
        .as_standard_layout()
        .into_dimensionality::<ndarray::Ix2>()
        .unwrap();

    for sample_idx in 0..2 {
        let sample = output_2d.row(sample_idx);
        let mean: f32 = sample.mean().unwrap();
        assert_abs_diff_eq!(mean, 0.0, epsilon = 1e-5);
    }

    println!("Training mode test passed");
}

#[test]
fn test_layer_normalization_inference_mode() {
    // Test that layer normalization works in inference mode
    let mut ln =
        LayerNormalization::new(vec![2, 4], LayerNormalizationAxis::Default, 1e-5).unwrap();

    let input = Array::from_shape_vec((2, 4), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0])
        .unwrap()
        .into_dyn();

    // First do a forward pass in training mode
    ln.set_training(true);
    ln.forward(&input).unwrap();

    // Switch to inference mode
    ln.set_training(false);

    let test_input = Array::from_shape_vec((2, 4), vec![2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0])
        .unwrap()
        .into_dyn();

    let output = ln.forward(&test_input).unwrap();
    assert_eq!(output.shape(), &[2, 4]);

    println!("Inference mode test passed");
}

#[test]
fn test_layer_normalization_backward_pass() {
    // Test backward pass gradient computation
    let mut ln =
        LayerNormalization::new(vec![3, 4], LayerNormalizationAxis::Default, 1e-5).unwrap();

    let input = Array::from_shape_vec(
        (3, 4),
        vec![1.0, 2.0, 3.0, 4.0, 2.0, 3.0, 4.0, 5.0, 3.0, 4.0, 5.0, 6.0],
    )
    .unwrap()
    .into_dyn();

    ln.set_training(true);
    let _output = ln.forward(&input).unwrap();

    // Create gradient
    let grad_output = Array::ones((3, 4)).into_dyn();

    let grad_input = ln.backward(&grad_output).unwrap();

    // Verify gradient dimensions
    assert_eq!(grad_input.shape(), input.shape());

    // For layer normalization, the sum of gradients along the normalized axis should be approximately zero
    let grad_2d = grad_input
        .as_standard_layout()
        .into_dimensionality::<ndarray::Ix2>()
        .unwrap();

    for sample_idx in 0..3 {
        let sample_grad_sum: f32 = grad_2d.row(sample_idx).sum();
        assert_abs_diff_eq!(sample_grad_sum, 0.0, epsilon = 1e-4);
    }

    println!("Backward pass test passed");
}

#[test]
fn test_layer_normalization_parameter_update_sgd() {
    // Test parameter update with SGD optimizer
    let mut ln =
        LayerNormalization::new(vec![2, 3], LayerNormalizationAxis::Default, 1e-5).unwrap();

    let input = Array::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
        .unwrap()
        .into_dyn();

    ln.set_training(true);
    let _output = ln.forward(&input).unwrap();

    let grad_output = Array::ones((2, 3)).into_dyn();
    let _grad_input = ln.backward(&grad_output).unwrap();

    // Get initial weights
    if let LayerWeight::LayerNormalizationLayer(weights) = ln.get_weights() {
        let initial_gamma = weights.gamma.clone();
        let initial_beta = weights.beta.clone();

        // Update parameters
        ln.update_parameters_sgd(0.01);

        // Get updated weights
        if let LayerWeight::LayerNormalizationLayer(updated_weights) = ln.get_weights() {
            // Verify parameters have changed
            let gamma_changed = updated_weights
                .gamma
                .as_slice()
                .unwrap()
                .iter()
                .zip(initial_gamma.as_slice().unwrap().iter())
                .any(|(a, b)| (a - b).abs() > 1e-6);

            let beta_changed = updated_weights
                .beta
                .as_slice()
                .unwrap()
                .iter()
                .zip(initial_beta.as_slice().unwrap().iter())
                .any(|(a, b)| (a - b).abs() > 1e-6);

            assert!(
                gamma_changed || beta_changed,
                "Parameters should be updated"
            );
        }
    }

    println!("SGD parameter update test passed");
}

#[test]
fn test_layer_normalization_parameter_update_adam() {
    // Test parameter update with Adam optimizer
    let mut ln =
        LayerNormalization::new(vec![2, 3], LayerNormalizationAxis::Default, 1e-5).unwrap();

    let input = Array::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
        .unwrap()
        .into_dyn();

    ln.set_training(true);
    let _output = ln.forward(&input).unwrap();

    let grad_output = Array::ones((2, 3)).into_dyn();
    let _grad_input = ln.backward(&grad_output).unwrap();

    // Get initial weights
    if let LayerWeight::LayerNormalizationLayer(weights) = ln.get_weights() {
        let initial_gamma = weights.gamma.clone();
        let initial_beta = weights.beta.clone();

        // Update parameters with Adam
        ln.update_parameters_adam(0.001, 0.9, 0.999, 1e-8, 1);

        // Get updated weights
        if let LayerWeight::LayerNormalizationLayer(updated_weights) = ln.get_weights() {
            // Verify parameters have changed
            let gamma_changed = updated_weights
                .gamma
                .as_slice()
                .unwrap()
                .iter()
                .zip(initial_gamma.as_slice().unwrap().iter())
                .any(|(a, b)| (a - b).abs() > 1e-6);

            let beta_changed = updated_weights
                .beta
                .as_slice()
                .unwrap()
                .iter()
                .zip(initial_beta.as_slice().unwrap().iter())
                .any(|(a, b)| (a - b).abs() > 1e-6);

            assert!(
                gamma_changed || beta_changed,
                "Parameters should be updated"
            );
        }
    }

    println!("Adam parameter update test passed");
}

#[test]
fn test_layer_normalization_parameter_update_rmsprop() {
    // Test parameter update with RMSprop optimizer
    let mut ln =
        LayerNormalization::new(vec![2, 3], LayerNormalizationAxis::Default, 1e-5).unwrap();

    let input = Array::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
        .unwrap()
        .into_dyn();

    ln.set_training(true);
    let _output = ln.forward(&input).unwrap();

    let grad_output = Array::ones((2, 3)).into_dyn();
    let _grad_input = ln.backward(&grad_output).unwrap();

    // Get initial weights
    if let LayerWeight::LayerNormalizationLayer(weights) = ln.get_weights() {
        let initial_gamma = weights.gamma.clone();
        let initial_beta = weights.beta.clone();

        // Update parameters with RMSprop
        ln.update_parameters_rmsprop(0.001, 0.9, 1e-8);

        // Get updated weights
        if let LayerWeight::LayerNormalizationLayer(updated_weights) = ln.get_weights() {
            // Verify parameters have changed
            let gamma_changed = updated_weights
                .gamma
                .as_slice()
                .unwrap()
                .iter()
                .zip(initial_gamma.as_slice().unwrap().iter())
                .any(|(a, b)| (a - b).abs() > 1e-6);

            let beta_changed = updated_weights
                .beta
                .as_slice()
                .unwrap()
                .iter()
                .zip(initial_beta.as_slice().unwrap().iter())
                .any(|(a, b)| (a - b).abs() > 1e-6);

            assert!(
                gamma_changed || beta_changed,
                "Parameters should be updated"
            );
        }
    }

    println!("RMSprop parameter update test passed");
}

#[test]
fn test_layer_normalization_parameter_update_adagrad() {
    // Test parameter update with AdaGrad optimizer
    let mut ln =
        LayerNormalization::new(vec![2, 3], LayerNormalizationAxis::Default, 1e-5).unwrap();

    let input = Array::from_shape_vec((2, 3), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
        .unwrap()
        .into_dyn();

    ln.set_training(true);
    let _output = ln.forward(&input).unwrap();

    let grad_output = Array::ones((2, 3)).into_dyn();
    let _grad_input = ln.backward(&grad_output).unwrap();

    // Get initial weights
    if let LayerWeight::LayerNormalizationLayer(weights) = ln.get_weights() {
        let initial_gamma = weights.gamma.clone();
        let initial_beta = weights.beta.clone();

        // Update parameters with AdaGrad
        ln.update_parameters_ada_grad(0.01, 1e-8);

        // Get updated weights
        if let LayerWeight::LayerNormalizationLayer(updated_weights) = ln.get_weights() {
            // Verify parameters have changed
            let gamma_changed = updated_weights
                .gamma
                .as_slice()
                .unwrap()
                .iter()
                .zip(initial_gamma.as_slice().unwrap().iter())
                .any(|(a, b)| (a - b).abs() > 1e-6);

            let beta_changed = updated_weights
                .beta
                .as_slice()
                .unwrap()
                .iter()
                .zip(initial_beta.as_slice().unwrap().iter())
                .any(|(a, b)| (a - b).abs() > 1e-6);

            assert!(
                gamma_changed || beta_changed,
                "Parameters should be updated"
            );
        }
    }

    println!("AdaGrad parameter update test passed");
}

#[test]
fn test_layer_normalization_different_batch_sizes() {
    // Test layer normalization with different batch sizes
    let batch_sizes = vec![2, 4, 8, 16];
    let features = 5;

    for batch_size in batch_sizes {
        let mut ln = LayerNormalization::new(
            vec![batch_size, features],
            LayerNormalizationAxis::Default,
            1e-5,
        )
        .unwrap();
        let input =
            Array::from_shape_fn((batch_size, features), |(i, j)| (i * features + j) as f32)
                .into_dyn();

        ln.set_training(true);
        let output = ln.forward(&input).unwrap();

        assert_eq!(
            output.shape(),
            &[batch_size, features],
            "Output shape should match input shape"
        );

        println!(
            "Batch size {} test passed: {:?} -> {:?}",
            batch_size,
            input.shape(),
            output.shape()
        );
    }
}

#[test]
fn test_layer_normalization_parameter_count() {
    // Test parameter count correctness
    let ln = LayerNormalization::new(vec![4, 10], LayerNormalizationAxis::Default, 1e-5).unwrap();
    let expected_params = 10 + 10; // gamma + beta
    assert_eq!(
        ln.param_count(),
        TrainingParameters::Trainable(expected_params)
    );
    println!(
        "Parameter count test passed: {} parameters",
        expected_params
    );
}

#[test]
fn test_layer_normalization_layer_type() {
    // Test layer type identification
    let ln = LayerNormalization::new(vec![2, 3], LayerNormalizationAxis::Default, 1e-5).unwrap();
    assert_eq!(ln.layer_type(), "LayerNormalization");
    println!("Layer type test passed");
}

#[test]
fn test_layer_normalization_output_shape() {
    // Test output shape string formatting
    let ln = LayerNormalization::new(vec![4, 8], LayerNormalizationAxis::Default, 1e-5).unwrap();
    let output_shape = ln.output_shape();
    assert_eq!(output_shape, "(4, 8)");
    println!("Output shape test passed: {}", output_shape);
}

#[test]
fn test_layer_normalization_with_sequential_model() {
    // Test layer normalization integration with Sequential model
    let mut model = Sequential::new();
    model.add(Dense::new(4, 8, Linear::new()).unwrap());
    model.add(LayerNormalization::new(vec![2, 8], LayerNormalizationAxis::Default, 1e-5).unwrap());
    model.add(Dense::new(8, 1, Linear::new()).unwrap());
    model.compile(SGD::new(0.01).unwrap(), MeanSquaredError::new());

    let input = Array::ones((2, 4)).into_dyn();
    let target = Array::ones((2, 1)).into_dyn();

    // Test forward pass
    let output = model.predict(&input).unwrap();
    assert_eq!(output.shape(), &[2, 1]);

    // Test training
    let result = model.fit(&input, &target, 10);
    assert!(result.is_ok(), "Training should succeed");

    println!("Sequential model integration test passed");
}

#[test]
fn test_layer_normalization_set_weights() {
    // Test manual weight setting
    let mut ln =
        LayerNormalization::new(vec![2, 3], LayerNormalizationAxis::Default, 1e-5).unwrap();

    let new_gamma = Array::from_vec(vec![2.0, 2.0, 2.0]).into_dyn();
    let new_beta = Array::from_vec(vec![1.0, 1.0, 1.0]).into_dyn();

    ln.set_weights(new_gamma.clone(), new_beta.clone());

    // Verify weights were set correctly
    if let LayerWeight::LayerNormalizationLayer(weights) = ln.get_weights() {
        assert_eq!(
            weights.gamma.as_slice().unwrap(),
            new_gamma.as_slice().unwrap()
        );
        assert_eq!(
            weights.beta.as_slice().unwrap(),
            new_beta.as_slice().unwrap()
        );
    }

    println!("Set weights test passed");
}

#[test]
fn test_layer_normalization_3d_input() {
    // Test layer normalization with 3D input (batch, sequence, features)
    let batch_size = 2;
    let sequence_len = 4;
    let features = 3;

    let mut ln = LayerNormalization::new(
        vec![batch_size, sequence_len, features],
        LayerNormalizationAxis::Default,
        1e-5,
    )
    .unwrap();

    let input = Array::from_shape_fn((batch_size, sequence_len, features), |(i, j, k)| {
        (i * 100 + j * 10 + k) as f32
    })
    .into_dyn();

    ln.set_training(true);
    let output = ln.forward(&input).unwrap();

    assert_eq!(output.shape(), &[batch_size, sequence_len, features]);

    println!("3D input test passed");
}

#[test]
fn test_layer_normalization_epsilon_effect() {
    // Test that epsilon prevents division by zero
    let mut ln =
        LayerNormalization::new(vec![2, 3], LayerNormalizationAxis::Default, 1e-5).unwrap();

    // Create input with constant values (zero variance)
    let input = Array::from_shape_vec((2, 3), vec![5.0, 5.0, 5.0, 3.0, 3.0, 3.0])
        .unwrap()
        .into_dyn();

    ln.set_training(true);
    let result = ln.forward(&input);

    // Should not panic or produce NaN/Inf
    assert!(
        result.is_ok(),
        "Forward pass should succeed with zero variance"
    );

    let output = result.unwrap();
    assert!(
        output.iter().all(|&x| x.is_finite()),
        "Output should not contain NaN or Inf"
    );

    println!("Epsilon effect test passed");
}

#[test]
fn test_layer_normalization_vs_batch_normalization_difference() {
    // Demonstrate the difference between layer norm and batch norm
    let input = Array::from_shape_vec(
        (3, 4),
        vec![
            1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0,
        ],
    )
    .unwrap()
    .into_dyn();

    // Layer normalization (normalizes across features for each sample)
    let mut ln =
        LayerNormalization::new(vec![3, 4], LayerNormalizationAxis::Default, 1e-5).unwrap();
    ln.set_training(true);
    let ln_output = ln.forward(&input).unwrap();

    // Batch normalization (normalizes across batch for each feature)
    let mut bn = BatchNormalization::new(vec![3, 4], 0.9, 1e-5).unwrap();
    bn.set_training(true);
    let bn_output = bn.forward(&input).unwrap();

    // The outputs should be different
    let ln_2d = ln_output
        .as_standard_layout()
        .into_dimensionality::<ndarray::Ix2>()
        .unwrap();
    let bn_2d = bn_output
        .as_standard_layout()
        .into_dimensionality::<ndarray::Ix2>()
        .unwrap();

    let outputs_different = ln_2d
        .iter()
        .zip(bn_2d.iter())
        .any(|(a, b)| (a - b).abs() > 1e-5);

    assert!(
        outputs_different,
        "Layer norm and batch norm should produce different outputs"
    );

    println!("Layer norm vs batch norm difference test passed");
}

#[test]
fn test_layer_normalization_gradient_flow() {
    // Test that gradients flow properly through the layer
    let mut ln =
        LayerNormalization::new(vec![2, 4], LayerNormalizationAxis::Default, 1e-5).unwrap();

    let input = Array::from_shape_vec((2, 4), vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0])
        .unwrap()
        .into_dyn();

    ln.set_training(true);
    let _output = ln.forward(&input).unwrap();

    let grad_output = Array::from_shape_vec((2, 4), vec![0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8])
        .unwrap()
        .into_dyn();

    let grad_input = ln.backward(&grad_output).unwrap();

    // Verify gradients are not zero (gradients are flowing)
    let has_nonzero_grad = grad_input.iter().any(|&x| x.abs() > 1e-6);
    assert!(has_nonzero_grad, "Gradients should flow through the layer");

    // Verify no NaN or Inf in gradients
    assert!(
        grad_input.iter().all(|&x| x.is_finite()),
        "Gradients should be finite"
    );

    println!("Gradient flow test passed");
}

#[test]
fn test_layer_normalization_multiple_forward_backward() {
    // Test multiple forward and backward passes
    let mut ln =
        LayerNormalization::new(vec![2, 3], LayerNormalizationAxis::Default, 1e-5).unwrap();

    for i in 0..5 {
        let input = Array::from_shape_fn((2, 3), |(b, f)| (i + b + f) as f32).into_dyn();

        ln.set_training(true);
        let output = ln.forward(&input).unwrap();

        assert_eq!(output.shape(), &[2, 3]);

        let grad_output = Array::ones((2, 3)).into_dyn();
        let grad_input = ln.backward(&grad_output).unwrap();

        assert_eq!(grad_input.shape(), &[2, 3]);

        ln.update_parameters_sgd(0.01);
    }

    println!("Multiple forward-backward passes test passed");
}

#[test]
fn test_layer_normalization_serialization() {
    // Test weight serialization and deserialization
    let mut ln =
        LayerNormalization::new(vec![2, 3], LayerNormalizationAxis::Default, 1e-5).unwrap();

    // Do a forward pass to initialize
    let input = Array::ones((2, 3)).into_dyn();
    ln.set_training(true);
    ln.forward(&input).unwrap();

    // Get weights and convert to serializable format
    let weights = ln.get_weights();
    let serializable_weights = SerializableLayerWeight::from_layer_weight(&weights);

    // Verify we can convert back (this tests the serialization structure)
    match serializable_weights {
        SerializableLayerWeight::LayerNormalization(w) => {
            assert_eq!(w.gamma.len(), 3);
            assert_eq!(w.beta.len(), 3);
            assert_eq!(w.shape, vec![3]);
        }
        _ => panic!("Expected LayerNormalization weights"),
    }

    println!("Serialization test passed");
}