neurons 2.6.2

Neural networks from scratch, in Rust.
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
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
// Copyright (C) 2024 Hallvard Høyland Lavik

use std::collections::HashMap;

use crate::{activation, assert_eq_shape, network, optimizer, tensor};

#[derive(Clone)]
pub enum Accumulation {
    Add,
    Subtract,
    Multiply,
    Overwrite,
    Mean,
    // TODO: Expand?
}

impl std::fmt::Display for Accumulation {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Accumulation::Add => write!(f, "additive"),
            Accumulation::Subtract => write!(f, "subtractive"),
            Accumulation::Multiply => write!(f, "multiplicative"),
            Accumulation::Overwrite => write!(f, "overwrite"),
            Accumulation::Mean => write!(f, "mean"),
            #[allow(unreachable_patterns)]
            _ => unimplemented!("Accumulation method not implemented."),
        }
    }
}

/// A simplified layer definition used for defining feedback blocks.
///
/// # Dense
///
/// * `nodes` - The number of nodes in the layer.
/// * `activation` - The activation function of the layer.
/// * `bias` - Whether the layer should include a bias.
/// * `dropout` - The dropout rate of the layer.
///
/// # Convolution
///
/// * `filters` - The number of filters in the layer.
/// * `activation` - The activation function of the layer.
/// * `kernel` - The kernel size of the layer.
/// * `stride` - The stride of the layer.
/// * `padding` - The padding of the layer.
/// * `dilation` - The dilation of the layer.
/// * `dropout` - The dropout rate of the layer.
///
/// # Maxpool
///
/// * `kernel` - The pool size of the layer.
/// * `stride` - The stride of the layer.
pub enum Layer {
    Dense(usize, activation::Activation, bool, Option<f32>),
    Convolution(
        usize,
        activation::Activation,
        (usize, usize),
        (usize, usize),
        (usize, usize),
        (usize, usize),
        Option<f32>,
    ),
    Deconvolution(
        usize,
        activation::Activation,
        (usize, usize),
        (usize, usize),
        (usize, usize),
        Option<f32>,
    ),
    Maxpool((usize, usize), (usize, usize)),
}

/// A feedback block.
///
/// # Attributes
///
/// * `inputs` - The number of inputs to the block.
/// * `outputs` - The number of outputs from the block.
/// * `optimizer` - The optimizer used for training the block.
/// * `flatten` - Whether the block should flatten the output.
/// * `layers` - The layers of the block.
/// * `connect` - The (skip) connections between layers.
/// * `coupled` - The coupled layers of the block.
///
/// # Notes
///
/// * The `inputs` should match the `outputs`, to allow for feedback looping.
/// * TODO: Add support for differing input and output shapes, projecting differences internally.
#[derive(Clone)]
pub struct Feedback {
    pub(crate) inputs: tensor::Shape,
    pub(crate) outputs: tensor::Shape,
    pub(crate) optimizer: optimizer::Optimizer,
    pub(crate) flatten: bool,
    pub layers: Vec<network::Layer>,
    connect: HashMap<usize, Vec<usize>>,
    pub(crate) accumulation: Accumulation,
    coupled: Vec<Vec<usize>>,
}

