rustyml 0.17.0

Machine learning models, neural networks, metrics, and data utilities 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
//! 1D convolutional layer for sequential data such as time series, audio, or text
//!
//! Holds the layer weights and the activation, and delegates the forward/backward
//! numerics to the dimension-generic convolution engine

use crate::error::Error;
use crate::neural_network::layers::ParamCounts;
use crate::neural_network::layers::activation::{Activation, ActivationCache};
#[doc(inline)]
pub use crate::neural_network::layers::convolution::convolution_engine::ConvPadding;
use crate::neural_network::layers::convolution::convolution_engine::{
    conv_backward, conv_forward, effective_kernel,
};
use crate::neural_network::layers::convolution::validation::{
    valid_output_size, validate_dilation, validate_filters, validate_input_shape_1d,
    validate_kernel_size_1d, validate_stride_dilation_exclusive, validate_strides_1d,
};
use crate::neural_network::layers::validation::{
    start_build, validate_built_input, validate_optional_weight, validate_weight_shape,
};
use crate::neural_network::layers::{built_layer_shape_functions, named_weight_layer_functions};
use crate::neural_network::traits::{LayerBase, ParamRef, UnaryLayer};
use crate::neural_network::{Ctx, Fans, Initializer, Shape, Tensor};
use ndarray::{Array1, Array3};

/// A 1D convolutional layer for neural networks
///
/// Applies a convolution operation to sequential data such as time series, audio signals,
/// or text. Input shape is `[batch_size, length, channels]`. Output shape is `[batch_size,
/// output_length, filters]`. The output length depends on input length, kernel size, stride,
/// dilation, and padding.
///
/// The dimension-generic convolution math lives in
/// [`convolution_engine`](crate::neural_network::layers::convolution). This layer holds the
/// weights and the activation, and delegates the forward/backward numerics to it.
///
/// # Examples
///
/// ```rust
/// use rustyml::neural_network::Shape;
/// use rustyml::neural_network::sequential::SequentialBuilder;
/// use rustyml::neural_network::layers::*;
/// use rustyml::neural_network::optimizers::*;
/// use rustyml::neural_network::losses::*;
/// use ndarray::Array3;
///
/// // Create a simple 3D input tensor: [batch_size, length, channels].
/// // Batch size=2, 10 time steps, 1 input channel.
/// let x = Array3::ones((2, 10, 1)).into_dyn();
///
/// // Create target tensor (output length 8, 3 filters).
/// let y = Array3::ones((2, 8, 3)).into_dyn();
///
/// // Build model: add a Conv1D layer with 3 filters and kernel size 3.
/// let mut model = SequentialBuilder::new()
///     .add(Conv1D::new(
///         3,                      // Number of filters
///         3,                      // Kernel size
///         1,                      // Stride
///         Activation::ReLU,       // ReLU activation
///     ).unwrap())
///     .build(&Shape::known(x.shape()))
///     .unwrap();
/// model.compile(RMSprop::new(0.001, 0.9, 1e-8, 0.0).unwrap(), MeanSquaredError::new());
///
/// // Print model structure.
/// model.summary();
///
/// // Train the model (run a few epochs).
/// model.fit(&x, &y, 3).unwrap();
///
/// // Use predict for forward propagation prediction.
/// let prediction = model.predict(&x).unwrap();
/// println!("Convolution layer prediction results: {:?}", prediction);
///
/// // Check if output shape is correct (should be [2, 8, 3]).
/// assert_eq!(prediction.shape(), &[2, 8, 3]);
/// ```
#[derive(Debug)]
pub struct Conv1D {
    /// Number of convolution filters (output channels)
    filters: usize,
    /// Size of the convolution kernel
    kernel_size: usize,
    /// Stride value for the convolution operation
    stride: usize,
    /// Tap spacing of the kernel. 1 gives a solid kernel
    dilation_rate: usize,
    /// Type of padding to apply (`Valid`, `Same`, or `Causal`)
    padding: ConvPadding,
    /// 3D array of filter weights with shape \[kernel_size, channels, filters\]
    weights: Array3<f32>,
    /// 1D array of bias values with shape \[filters\]
    ///
    /// The array stays allocated when `use_bias` is false. The forward pass and weight rosters
    /// exclude it.
    bias: Array1<f32>,
    /// Activation applied to the convolution output
    activation: Activation,
    /// Shape the layer was built for, batch axis first. `None` before the build
    built: Option<Shape>,
    /// Input channels, which
    /// [`UnaryLayer::build`](crate::neural_network::traits::UnaryLayer::build) reads from the input
    /// shape
    channels: usize,
    /// Seed of the weight draw, or `None` to take the global seed or entropy
    random_state: Option<u64>,
    /// Whether the layer adds a bias to the convolution output
    use_bias: bool,
}

