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, model_params::ModelParams, trainer::Trainer};
9
10pub mod config;
11pub mod model_params;
12pub mod trainer;
13
14pub(crate) mod prelude {
15 pub use super::Model;
16 pub use super::config::*;
17 pub use super::model_params::*;
18 pub use super::trainer::*;
19}
20
21use crate::{ModelFeatures, NetworkConfig, Train};
22use cnc::Dataset;
23
24/// This trait defines the base interface for all models, providing access to the models
25/// configuration, layout, and learned parameters.
26pub trait Model<T = f32> {
27 /// The configuration type for the model
28 type Config: NetworkConfig<T>;
29 /// returns an immutable reference to the models configuration; this is typically used to
30 /// access the models hyperparameters (i.e. learning rate, momentum, etc.) and other
31 /// related control parameters.
32 fn config(&self) -> &Self::Config;
33 /// returns a mutable reference to the models configuration; useful for setting hyperparams
34 fn config_mut(&mut self) -> &mut Self::Config;
35 /// returns a copy of the models features (or layout); this is used to define the structure
36 /// of the model and its consituents.
37 fn features(&self) -> ModelFeatures;
38 /// returns an immutable reference to the model parameters
39 fn params(&self) -> &ModelParams<T>;
40 /// returns a mutable reference to the model's parameters
41 fn params_mut(&mut self) -> &mut ModelParams<T>;
42 /// propagates the input through the model; each layer is applied in sequence meaning that
43 /// the output of each previous layer is the input to the next layer. This pattern
44 /// repeats until the output layer returns the final result.
45 ///
46 /// By default, the trait simply passes each output from one layer to the next, however,
47 /// custom models will likely override this method to inject activation methods and other
48 /// related logic
49 fn predict<U, V>(&self, inputs: &U) -> cnc::Result<V>
50 where
51 Self: cnc::Forward<U, Output = V>,
52 {
53 <Self as cnc::Forward<U>>::forward(self, inputs)
54 }
55 #[doc(hidden)]
56 #[deprecated(since = "0.1.17", note = "use predict instead")]
57 fn forward<U, V>(&self, inputs: &U) -> cnc::Result<V>
58 where
59 Self: cnc::Forward<U, Output = V>,
60 {
61 <Self as cnc::Forward<U>>::forward(self, inputs)
62 }
63 /// a convience method that trains the model using the provided dataset; this method
64 /// requires that the model implements the [`Train`] trait and that the dataset
65 fn train<U, V, W>(&mut self, dataset: &Dataset<U, V>) -> crate::NeuralResult<W>
66 where
67 Self: Train<U, V, Output = W>,
68 {
69 <Self as Train<U, V>>::train(self, dataset.records(), dataset.targets())
70 }
71 /// returns a model trainer prepared to train the model; this is a convenience method
72 /// that creates a new trainer instance and returns it. Trainers are lazily evaluated
73 /// meaning that the training process won't begin until the user calls the `begin` method.
74 fn trainer<U, V>(&mut self, dataset: Dataset<U, V>) -> Trainer<'_, Self, T, Dataset<U, V>>
75 where
76 Self: Sized,
77 T: Default,
78 {
79 Trainer::new(self, dataset)
80 }
81}