impl std::fmt::Display for Feedback {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "Feedback (\n")?;
        write!(f, "\t\t\t{} -> {}\n", self.inputs, self.outputs)?;

        // let optimizer: String = self
        //     .optimizer
        //     .to_string()
        //     .lines()
        //     .map(|line| format!("\t\t{}", line))
        //     .collect::<Vec<String>>()
        //     .join("\n");
        // write!(f, "\t\t\toptimizer: (\n{}\n", optimizer)?;

        write!(f, "\t\t\tlayers: (\n")?;
        for (i, layer) in self.layers.iter().enumerate() {
            match layer {
                network::Layer::Dense(layer) => {
                    write!(
                        f,
                        "\t\t\t\t{}: Dense{} ({} -> {})\n",
                        i, layer.activation, layer.inputs, layer.outputs
                    )?;
                }
                network::Layer::Convolution(layer) => {
                    write!(
                        f,
                        "\t\t\t\t{}: Convolution{} ({} -> {})\n",
                        i, layer.activation, layer.inputs, layer.outputs
                    )?;
                }
                network::Layer::Deconvolution(layer) => {
                    write!(
                        f,
                        "\t\t\t\t{}: Decovolution{} ({} -> {})\n",
                        i, layer.activation, layer.inputs, layer.outputs
                    )?;
                }
                network::Layer::Maxpool(layer) => {
                    write!(
                        f,
                        "\t\t\t\t{}: Maxpool ({} -> {})\n",
                        i, layer.inputs, layer.outputs
                    )?;
                }
                network::Layer::Feedback(_) => panic!("Nested feedback blocks are not supported."),
            }
        }
        write!(f, "\t\t\t)\n")?;
        if !self.coupled.is_empty() {
            write!(f, "\t\t\tcoupled: (\n")?;
            for coupled in self.coupled.iter() {
                write!(f, "\t\t\t\t{:?}\n", coupled)?;
            }
            write!(f, "\t\t\t\taccumulation: {}\n", self.accumulation)?;
            write!(f, "\t\t\t)\n")?;
        }
        if !self.connect.is_empty() {
            write!(f, "\t\t\tconnections: (\n")?;
            write!(f, "\t\t\t\taccumulation: {}\n", self.accumulation)?;

            let mut entries: Vec<(&usize, &Vec<usize>)> = self.connect.iter().collect();
            entries.sort_by_key(|&(to, _)| to);
            for (to, from) in entries.iter() {
                write!(f, "\t\t\t\t{:?}.input -> {}.input\n", from, to)?;
            }
            write!(f, "\t\t\t)\n")?;
        }
        write!(f, "\t\t\tflatten: {}\n", self.flatten)?;
        write!(f, "\t\t)")?;
        Ok(())
    }
}

impl Feedback {
    /// Create a new feedback block.
    ///
    /// # Arguments
    ///
    /// * `layers` - The layers of the block.
    /// * `loops` - The number of loops the block should perform.
    /// * `inskips` - Whether the block should include input-to-input skip connections.
    /// * `outskips` - Whether the block should include output-to-output skip connections.
    /// * `accumulation` - The accumulation method of the block.
    pub fn create(
        mut layers: Vec<network::Layer>,
        loops: usize,
        inskips: bool,
        outskips: bool,
        accumulation: Accumulation,
    ) -> Self {
        assert!(loops > 0, "Feedback block should loop at least once.");
        let inputs = match layers.first().unwrap() {
            network::Layer::Dense(dense) => dense.inputs.clone(),
            network::Layer::Convolution(convolution) => convolution.inputs.clone(),
            network::Layer::Deconvolution(deconvolution) => deconvolution.inputs.clone(),
            network::Layer::Maxpool(maxpool) => maxpool.inputs.clone(),
            network::Layer::Feedback(_) => panic!("Nested feedback blocks are not supported."),
        };
        let outputs = match layers.last().unwrap() {
            network::Layer::Dense(dense) => dense.outputs.clone(),
            network::Layer::Convolution(convolution) => convolution.outputs.clone(),
            network::Layer::Deconvolution(deconvolution) => deconvolution.outputs.clone(),
            network::Layer::Maxpool(maxpool) => maxpool.outputs.clone(),
            network::Layer::Feedback(_) => panic!("Nested feedback blocks are not supported."),
        };
        assert_eq_shape!(inputs, outputs);

        let length = layers.len();

        // Extend the layers `loops` times.
        let _layers = layers.clone();
        for _ in 1..loops {
            layers.extend(_layers.clone());
        }

        // Define the coupled layers.
        let mut coupled: Vec<Vec<usize>> = Vec::new();
        for layer in 0..length {
            let mut coupling = Vec::new();
            for i in 0..loops {
                coupling.push(layer + i * length);
            }
            coupled.push(coupling);
        }

        // Define the skip connections.
        let mut connect: HashMap<usize, Vec<usize>> = HashMap::new();
        if inskips || outskips {
            let mut outputs = Vec::new();
            for i in 1..loops {
                if inskips {
                    // {to: from}
                    connect.insert(i * length, vec![0]);
                }
                if outskips {
                    outputs.push(i * length);
                }
            }
            if outskips {
                // {to: from}
                connect.insert(loops * length, outputs);
            }
        }

        Feedback {
            inputs,
            outputs,
            optimizer: optimizer::SGD::create(0.1, None),
            flatten: false,
            layers,
            connect,
            accumulation,
            coupled,
        }
    }

