scirs2-neural 0.3.3

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
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
//! Patch Embedding layer for Vision Transformers
//!
//! Implements the patch embedding operation used in ViT and related architectures.
//! An input image `[batch, channels, height, width]` is divided into non-overlapping
//! `patch_size × patch_size` patches, which are then linearly projected to an
//! embedding space of dimension `embed_dim`, yielding `[batch, num_patches, embed_dim]`.
//!
//! Reference: "An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale",
//! Dosovitskiy et al. (2020). <https://arxiv.org/abs/2010.11929>

use crate::error::{NeuralError, Result};
use crate::layers::{Layer, ParamLayer};
use scirs2_core::ndarray::{Array, IxDyn, ScalarOperand};
use scirs2_core::numeric::{Float, NumAssign};
use scirs2_core::random::{Distribution, Rng, RngExt, Uniform};
use std::fmt::Debug;
use std::sync::RwLock;

/// Patch Embedding layer.
///
/// Divides an image into fixed-size patches and projects each patch to an embedding
/// vector via a learned linear projection (optionally including a bias term).
///
/// # Shape
/// - Input: `[batch_size, in_channels, image_height, image_width]`
/// - Output: `[batch_size, num_patches, embed_dim]`
///   where `num_patches = (image_height / patch_size) * (image_width / patch_size)`
///
/// # Parameters
/// - `weight`: shape `[embed_dim, in_channels * patch_h * patch_w]` — the projection matrix
/// - `bias` (optional): shape `[embed_dim]` — per-embedding bias
pub struct PatchEmbedding<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign> {
    /// Image size `(height, width)` expected as input
    image_size: (usize, usize),
    /// Patch size `(height, width)`
    patch_size: (usize, usize),
    /// Number of input channels
    in_channels: usize,
    /// Output embedding dimension
    embed_dim: usize,
    /// Number of patches along height
    num_patches_h: usize,
    /// Number of patches along width
    num_patches_w: usize,
    /// Flat patch dimension: `in_channels * patch_h * patch_w`
    patch_dim: usize,

    /// Projection weight matrix, shape `[embed_dim, patch_dim]`
    weight: Array<F, IxDyn>,
    /// Projection bias, shape `[embed_dim]` (optional but always allocated as zeros)
    bias: Array<F, IxDyn>,
    /// Whether a bias term is used
    use_bias: bool,

