concision_neural/layout/traits/
layout.rs

1/*
2    appellation: layout <module>
3    authors: @FL03
4*/
5
6/// The [`ModelLayout`] trait defines an interface for object capable of representing the
7/// _layout_; i.e. the number of input, hidden, and output features of a neural network model
8/// containing some number of hidden layers.
9pub trait ModelLayout: Copy + core::fmt::Debug {
10    /// returns a copy of the input features for the model
11    fn input(&self) -> usize;
12    /// returns a mutable reference to the input features for the model
13    fn input_mut(&mut self) -> &mut usize;
14    /// returns a copy of the hidden features for the model
15    fn hidden(&self) -> usize;
16    /// returns a mutable reference to the hidden features for the model
17    fn hidden_mut(&mut self) -> &mut usize;
18    /// returns a copy of the number of hidden layers for the model
19    fn layers(&self) -> usize;
20    /// returns a mutable reference to the number of hidden layers for the model
21    fn layers_mut(&mut self) -> &mut usize;
22    /// returns a copy of the output features for the model
23    fn output(&self) -> usize;
24    /// returns a mutable reference to the output features for the model
25    fn output_mut(&mut self) -> &mut usize;
26    #[inline]
27    /// update the number of input features for the model and return a mutable reference to the
28    /// current layout.
29    fn set_input(&mut self, input: usize) -> &mut Self {
30        *self.input_mut() = input;
31        self
32    }
33    #[inline]
34    /// update the number of hidden features for the model and return a mutable reference to
35    /// the current layout.
36    fn set_hidden(&mut self, hidden: usize) -> &mut Self {
37        *self.hidden_mut() = hidden;
38        self
39    }
40    #[inline]
41    /// update the number of hidden layers for the model and return a mutable reference to
42    /// the current layout.
43    fn set_layers(&mut self, layers: usize) -> &mut Self {
44        *self.layers_mut() = layers;
45        self
46    }
47    #[inline]
48    /// update the number of output features for the model and return a mutable reference to
49    /// the current layout.
50    fn set_output(&mut self, output: usize) -> &mut Self {
51        *self.output_mut() = output;
52        self
53    }
54    /// the dimension of the input layer; (input, hidden)
55    fn dim_input(&self) -> (usize, usize) {
56        (self.input(), self.hidden())
57    }
58    /// the dimension of the hidden layers; (hidden, hidden)
59    fn dim_hidden(&self) -> (usize, usize) {
60        (self.hidden(), self.hidden())
61    }
62    /// the dimension of the output layer; (hidden, output)
63    fn dim_output(&self) -> (usize, usize) {
64        (self.hidden(), self.output())
65    }
66    /// the total number of parameters in the model
67    fn size(&self) -> usize {
68        self.size_input() + self.size_hidden() + self.size_output()
69    }
70    /// the total number of input parameters in the model
71    fn size_input(&self) -> usize {
72        self.input() * self.hidden()
73    }
74    /// the total number of hidden parameters in the model
75    fn size_hidden(&self) -> usize {
76        self.hidden() * self.hidden() * self.layers()
77    }
78    /// the total number of output parameters in the model
79    fn size_output(&self) -> usize {
80        self.hidden() * self.output()
81    }
82}