    /// Set the `optimizer::Optimizer` function of the network.
    ///
    /// # Arguments
    ///
    /// * `optimizer` - The reference to the network optimizer, to copy the values from.
    pub fn copy_optimizer(&mut self, mut optimizer: optimizer::Optimizer) {
        let mut vectors: Vec<Vec<Vec<tensor::Tensor>>> = Vec::new();
        for layer in self.layers.iter().rev() {
            match layer {
                network::Layer::Dense(layer) => {
                    let (output, input) = match &layer.weights.shape {
                        tensor::Shape::Double(output, input) => (*output, *input),
                        _ => panic!("Expected Dense shape"),
                    };
                    vectors.push(vec![vec![
                        tensor::Tensor::double(vec![vec![0.0; input]; output]),
                        if layer.bias.is_some() {
                            tensor::Tensor::single(vec![0.0; output])
                        } else {
                            tensor::Tensor::single(vec![])
                        },
                    ]]);
                }
                network::Layer::Convolution(layer) => {
                    let (ch, kh, kw) = match layer.kernels[0].shape {
                        tensor::Shape::Triple(ch, he, wi) => (ch, he, wi),
                        _ => panic!("Expected Convolution shape"),
                    };
                    vectors.push(vec![
                        vec![
                            tensor::Tensor::triple(vec![vec![vec![0.0; kw]; kh]; ch]),
                            // TODO: Add bias term here.
                        ];
                        layer.kernels.len()
                    ]);
                }
                network::Layer::Deconvolution(layer) => {
                    let (ch, kh, kw) = match layer.kernels[0].shape {
                        tensor::Shape::Triple(ch, he, wi) => (ch, he, wi),
                        _ => panic!("Expected Convolution shape"),
                    };
                    vectors.push(vec![
                        vec![
                            tensor::Tensor::triple(vec![vec![vec![0.0; kw]; kh]; ch]),
                            // TODO: Add bias term here.
                        ];
                        layer.kernels.len()
                    ]);
                }
                network::Layer::Maxpool(_) => {
                    vectors.push(vec![vec![tensor::Tensor::single(vec![0.0; 0])]])
                }
                _ => unimplemented!("Feedback blocks not yet implemented."),
            }
        }

        // Validate the optimizers' parameters.
        // Override to default values if wrongly set.
        optimizer.validate(vectors);

        self.optimizer = optimizer;
    }

    /// Count the number of parameters.
    /// Only counts the parameters of the first loop, as the rest are identical (coupled).
    pub fn parameters(&self) -> usize {
        let mut parameters = 0;
        for idx in 0..self.coupled.len() {
            parameters += match &self.layers[idx] {
                network::Layer::Dense(dense) => dense.parameters(),
                network::Layer::Convolution(convolution) => convolution.parameters(),
                network::Layer::Deconvolution(deconvolution) => deconvolution.parameters(),
                network::Layer::Maxpool(_) => 0,
                network::Layer::Feedback(_) => panic!("Nested feedback blocks are not supported."),
            };
        }
        parameters
    }

    pub fn training(&mut self, train: bool) {
        self.layers.iter_mut().for_each(|layer| match layer {
            network::Layer::Dense(layer) => layer.training = train,
            network::Layer::Convolution(layer) => layer.training = train,
            network::Layer::Deconvolution(layer) => layer.training = train,
            network::Layer::Maxpool(_) => {}
            network::Layer::Feedback(_) => panic!("Nested feedback blocks are not supported."),
        });
    }

