concision_neural/model/mod.rs
1/*
2 Appellation: model <module>
3 Contrib: @FL03
4*/
5//! This module provides the scaffolding for creating models and layers in a neural network.
6
7#[doc(inline)]
8pub use self::{config::StandardModelConfig, store::ModelParams};
9
10pub mod config;
11pub mod store;
12
13pub(crate) mod prelude {
14 pub use super::store::*;
15}
16
17use crate::ModelFeatures;
18use cnc::data::Dataset;
19
20/// This trait defines the base interface for all models, providing access to the models
21/// configuration, layout, and learned parameters.
22pub trait Model<T = f32> {
23 /// returns an immutable reference to the models configuration; this is typically used to
24 /// access the models hyperparameters (i.e. learning rate, momentum, etc.) and other
25 /// related control parameters.
26 fn config(&self) -> &StandardModelConfig<T>;
27 /// returns a mutable reference to the models configuration; useful for setting hyperparams
28 fn config_mut(&mut self) -> &mut StandardModelConfig<T>;
29 /// returns a copy of the models features (or layout); this is used to define the structure
30 /// of the model and its consituents.
31 fn features(&self) -> ModelFeatures;
32 /// returns an immutable reference to the model parameters
33 fn params(&self) -> &ModelParams<T>;
34 /// returns a mutable reference to the model's parameters
35 fn params_mut(&mut self) -> &mut ModelParams<T>;
36 /// propagates the input through the model; each layer is applied in sequence meaning that
37 /// the output of each previous layer is the input to the next layer. This pattern
38 /// repeats until the output layer returns the final result.
39 ///
40 /// By default, the trait simply passes each output from one layer to the next, however,
41 /// custom models will likely override this method to inject activation methods and other
42 /// related logic
43 fn forward<U, V>(&self, inputs: &U) -> cnc::Result<V>
44 where
45 Self: cnc::Forward<U, Output = V>,
46 {
47 <Self as cnc::Forward<U>>::forward(self, inputs)
48 }
49 /// returns a model trainer prepared to train the model; this is a convenience method
50 /// that creates a new trainer instance and returns it. Trainers are lazily evaluated
51 /// meaning that the training process won't begin until the user calls the `begin` method.
52 fn train<U, V>(
53 &mut self,
54 dataset: Dataset<U, V>,
55 ) -> crate::train::trainer::Trainer<'_, Self, T, Dataset<U, V>>
56 where
57 Self: Sized,
58 T: Default,
59 {
60 crate::train::trainer::Trainer::new(self, dataset)
61 }
62}