impl Conv1D {
    /// Creates a new Conv1D layer with the specified parameters
    ///
    /// The constructor draws nothing. [`UnaryLayer::build`] reads the channel count from the
    /// input shape, draws the kernel with Xavier (Glorot) uniform initialization, and sets
    /// the bias to 0
    ///
    /// # Parameters
    ///
    /// - `filters` - Number of convolution filters (output channels)
    /// - `kernel_size` - Size of the convolution kernel
    /// - `stride` - Stride value for the convolution operation
    /// - `activation` - Activation applied to the convolution output
    ///
    /// # Returns
    ///
    /// - `Result<Self, Error>` - A new `Conv1D` layer instance or an error
    ///
    /// # Notes
    ///
    /// Padding defaults to [`ConvPadding::Valid`]. Choose [`ConvPadding::Same`] or
    /// [`ConvPadding::Causal`] with [`Conv1D::with_padding`]. The kernel is solid by default.
    /// Space its taps out with [`Conv1D::with_dilation_rate`]. By default, the layer seeds
    /// weights from the global seed or entropy. For reproducible initialization, set a seed with
    /// [`Conv1D::with_random_state`].
    ///
    /// The kernel is not bounded by the input length here. Only [`ConvPadding::Valid`] needs the
    /// effective kernel to fit. The padding mode is not final until the build, so the build
    /// applies that rule.
    ///
    /// # Errors
    ///
    /// - `Error::InvalidParameter` - If `filters`, `kernel_size`, or `stride` is 0
    /// - `Error::InvalidParameter` - If the activation carries an unusable parameter (see
    ///   [`Activation::validate`])
    pub fn new(
        filters: usize,
        kernel_size: usize,
        stride: usize,
        activation: impl Into<Activation>,
    ) -> Result<Self, Error> {
        validate_filters(filters)?;
        validate_kernel_size_1d(kernel_size)?;
        validate_strides_1d(stride)?;
        let activation = activation.into();
        activation.validate()?;

        Ok(Self {
            filters,
            kernel_size,
            stride,
            dilation_rate: 1,
            padding: ConvPadding::Valid,
            weights: Array3::zeros((0, 0, 0)),
            bias: Array1::zeros(0),
            activation,
            built: None,
            channels: 0,
            random_state: None,
            use_bias: true,
        })
    }

    /// Sets the padding mode (defaults to [`ConvPadding::Valid`])
    ///
    /// This takes a [`PaddingType`](super::PaddingType) as well, which converts to the matching
    /// [`ConvPadding`]. [`ConvPadding::Causal`] is the 1 mode no other convolution accepts. It
    /// puts every pad cell on the leading edge, so an output position reads no later input
    /// position
    ///
    /// # Parameters
    ///
    /// - `padding` - Type of padding to apply (`Valid`, `Same`, or `Causal`)
    ///
    /// # Returns
    ///
    /// - `Self` - The updated layer
    pub fn with_padding(mut self, padding: impl Into<ConvPadding>) -> Self {
        self.padding = padding.into();
        self
    }

    /// Sets the tap spacing of the kernel (defaults to 1)
    ///
    /// A dilation of `d` spaces the kernel taps `d` cells apart, so `kernel_size` taps span
    /// `(kernel_size - 1) * d + 1` input cells. The window still advances by the stride. A
    /// dilation of 1 gives a solid kernel
    ///
    /// # Parameters
    ///
    /// - `dilation_rate` - Tap spacing along the length axis
    ///
    /// # Returns
    ///
    /// - `Result<Self, Error>` - The updated layer, or an error
    ///
    /// # Notes
    ///
    /// The effective kernel is not bounded by the input length here. Only [`ConvPadding::Valid`]
    /// needs it to fit, and the build applies that rule
    ///
    /// # Errors
    ///
    /// - `Error::InvalidParameter` - If `dilation_rate` is 0
    /// - `Error::InvalidParameter` - If `dilation_rate` is above 1 and the stride is also above 1
    pub fn with_dilation_rate(mut self, dilation_rate: usize) -> Result<Self, Error> {
        validate_dilation(&[dilation_rate])?;
        validate_stride_dilation_exclusive(&[self.stride], &[dilation_rate])?;
        self.dilation_rate = dilation_rate;
        Ok(self)
    }