    /// Compute the forward pass of the feedback block for the given input, including all
    /// intermediate pre- and post-activation values.
    ///
    /// # Arguments
    ///
    /// * `input` - The input data (x).
    ///
    /// # Returns
    ///
    /// * Unactivated tensor to be used for neighbouring layers when backpropagating.
    /// * Activated tensor to be used for neighbouring layers when backpropagating.
    /// * Maxpool tensor to be used for neighbouring layers when backpropagating.
    /// * Intermediate unactivated tensors (nested).
    /// * Intermediate activated tensors (nested).
    pub fn forward(
        &self,
        input: &tensor::Tensor,
    ) -> (
        tensor::Tensor,
        tensor::Tensor,
        tensor::Tensor,
        tensor::Tensor,
        tensor::Tensor,
    ) {
        let mut unactivated = Vec::with_capacity(self.layers.len());
        let mut activated = Vec::with_capacity(self.layers.len() + 1);
        let mut maxpools = Vec::with_capacity(self.layers.len());

        activated.push(input.clone());

        for (i, layer) in self.layers.iter().enumerate() {
            let mut x = activated.last().unwrap().clone();

            // Check if the layer should account for a skip connection.
            if self.connect.contains_key(&i) {
                match self.accumulation {
                    Accumulation::Add => {
                        for idx in self.connect.get(&i).unwrap() {
                            x.add_inplace(&activated[*idx]);
                        }
                    }
                    Accumulation::Subtract => {
                        for idx in self.connect.get(&i).unwrap() {
                            x.sub_inplace(&activated[*idx]);
                        }
                    }
                    Accumulation::Multiply => {
                        for idx in self.connect.get(&i).unwrap() {
                            x.mul_inplace(&activated[*idx]);
                        }
                    }
                    Accumulation::Overwrite => {
                        x = activated[*self.connect.get(&i).unwrap().last().unwrap()].clone();
                    }
                    Accumulation::Mean => {
                        let mut _x: Vec<&tensor::Tensor> = Vec::new();
                        for idx in self.connect.get(&i).unwrap() {
                            _x.push(&activated[*idx]);
                        }
                        x.mean_inplace(&_x);
                    }
                    #[allow(unreachable_patterns)]
                    _ => unimplemented!("Accumulation method not implemented."),
                }
            }

            let (pre, post, max) = match layer {
                network::Layer::Dense(layer) => {
                    assert_eq_shape!(layer.inputs, x.shape);
                    let (pre, post) = layer.forward(&x);
                    (pre, post, None)
                }
                network::Layer::Convolution(layer) => {
                    assert_eq_shape!(layer.inputs, x.shape);
                    let (pre, post) = layer.forward(&x);
                    (pre, post, None)
                }
                network::Layer::Deconvolution(layer) => {
                    assert_eq_shape!(layer.inputs, x.shape);
                    let (pre, post) = layer.forward(&x);
                    (pre, post, None)
                }
                network::Layer::Maxpool(layer) => {
                    assert_eq_shape!(layer.inputs, x.shape);
                    let (pre, post, max) = layer.forward(&x);
                    (pre, post, Some(max))
                }
                network::Layer::Feedback(_) => panic!("Nested feedback blocks are not supported."),
            };

            unactivated.push(pre);
            activated.push(post);
            maxpools.push(max);
        }

        let mut last = activated.pop().unwrap();

        // Check if the last layer should account for a skip connection.
        if self.connect.contains_key(&self.layers.len()) {
            let i = self.layers.len();
            match self.accumulation {
                Accumulation::Add => {
                    for idx in self.connect.get(&i).unwrap() {
                        last.add_inplace(&activated[*idx]);
                    }
                }
                Accumulation::Subtract => {
                    for idx in self.connect.get(&i).unwrap() {
                        last.sub_inplace(&activated[*idx]);
                    }
                }
                Accumulation::Multiply => {
                    for idx in self.connect.get(&i).unwrap() {
                        last.mul_inplace(&activated[*idx]);
                    }
                }
                Accumulation::Overwrite => {
                    last = activated[*self.connect.get(&i).unwrap().last().unwrap()].clone();
                }
                Accumulation::Mean => {
                    let mut _x: Vec<&tensor::Tensor> = Vec::new();
                    for idx in self.connect.get(&i).unwrap() {
                        _x.push(&activated[*idx]);
                    }
                    last.mean_inplace(&_x);
                }
                #[allow(unreachable_patterns)]
                _ => unimplemented!("Accumulation method not implemented."),
            }
        }

        // Flattening the last output if specified.
        if self.flatten {
            activated.push(last.flatten());
        } else {
            activated.push(last);
        }

        (
            unactivated[0].clone(),
            activated[activated.len() - 1].clone(),
            tensor::Tensor::nestedoptional(maxpools),
            tensor::Tensor::nested(unactivated),
            tensor::Tensor::nested(activated),
        )
    }