    /// Gradient for weight
    d_weight: RwLock<Array<F, IxDyn>>,
    /// Gradient for bias
    d_bias: RwLock<Array<F, IxDyn>>,
    /// Cached input from most recent forward pass (for backward)
    cached_patches: RwLock<Option<Array<F, IxDyn>>>,
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> Debug
    for PatchEmbedding<F>
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("PatchEmbedding")
            .field("image_size", &self.image_size)
            .field("patch_size", &self.patch_size)
            .field("in_channels", &self.in_channels)
            .field("embed_dim", &self.embed_dim)
            .field(
                "num_patches",
                &(self.num_patches_h * self.num_patches_w),
            )
            .field("use_bias", &self.use_bias)
            .finish()
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> Clone
    for PatchEmbedding<F>
{
    fn clone(&self) -> Self {
        Self {
            image_size: self.image_size,
            patch_size: self.patch_size,
            in_channels: self.in_channels,
            embed_dim: self.embed_dim,
            num_patches_h: self.num_patches_h,
            num_patches_w: self.num_patches_w,
            patch_dim: self.patch_dim,
            weight: self.weight.clone(),
            bias: self.bias.clone(),
            use_bias: self.use_bias,
            d_weight: RwLock::new(
                self.d_weight
                    .read()
                    .expect("RwLock poisoned on d_weight read")
                    .clone(),
            ),
            d_bias: RwLock::new(
                self.d_bias
                    .read()
                    .expect("RwLock poisoned on d_bias read")
                    .clone(),
            ),
            cached_patches: RwLock::new(
                self.cached_patches
                    .read()
                    .expect("RwLock poisoned on cached_patches read")
                    .clone(),
            ),
        }
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> PatchEmbedding<F> {
    /// Create a new PatchEmbedding layer.
    ///
    /// # Arguments
    /// * `image_size` — expected input image dimensions `(height, width)`
    /// * `patch_size` — patch dimensions `(height, width)` — must divide `image_size` evenly
    /// * `in_channels` — number of input image channels
    /// * `embed_dim` — output embedding dimension
    /// * `use_bias` — whether to include a learnable bias term
    /// * `rng` — random number generator for weight initialisation
    pub fn new<R: Rng>(
        image_size: (usize, usize),
        patch_size: (usize, usize),
        in_channels: usize,
        embed_dim: usize,
        use_bias: bool,
        rng: &mut R,
    ) -> Result<Self> {
        if image_size.0 == 0 || image_size.1 == 0 {
            return Err(NeuralError::InvalidArchitecture(
                "image_size dimensions must be non-zero".to_string(),
            ));
        }
        if patch_size.0 == 0 || patch_size.1 == 0 {
            return Err(NeuralError::InvalidArchitecture(
                "patch_size dimensions must be non-zero".to_string(),
            ));
        }
        if image_size.0 % patch_size.0 != 0 || image_size.1 % patch_size.1 != 0 {
            return Err(NeuralError::InvalidArchitecture(format!(
                "image_size {:?} must be divisible by patch_size {:?}",
                image_size, patch_size
            )));
        }
        if in_channels == 0 {
            return Err(NeuralError::InvalidArchitecture(
                "in_channels must be non-zero".to_string(),
            ));
        }
        if embed_dim == 0 {
            return Err(NeuralError::InvalidArchitecture(
                "embed_dim must be non-zero".to_string(),
            ));
        }

        let num_patches_h = image_size.0 / patch_size.0;
        let num_patches_w = image_size.1 / patch_size.1;
        let patch_dim = in_channels * patch_size.0 * patch_size.1;

        // Kaiming / Xavier uniform initialisation: uniform(-bound, bound)
        // bound = sqrt(6 / (fan_in + fan_out)) for Xavier uniform
        let fan_in = patch_dim as f64;
        let fan_out = embed_dim as f64;
        let bound = f64::sqrt(6.0 / (fan_in + fan_out));

        let uniform = Uniform::new(-bound, bound).map_err(|e| {
            NeuralError::InvalidArchitecture(format!(
                "Failed to create uniform distribution: {e}"
            ))
        })?;

        // Weight: [embed_dim, patch_dim]
        let weight_vec: Vec<F> = (0..(embed_dim * patch_dim))
            .map(|_| {
                F::from(uniform.sample(rng))
                    .ok_or_else(|| {
                        NeuralError::InvalidArchitecture(
                            "Failed to convert random value to float type".to_string(),
                        )
                    })
                    .unwrap_or(F::zero())
            })
            .collect();

        let weight =
            Array::from_shape_vec(IxDyn(&[embed_dim, patch_dim]), weight_vec).map_err(|e| {
                NeuralError::InvalidArchitecture(format!(
                    "Failed to construct weight array: {e}"
                ))
            })?;

        // Bias: [embed_dim], initialised to zero
        let bias = Array::zeros(IxDyn(&[embed_dim]));

        let d_weight = RwLock::new(Array::zeros(IxDyn(&[embed_dim, patch_dim])));
        let d_bias = RwLock::new(Array::zeros(IxDyn(&[embed_dim])));

        Ok(Self {
            image_size,
            patch_size,
            in_channels,
            embed_dim,
            num_patches_h,
            num_patches_w,
            patch_dim,
            weight,
            bias,
            use_bias,
            d_weight,
            d_bias,
            cached_patches: RwLock::new(None),
        })
    }

    /// Total number of patches produced from one image
    pub fn num_patches(&self) -> usize {
        self.num_patches_h * self.num_patches_w
    }

    /// Flat patch vector dimension: `in_channels * patch_h * patch_w`
    pub fn patch_dim(&self) -> usize {
        self.patch_dim
    }

    /// Extract and flatten patches from the input image batch.
    ///
    /// Returns an array of shape `[batch, num_patches, patch_dim]`.
    fn extract_patches(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
        let shape = input.shape();
        let batch = shape[0];
        let num_patches = self.num_patches_h * self.num_patches_w;

        let mut patches = Array::zeros(IxDyn(&[batch, num_patches, self.patch_dim]));

        for b in 0..batch {
            for ph in 0..self.num_patches_h {
                for pw in 0..self.num_patches_w {
                    let patch_idx = ph * self.num_patches_w + pw;
                    // Top-left pixel of this patch in the image
                    let h_start = ph * self.patch_size.0;
                    let w_start = pw * self.patch_size.1;
                    let mut flat_idx = 0usize;
                    for c in 0..self.in_channels {
                        for dy in 0..self.patch_size.0 {
                            for dx in 0..self.patch_size.1 {
                                patches[[b, patch_idx, flat_idx]] =
                                    input[[b, c, h_start + dy, w_start + dx]];
                                flat_idx += 1;
                            }
                        }
                    }
                }
            }
        }

        Ok(patches)
    }

    /// Apply the linear projection `patches @ weight.T + bias` over all patches.
    ///
    /// Input shape: `[batch, num_patches, patch_dim]`
    /// Output shape: `[batch, num_patches, embed_dim]`
    fn linear_project(&self, patches: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
        let batch = patches.shape()[0];
        let num_patches = patches.shape()[1];
        let mut output = Array::zeros(IxDyn(&[batch, num_patches, self.embed_dim]));

        for b in 0..batch {
            for p in 0..num_patches {
                for e in 0..self.embed_dim {
                    let mut acc = F::zero();
                    for k in 0..self.patch_dim {
                        acc += patches[[b, p, k]] * self.weight[[e, k]];
                    }
                    if self.use_bias {
                        acc += self.bias[e];
                    }
                    output[[b, p, e]] = acc;
                }
            }
        }

        Ok(output)
    }

    /// Validate input tensor shape for this layer.
    fn validate_input_shape(&self, input: &Array<F, IxDyn>) -> Result<()> {
        let shape = input.shape();
        if shape.len() != 4 {
            return Err(NeuralError::InferenceError(format!(
                "PatchEmbedding expects 4-D input [batch, channels, height, width], got {:?}",
                shape
            )));
        }
        if shape[1] != self.in_channels {
            return Err(NeuralError::InferenceError(format!(
                "PatchEmbedding: expected {} input channels, got {}",
                self.in_channels, shape[1]
            )));
        }
        if shape[2] != self.image_size.0 || shape[3] != self.image_size.1 {
            return Err(NeuralError::InferenceError(format!(
                "PatchEmbedding: expected image size {:?}, got ({}, {})",
                self.image_size, shape[2], shape[3]
            )));
        }
        Ok(())
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> Layer<F>
    for PatchEmbedding<F>
{
    fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>> {
        self.validate_input_shape(input)?;

        // Extract patches: [batch, num_patches, patch_dim]
        let patches = self.extract_patches(input)?;

        // Cache flattened patches for backward pass
        {
            let mut cache = self
                .cached_patches
                .write()
                .expect("RwLock poisoned on cached_patches write");
            *cache = Some(patches.clone());
        }

        // Linear projection: [batch, num_patches, embed_dim]
        self.linear_project(&patches)
    }

    fn backward(
        &self,
        _input: &Array<F, IxDyn>,
        grad_output: &Array<F, IxDyn>,
    ) -> Result<Array<F, IxDyn>> {
        // grad_output shape: [batch, num_patches, embed_dim]
        let go_shape = grad_output.shape();
        if go_shape.len() != 3 {
            return Err(NeuralError::InferenceError(format!(
                "PatchEmbedding backward: grad_output must be 3-D [batch, num_patches, embed_dim], got {:?}",
                go_shape
            )));
        }
        let batch = go_shape[0];
        let num_patches = go_shape[1];

        let patches = {
            let cache = self
                .cached_patches
                .read()
                .expect("RwLock poisoned on cached_patches read");
            cache.clone().ok_or_else(|| {
                NeuralError::InferenceError(
                    "PatchEmbedding backward called before forward — no cached patches".to_string(),
                )
            })?
        };

        // Gradient w.r.t. weight: d_weight[e, k] = sum_{b,p} grad_output[b,p,e] * patches[b,p,k]
        let mut d_weight = Array::zeros(IxDyn(&[self.embed_dim, self.patch_dim]));
        for b in 0..batch {
            for p in 0..num_patches {
                for e in 0..self.embed_dim {
                    let go = grad_output[[b, p, e]];
                    for k in 0..self.patch_dim {
                        d_weight[[e, k]] += go * patches[[b, p, k]];
                    }
                }
            }
        }

        // Gradient w.r.t. bias: d_bias[e] = sum_{b,p} grad_output[b,p,e]
        let mut d_bias = Array::zeros(IxDyn(&[self.embed_dim]));
        if self.use_bias {
            for b in 0..batch {
                for p in 0..num_patches {
                    for e in 0..self.embed_dim {
                        d_bias[e] += grad_output[[b, p, e]];
                    }
                }
            }
        }

        // Store gradients
        {
            let mut dw = self
                .d_weight
                .write()
                .expect("RwLock poisoned on d_weight write");
            *dw = d_weight;
        }
        {
            let mut db = self
                .d_bias
                .write()
                .expect("RwLock poisoned on d_bias write");
            *db = d_bias;
        }

        // Gradient w.r.t. input patches: d_patches[b,p,k] = sum_e grad_output[b,p,e] * weight[e,k]
        let mut d_patches = Array::zeros(IxDyn(&[batch, num_patches, self.patch_dim]));
        for b in 0..batch {
            for p in 0..num_patches {
                for k in 0..self.patch_dim {
                    let mut acc = F::zero();
                    for e in 0..self.embed_dim {
                        acc += grad_output[[b, p, e]] * self.weight[[e, k]];
                    }
                    d_patches[[b, p, k]] = acc;
                }
            }
        }

        // Scatter gradient back into image-shaped tensor [batch, channels, H, W]
        let mut d_input = Array::zeros(IxDyn(&[
            batch,
            self.in_channels,
            self.image_size.0,
            self.image_size.1,
        ]));
        for b in 0..batch {
            for ph in 0..self.num_patches_h {
                for pw in 0..self.num_patches_w {
                    let patch_idx = ph * self.num_patches_w + pw;
                    let h_start = ph * self.patch_size.0;
                    let w_start = pw * self.patch_size.1;
                    let mut flat_idx = 0usize;
                    for c in 0..self.in_channels {
                        for dy in 0..self.patch_size.0 {
                            for dx in 0..self.patch_size.1 {
                                d_input[[b, c, h_start + dy, w_start + dx]] +=
                                    d_patches[[b, patch_idx, flat_idx]];
                                flat_idx += 1;
                            }
                        }
                    }
                }
            }
        }

        Ok(d_input)
    }

    fn update(&mut self, learning_rate: F) -> Result<()> {
        let d_weight = {
            self.d_weight
                .read()
                .expect("RwLock poisoned on d_weight read")
                .clone()
        };
        let d_bias = {
            self.d_bias
                .read()
                .expect("RwLock poisoned on d_bias read")
                .clone()
        };

        // SGD update for weight
        for e in 0..self.embed_dim {
            for k in 0..self.patch_dim {
                self.weight[[e, k]] -= learning_rate * d_weight[[e, k]];
            }
        }

        // SGD update for bias
        if self.use_bias {
            for e in 0..self.embed_dim {
                self.bias[e] -= learning_rate * d_bias[e];
            }
        }

        Ok(())
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
        self
    }

    fn layer_type(&self) -> &str {
        "PatchEmbedding"
    }

    fn parameter_count(&self) -> usize {
        let weight_params = self.embed_dim * self.patch_dim;
        let bias_params = if self.use_bias { self.embed_dim } else { 0 };
        weight_params + bias_params
    }

    fn layer_description(&self) -> String {
        format!(
            "type:PatchEmbedding, image_size:{:?}, patch_size:{:?}, in_channels:{}, embed_dim:{}, num_patches:{}, params:{}",
            self.image_size,
            self.patch_size,
            self.in_channels,
            self.embed_dim,
            self.num_patches(),
            self.parameter_count()
        )
    }

    fn params(&self) -> Vec<Array<F, IxDyn>> {
        if self.use_bias {
            vec![self.weight.clone(), self.bias.clone()]
        } else {
            vec![self.weight.clone()]
        }
    }

    fn set_params(&mut self, params: &[Array<F, IxDyn>]) -> Result<()> {
        if params.is_empty() {
            return Err(NeuralError::InvalidArchitecture(
                "PatchEmbedding set_params: expected at least 1 parameter (weight)".to_string(),
            ));
        }
        self.weight = params[0].clone();
        if self.use_bias && params.len() >= 2 {
            self.bias = params[1].clone();
        }
        Ok(())
    }

    fn inputshape(&self) -> Option<Vec<usize>> {
        Some(vec![
            self.in_channels,
            self.image_size.0,
            self.image_size.1,
        ])
    }

    fn outputshape(&self) -> Option<Vec<usize>> {
        Some(vec![self.num_patches(), self.embed_dim])
    }
}

impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign + 'static> ParamLayer<F>
    for PatchEmbedding<F>
{
    fn get_parameters(&self) -> Vec<Array<F, IxDyn>> {
        self.params()
    }

    fn get_gradients(&self) -> Vec<Array<F, IxDyn>> {
        let dw = self
            .d_weight
            .read()
            .expect("RwLock poisoned on d_weight read")
            .clone();
        if self.use_bias {
            let db = self
                .d_bias
                .read()
                .expect("RwLock poisoned on d_bias read")
                .clone();
            vec![dw, db]
        } else {
            vec![dw]
        }
    }

    fn set_parameters(&mut self, params: Vec<Array<F, IxDyn>>) -> Result<()> {
        self.set_params(&params)
    }
}

// Safety: PatchEmbedding is safe to send across threads; interior mutability is via RwLock
unsafe impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign> Send
    for PatchEmbedding<F>
{
}
unsafe impl<F: Float + Debug + ScalarOperand + Send + Sync + NumAssign> Sync
    for PatchEmbedding<F>
{
}

#[cfg(test)]
mod tests {
    use super::*;
    use scirs2_core::random::rngs::SmallRng;
    use scirs2_core::random::SeedableRng;

    fn make_embed(
        image_size: (usize, usize),
        patch_size: (usize, usize),
        in_channels: usize,
        embed_dim: usize,
    ) -> PatchEmbedding<f64> {
        let mut rng = SmallRng::from_seed([0u8; 32]);
        PatchEmbedding::new(image_size, patch_size, in_channels, embed_dim, true, &mut rng)
            .expect("Failed to construct PatchEmbedding")
    }

    #[test]
    fn test_patch_embedding_output_shape() {
        // 8×8 image, 2×2 patches → 4×4 = 16 patches
        let layer = make_embed((8, 8), (2, 2), 3, 32);
        assert_eq!(layer.num_patches(), 16);
        assert_eq!(layer.patch_dim(), 3 * 2 * 2);

        let batch = 2usize;
        let input = Array::zeros(IxDyn(&[batch, 3, 8, 8]));
        let output = layer.forward(&input).expect("Forward pass failed");
        assert_eq!(output.shape(), &[batch, 16, 32]);
    }

    #[test]
    fn test_patch_embedding_parameter_count() {
        let layer = make_embed((16, 16), (4, 4), 3, 64);
        // patch_dim = 3*4*4 = 48; weight = 64*48 = 3072; bias = 64
        assert_eq!(layer.parameter_count(), 64 * 48 + 64);
    }

    #[test]
    fn test_patch_embedding_backward_shape() {
        let layer = make_embed((8, 8), (2, 2), 3, 32);
        let batch = 2usize;
        let input = Array::zeros(IxDyn(&[batch, 3, 8, 8]));
        let output = layer.forward(&input).expect("Forward failed");
        let grad_out = Array::ones(output.raw_dim());
        let grad_in = layer
            .backward(&input, &grad_out)
            .expect("Backward pass failed");
        // Gradient must match input shape
        assert_eq!(grad_in.shape(), input.shape());
    }

    #[test]
    fn test_patch_embedding_invalid_size() {
        let mut rng = SmallRng::from_seed([0u8; 32]);
        // 7 is not divisible by 4
        let result = PatchEmbedding::<f64>::new((7, 8), (4, 4), 3, 32, true, &mut rng);
        assert!(result.is_err());
    }

    #[test]
    fn test_patch_embedding_update() {
        let mut layer = make_embed((8, 8), (2, 2), 1, 16);
        let input = Array::zeros(IxDyn(&[1, 1, 8, 8]));
        let output = layer.forward(&input).expect("Forward failed");
        let grad_out = Array::ones(output.raw_dim());
        layer.backward(&input, &grad_out).expect("Backward failed");
        layer.update(0.01f64).expect("Update failed");
    }
}