    /// Sets the seed used to initialize the filter weights and re-initializes them
    /// deterministically
    ///
    /// By default, the draw takes the global seed or entropy (see [`crate::random`]). An
    /// unbuilt layer holds no kernel, so this records the seed and draws nothing. A layer that
    /// is already built draws its kernel again from the new seed, so the order of the 2 calls
    /// does not matter. A built layer also resets its bias to 0
    ///
    /// # Parameters
    ///
    /// - `random_state` - Seed for weight initialization
    ///
    /// # Returns
    ///
    /// - `Self` - The updated layer
    pub fn with_random_state(mut self, random_state: u64) -> Self {
        self.random_state = Some(random_state);
        if self.built.is_some() {
            self.draw_parameters();
        }
        self
    }

    /// Draws the kernel and zeroes the bias, at the extents the build settled
    ///
    /// The layer supplies its channel count and filter count. The fan pair does not depend on
    /// the order of the stored channel axes.
    fn draw_parameters(&mut self) {
        let mut rng = crate::random::make_rng(self.random_state);
        self.weights = Initializer::GlorotUniform.draw(
            (self.kernel_size, self.channels, self.filters),
            Fans::conv(self.channels, self.filters, self.kernel_size),
            &mut rng,
        );
        self.bias = Array1::zeros(self.filters);
    }

    /// Calculates the output length after convolution
    ///
    /// The `Valid` rule reads the dilated extent of the kernel, and it refuses an extent longer
    /// than the input length. `Causal` gives the same length as `Same`, because both rules round
    /// the input length up by the stride, and both accept every kernel
    ///
    /// # Errors
    ///
    /// - `Error::InvalidInput` - If the padding is `Valid` and the effective kernel is longer than
    ///   the input length
    fn calculate_output_length(&self, input_length: usize) -> Result<usize, Error> {
        match self.padding {
            ConvPadding::Valid => valid_output_size(
                "Conv1D",
                "length",
                input_length,
                effective_kernel(self.kernel_size, self.dilation_rate),
                self.stride,
            ),
            ConvPadding::Same | ConvPadding::Causal => Ok(input_length.div_ceil(self.stride)),
        }
    }

    /// Sets whether the layer adds a bias to the convolution output (defaults to `true`)
    ///
    /// With `use_bias` set to false, the layer holds the kernel alone. `param_count` counts
    /// the kernel alone, `parameters` yields the kernel alone, and a checkpoint holds 1 array
    /// under the path `<position>.kernel`. A checkpoint written by a layer that has a bias
    /// fails to load into a layer that has none, and the refusal names the path
    ///
    /// # Parameters
    ///
    /// - `use_bias` - `true` to add a bias, `false` to leave it out
    ///
    /// # Returns
    ///
    /// - `Self` - The updated layer
    pub fn with_use_bias(mut self, use_bias: bool) -> Self {
        self.use_bias = use_bias;
        self
    }

    /// Sets the weights and bias for this layer
    ///
    /// # Parameters
    ///
    /// - `weights` - 3D array of filter weights with shape \[kernel_size, channels, filters\]
    /// - `bias` - 1D array of bias values with shape \[filters\], or `None` for a layer
    ///   built with [`with_use_bias(false)`](Self::with_use_bias)
    ///
    /// # Returns
    ///
    /// - `Result<(), Error>` - Ok when `weights` and `bias` match the layer's configured shape
    ///
    /// # Errors
    ///
    /// - `Error::NeuralNetwork(NnError::NotBuilt)` - If the layer holds no array yet
    /// - `Error::NeuralNetwork(NnError::WeightShape)` - If `weights` or `bias` does not match the
    ///   layer's expected shape
    /// - `Error::InvalidParameter` - If a bias is given to a layer that holds none, or none is
    ///   given to a layer that holds 1
    pub fn set_weights(
        &mut self,
        weights: Array3<f32>,
        bias: impl Into<Option<Array1<f32>>>,
    ) -> Result<(), Error> {
        if self.built.is_none() {
            return Err(Error::not_built("Conv1D"));
        }
        validate_weight_shape("kernel", self.weights.shape(), weights.shape())?;
        let bias = validate_optional_weight("bias", "use_bias", self.use_bias, bias.into())?;
        if let Some(bias) = bias.as_ref() {
            validate_weight_shape("bias", self.bias.shape(), bias.shape())?;
        }
        self.weights = weights;
        if let Some(bias) = bias {
            self.bias = bias;
        }
        Ok(())
    }
}