    /// Applies the backward pass of the layer to the gradient vector.
    ///
    /// # Arguments
    ///
    /// * `gradient` - The gradient tensor::Tensor to the layer.
    /// * `inbetween` - The intermediate tensors of the forward pass.
    ///
    /// # Returns
    ///
    /// The input-, weight- and bias gradient of the layer.
    pub fn backward(
        &self,
        gradient: &tensor::Tensor,
        inbetween: &Vec<tensor::Tensor>,
    ) -> (tensor::Tensor, tensor::Tensor, Option<tensor::Tensor>) {
        // We need to un-nest the input and output tensors (see `forward`).
        let unactivated = inbetween[0].unnested();
        let activated = inbetween[1].unnested();

        let mut gradients: Vec<tensor::Tensor> = vec![gradient.clone()];
        let mut weight_gradients: Vec<tensor::Tensor> = Vec::new();
        let mut bias_gradients: Vec<Option<tensor::Tensor>> = Vec::new();

        let mut connect: HashMap<usize, Vec<usize>> = HashMap::new();
        for (key, value) in self.connect.iter() {
            for idx in value.iter() {
                // {to: from} -> {from: [to1, ...]}
                if connect.contains_key(idx) {
                    connect.get_mut(idx).unwrap().push(*key);
                } else {
                    connect.insert(*idx, vec![*key]);
                }
            }
        }

        self.layers.iter().rev().enumerate().for_each(|(i, layer)| {
            let idx = self.layers.len() - i - 1;

            let input: &tensor::Tensor = &activated[idx];
            let output: &tensor::Tensor = &unactivated[idx];

            // Check for skip connections.
            // Add the gradient of the skip connection to the current gradient.
            if connect.contains_key(&idx) {
                for j in connect[&idx].iter() {
                    let mut idx = *j;
                    if j == &self.layers.len() {
                        // If the skip connection is the last layer (i.e., output);
                        // * Account for this by using the output gradient (i.e., gradients[0]).
                        // * Equivalent to `layers.len() - (layers.len() - 1) - 1 = 0` (below).
                        idx = idx - 1;
                    }
                    let gradient = gradients[self.layers.len() - idx - 1].clone();
                    gradients.last_mut().unwrap().add_inplace(&gradient);
                }
                // TODO: Handle accumulation methods.
            }

            let (gradient, wg, bg) = match layer {
                network::Layer::Dense(layer) => {
                    layer.backward(&gradients.last().unwrap(), input, output)
                }
                network::Layer::Convolution(layer) => {
                    layer.backward(&gradients.last().unwrap(), input, output)
                }
                network::Layer::Deconvolution(layer) => {
                    layer.backward(&gradients.last().unwrap(), input, output)
                }
                _ => panic!("Unsupported layer type."),
            };

            gradients.push(gradient);
            weight_gradients.push(wg);
            bias_gradients.push(bg);
        });

        return (
            gradients.last().unwrap().clone(),
            tensor::Tensor::nested(weight_gradients),
            Some(tensor::Tensor::nestedoptional(bias_gradients)),
        );
    }