/// What the forward pass of [`Conv1D`] parks for its backward pass
struct Conv1DCache {
    /// The input tensor the forward pass received
    input: Tensor,
    /// The tensor that the backward pass of the activation reads
    activation: ActivationCache,
}

impl LayerBase for Conv1D {
    fn layer_type(&self) -> &str {
        "Conv1D"
    }

    fn param_count(&self) -> ParamCounts {
        // Read the arrays the layer holds rather than the configuration, so dropping the
        // bias corrects the count with no second formula to keep in step
        let bias = if self.use_bias { self.bias.len() } else { 0 };
        ParamCounts::trainable(self.weights.len() + bias)
    }

    fn parameters_mut(&mut self) -> Vec<ParamRef<'_>> {
        let Self {
            weights,
            bias,
            use_bias,
            ..
        } = self;
        let mut params = vec![ParamRef::weight(
            "kernel",
            weights.as_slice_mut().expect("weights must be contiguous"),
        )];
        if *use_bias {
            params.push(ParamRef::no_decay(
                "bias",
                bias.as_slice_mut().expect("bias must be contiguous"),
            ));
        }
        params
    }

    built_layer_shape_functions!();

    named_weight_layer_functions!(
        trainable "kernel" => weights,
        trainable "bias" => bias if use_bias,
    );
}

impl UnaryLayer for Conv1D {
    /// Reads the channel count from the input shape, and draws the kernel and the bias
    fn build(&mut self, input: &Shape) -> Result<(), Error> {
        let Some(built) = start_build(&self.built, "Conv1D", input)? else {
            return Ok(());
        };
        built.check_rank("Conv1D", 3)?;
        let (batch, tail) = built.split_batch("Conv1D")?;
        // The family validators read a full extent list, and the batch extent is not part of
        // what they check
        let mut dims = vec![batch.unwrap_or(1)];
        dims.extend(tail);
        validate_input_shape_1d(&dims)?;
        // The shape algebra holds every rule the geometry has, so a stack that cannot run is
        // refused here, before the layer draws a single weight
        self.compute_output_shape(&built)?;
        self.channels = dims[2];
        self.built = Some(built);
        self.draw_parameters();
        Ok(())
    }

    fn forward(&self, input: &Tensor, ctx: &mut Ctx) -> Result<Tensor, Error> {
        validate_built_input(&self.built, "Conv1D", input.shape())?;

        let output = conv_forward(
            input,
            self.weights.as_slice().expect("weights must be contiguous"),
            self.weights.shape(),
            self.use_bias
                .then(|| self.bias.as_slice().expect("bias must be contiguous")),
            &[self.stride],
            &[self.dilation_rate],
            self.padding,
        )?;
        if !ctx.is_training() {
            return self.activation.forward(&output);
        }

        let (activated, activation) = self.activation.forward_train(output)?;
        ctx.push_cache(
            "Conv1D",
            Conv1DCache {
                input: input.clone(),
                activation,
            },
        );

        Ok(activated)
    }

    fn backward(&self, grad_output: &Tensor, ctx: &mut Ctx) -> Result<Tensor, Error> {
        let cache: Conv1DCache = ctx.pop_cache("Conv1D")?;

        let grad_upstream = self.activation.backward(&cache.activation, grad_output)?;

        let grads = conv_backward(
            &grad_upstream,
            &cache.input,
            self.weights.as_slice().expect("weights must be contiguous"),
            self.weights.shape(),
            &[self.stride],
            &[self.dilation_rate],
            self.padding,
        )?;

        ctx.add_grad(
            "kernel",
            Array3::from_shape_vec(self.weights.raw_dim(), grads.weight_grad)
                .expect("weight gradient shape matches weights")
                .into_dyn(),
        )?;
        // A layer without bias has no stored bias gradient or bias optimizer state.
        if self.use_bias {
            ctx.add_grad(
                "bias",
                Array1::from_shape_vec(self.bias.raw_dim(), grads.bias_grad)
                    .expect("bias gradient shape matches bias")
                    .into_dyn(),
            )?;
        }

        Ok(grads.input_grad)
    }

    fn compute_output_shape(&self, input: &Shape) -> Result<Shape, Error> {
        input.check_rank("Conv1D", 3)?;
        let (batch, tail) = input.split_batch("Conv1D")?;
        Ok(Shape::from_batch(
            batch,
            &[self.calculate_output_length(tail[0])?, self.filters],
        ))
    }
}