    pub fn update(
        &mut self,
        stepnr: i32,
        weight_gradients: &mut tensor::Tensor,
        bias_gradients: &mut tensor::Tensor,
    ) {
        let mut weight_gradients = weight_gradients.unnested();
        let mut bias_gradients = bias_gradients.unnestedoptional();

        // Update the weights and biases of the layers.
        self.layers
            .iter_mut()
            .rev()
            .enumerate()
            .for_each(|(i, layer)| match layer {
                network::Layer::Dense(layer) => {
                    self.optimizer.update(
                        i,
                        0,
                        false,
                        stepnr,
                        &mut layer.weights,
                        &mut weight_gradients[i],
                    );

                    if let Some(bias) = &mut layer.bias {
                        self.optimizer.update(
                            i,
                            0,
                            true,
                            stepnr,
                            bias,
                            &mut bias_gradients[i].as_mut().unwrap(),
                        )
                    }
                }
                network::Layer::Convolution(layer) => {
                    for (f, (filter, gradient)) in layer
                        .kernels
                        .iter_mut()
                        .zip(weight_gradients[i].quadruple_to_vec_triple().iter_mut())
                        .enumerate()
                    {
                        self.optimizer.update(i, f, false, stepnr, filter, gradient);
                        // TODO: Add bias term here.
                    }
                }
                network::Layer::Deconvolution(layer) => {
                    for (f, (filter, gradient)) in layer
                        .kernels
                        .iter_mut()
                        .zip(weight_gradients[i].quadruple_to_vec_triple().iter_mut())
                        .enumerate()
                    {
                        self.optimizer.update(i, f, false, stepnr, filter, gradient);
                        // TODO: Add bias term here.
                    }
                }
                network::Layer::Maxpool(_) => {}
                network::Layer::Feedback(_) => panic!("Feedback layers are not supported."),
            });

        // Couple respective layers.
        // Iterates through `self.coupled` and updates the weights and biases to match.
        for couple in self.coupled.iter() {
            let mut count: f32 = 0.0;
            let mut weights: Vec<tensor::Tensor> = Vec::new();
            let mut biases: Vec<tensor::Tensor> = Vec::new();

            // Add the weights and biases of the coupled layers.
            for idx in couple.iter() {
                match &self.layers[*idx] {
                    network::Layer::Dense(layer) => {
                        weights.push(layer.weights.clone());
                        if let Some(bias) = &layer.bias {
                            biases.push(bias.clone());
                        }
                    }
                    network::Layer::Convolution(layer) => {
                        weights.push(tensor::Tensor::nested(layer.kernels.clone()));
                    }
                    network::Layer::Deconvolution(layer) => {
                        weights.push(tensor::Tensor::nested(layer.kernels.clone()));
                    }
                    _ => continue,
                }
                count += 1.0;
            }

            let mut weight: tensor::Tensor = weights.remove(0);
            let mut bias: Option<tensor::Tensor> = if biases.is_empty() {
                None
            } else {
                Some(biases.remove(0))
            };
            match self.accumulation {
                Accumulation::Add => {
                    for w in weights.iter() {
                        weight.add_inplace(w);
                    }
                    if let Some(bias) = &mut bias {
                        for b in biases.iter() {
                            bias.add_inplace(b);
                        }
                    }
                }
                Accumulation::Multiply => {
                    for w in weights.iter() {
                        weight.mul_inplace(w);
                    }
                    if let Some(bias) = &mut bias {
                        for b in biases.iter() {
                            bias.mul_inplace(b);
                        }
                    }
                }
                Accumulation::Subtract => {
                    for w in weights.iter() {
                        weight.sub_inplace(w);
                    }
                    if let Some(bias) = &mut bias {
                        for b in biases.iter() {
                            bias.sub_inplace(b);
                        }
                    }
                }
                Accumulation::Mean => {
                    for w in weights.iter() {
                        weight.add_inplace(w);
                    }
                    if let Some(bias) = &mut bias {
                        for b in biases.iter() {
                            bias.add_inplace(b);
                        }
                    }
                    weight.div_scalar_inplace(count);
                    if let Some(b) = &mut bias {
                        b.div_scalar_inplace(count);
                    }
                }
                Accumulation::Overwrite => {
                    // Do nothing?
                    unimplemented!("Overwrite accumulation is not implemented.")
                }
            }

            // Update the weights and biases of the coupled layers.
            for i in couple.iter() {
                match &mut self.layers[*i] {
                    network::Layer::Dense(layer) => {
                        layer.weights = weight.clone();
                        if let Some(b) = &mut layer.bias {
                            *b = bias.clone().unwrap();
                        }
                    }
                    network::Layer::Convolution(layer) => {
                        layer.kernels = weight.unnested();
                    }
                    network::Layer::Deconvolution(layer) => {
                        layer.kernels = weight.unnested();
                    }
                    _ => continue,
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{activation, assert_eq_data, assert_eq_shape, dense, network, tensor};

    #[test]
    fn test_feedback_create() {
        let layers = vec![
            network::Layer::Dense(dense::Dense::create(
                tensor::Shape::Single(2),
                tensor::Shape::Single(2),
                &activation::Activation::ReLU,
                false,
                None,
            )),
            network::Layer::Dense(dense::Dense::create(
                tensor::Shape::Single(2),
                tensor::Shape::Single(2),
                &activation::Activation::ReLU,
                false,
                None,
            )),
        ];
        let feedback = Feedback::create(layers.clone(), 2, true, false, Accumulation::Add);

        assert_eq!(feedback.inputs, tensor::Shape::Single(2));
        assert_eq!(feedback.outputs, tensor::Shape::Single(2));
        assert_eq!(feedback.layers.len(), 4); // 2 loops of 2 layers each
        assert_eq!(feedback.coupled.len(), 2);
        assert_eq!(feedback.connect.len(), 1);
    }

    // #[test]
    // fn test_feedback_copy_optimizer() {
    //     let layers = vec![network::Layer::Dense(dense::Dense::create(
    //         tensor::Shape::Single(2),
    //         tensor::Shape::Single(2),
    //         &activation::Activation::ReLU,
    //         false,
    //         None,
    //     ))];
    //     let mut feedback = Feedback::create(layers.clone(), 1, false, false, Accumulation::Add);
    //     let optimizer = optimizer::SGD::create(0.1, None);
    //     feedback.copy_optimizer(optimizer.clone());

    //     assert_eq!(feedback.optimizer, optimizer);
    // }

    #[test]
    fn test_feedback_parameters() {
        let layers = vec![network::Layer::Dense(dense::Dense::create(
            tensor::Shape::Single(3),
            tensor::Shape::Single(3),
            &activation::Activation::ReLU,
            true,
            None,
        ))];
        let feedback = Feedback::create(layers.clone(), 1, false, false, Accumulation::Add);

        assert_eq!(feedback.parameters(), 12); // 9 weights + 3 biases
    }

    #[test]
    fn test_feedback_training() {
        let layers = vec![network::Layer::Dense(dense::Dense::create(
            tensor::Shape::Single(3),
            tensor::Shape::Single(3),
            &activation::Activation::ReLU,
            true,
            None,
        ))];
        let mut feedback = Feedback::create(layers.clone(), 1, false, false, Accumulation::Add);
        feedback.training(true);

        for layer in feedback.layers.iter() {
            if let network::Layer::Dense(layer) = layer {
                assert!(layer.training);
            }
        }
    }

    #[test]
    fn test_feedback_forward() {
        let mut layer = dense::Dense::create(
            tensor::Shape::Single(3),
            tensor::Shape::Single(3),
            &activation::Activation::ReLU,
            true,
            None,
        );
        layer.weights = tensor::Tensor::double(vec![vec![1.0; 3]; 3]);
        layer.bias = Some(tensor::Tensor::single(vec![0.0; 3]));
        let layers = vec![network::Layer::Dense(layer)];
        let feedback = Feedback::create(layers.clone(), 1, false, false, Accumulation::Add);
        let input = tensor::Tensor::single(vec![-1.0, 2.0, 3.0]);

        let (unactivated, activated, maxpool, intermediate_unactivated, intermediate_activated) =
            feedback.forward(&input);

        assert_eq_shape!(unactivated.shape, tensor::Shape::Single(3));
        assert_eq_shape!(activated.shape, tensor::Shape::Single(3));
        assert_eq_shape!(maxpool.shape, tensor::Shape::Nested(1));
        assert_eq_shape!(
            intermediate_unactivated.shape,
            tensor::Tensor::nested(vec![tensor::Tensor::single(vec![1.0; 3]),]).shape
        );
        assert_eq_shape!(
            intermediate_activated.shape,
            tensor::Tensor::nested(vec![
                tensor::Tensor::single(vec![1.0; 3]),
                tensor::Tensor::single(vec![1.0; 3]),
            ])
            .shape
        );

        // Check actual values
        let expected_unactivated = tensor::Tensor::single(vec![4.0; 3]);
        let expected_activated = tensor::Tensor::single(vec![4.0; 3]);
        assert_eq_data!(unactivated.data, expected_unactivated.data);
        assert_eq_data!(activated.data, expected_activated.data);
    }

    #[test]
    fn test_feedback_backward() {
        let mut layer = dense::Dense::create(
            tensor::Shape::Single(3),
            tensor::Shape::Single(3),
            &activation::Activation::ReLU,
            true,
            None,
        );
        layer.weights = tensor::Tensor::double(vec![vec![1.0; 3]; 3]);
        layer.bias = Some(tensor::Tensor::single(vec![0.0; 3]));
        let layers = vec![network::Layer::Dense(layer)];
        let feedback = Feedback::create(layers.clone(), 1, false, false, Accumulation::Add);
        let input = tensor::Tensor::single(vec![1.0, 2.0, 3.0]);
        let (_, _, _, intermediate_unactivated, intermediate_activated) = feedback.forward(&input);
        let gradient = tensor::Tensor::single(vec![0.1, 0.2, 0.3]);

        let (input_gradient, weight_gradient, bias_gradient) = feedback.backward(
            &gradient,
            &vec![intermediate_unactivated, intermediate_activated],
        );

        assert_eq_shape!(input_gradient.shape, tensor::Shape::Single(3));
        assert_eq!(
            weight_gradient.shape,
            tensor::Tensor::nested(vec![tensor::Tensor::double(vec![vec![1.0; 3]; 2]),]).shape
        );
        assert_eq!(
            bias_gradient.clone().unwrap().shape,
            tensor::Tensor::nested(vec![tensor::Tensor::single(vec![1.0; 3]),]).shape
        );

        // Check actual values
        let expected_input_gradient = tensor::Tensor::single(vec![0.6, 0.6, 0.6]);
        let expected_weight_gradient = tensor::Tensor::nested(vec![tensor::Tensor::double(vec![
            vec![0.1 * 1.0, 0.1 * 2.0, 0.1 * 3.0],
            vec![0.2 * 1.0, 0.2 * 2.0, 0.2 * 3.0],
            vec![0.3 * 1.0, 0.3 * 2.0, 0.3 * 3.0],
        ])]);
        let expected_bias_gradient = tensor::Tensor::single(vec![0.1, 0.2, 0.3]);

        assert_eq_data!(input_gradient.data, expected_input_gradient.data);
        assert_eq_data!(
            weight_gradient.unnested()[0].data,
            expected_weight_gradient.unnested()[0].data
        );
        assert_eq_data!(
            bias_gradient.clone().unwrap().unnestedoptional()[0]
                .clone()
                .unwrap()
                .data,
            expected_bias_gradient.data
        );
    }

    #[test]
    fn test_feedback_update() {
        let layers = vec![network::Layer::Dense(dense::Dense::create(
            tensor::Shape::Single(3),
            tensor::Shape::Single(3),
            &activation::Activation::ReLU,
            true,
            None,
        ))];
        let mut weight_gradient = tensor::Tensor::nested(vec![
            tensor::Tensor::double(vec![
                vec![0.1, 0.2, 0.3],
                vec![0.4, 0.5, 0.6],
                vec![0.7, 0.8, 0.9],
            ]),
            tensor::Tensor::double(vec![
                vec![0.1, 0.2, 0.3],
                vec![0.7, 0.8, 0.9],
                vec![0.4, 0.5, 0.6],
            ]),
            tensor::Tensor::double(vec![
                vec![0.7, 0.8, 0.9],
                vec![0.1, 0.2, 0.3],
                vec![0.4, 0.5, 0.6],
            ]),
        ]);
        let mut bias_gradient = tensor::Tensor::nestedoptional(vec![
            Some(tensor::Tensor::single(vec![0.1, 0.2, 0.3])),
            Some(tensor::Tensor::single(vec![0.5, 0.7, 1.0])),
            Some(tensor::Tensor::single(vec![1.1, 1.2, 0.3])),
        ]);

        for accumulation in vec![
            Accumulation::Add,
            Accumulation::Subtract,
            Accumulation::Multiply,
            // Accumulation::Overwrite,
            Accumulation::Mean,
        ] {
            let mut feedback =
                Feedback::create(layers.clone(), 3, false, false, accumulation.clone());
            feedback.update(1, &mut weight_gradient, &mut bias_gradient);

            let (weight, bias) = match &feedback.layers[0] {
                network::Layer::Dense(layer) => (layer.weights.clone(), layer.bias.clone()),
                _ => panic!("Invalid layer type"),
            };

            // Check if weights and biases have been updated
            for i in 0..3 {
                match &feedback.layers[i] {
                    network::Layer::Dense(layer) => {
                        assert_eq_data!(layer.weights.data, weight.data);
                        if let Some(bias) = &bias {
                            assert_eq_data!(layer.bias.clone().unwrap().data, bias.data);
                        } else {
                            panic!("Should have bias!");
                        }
                    }
                    _ => panic!("Invalid layer type"),
                }
            }
        }
    }
}