eryon_surface/
network.rs

1/*
2    Appellation: network <module>
3    Contrib: @FL03
4*/
5
6mod impl_network;
7
8use crate::model::{SurfaceModel, SurfaceModelConfig};
9use crate::points::Point;
10use cnc::nn::ModelFeatures;
11use cnc::params::Params;
12use ndarray::{Array1, Array2};
13use rstmt::nrt::Triad;
14
15/// A neural network capable of dynamic configuration. Essentially, each network is designed to
16/// materialize the surface of a 2-simplex (triad) using barycentric coordinates to define
17/// three critical points. These critical points define the minimum number of hidden layers
18/// within the network and serve as _goalposts_ that guide the learning process. The remaining
19/// points continue this trend, simply mapping each extra hidden layer to another position
20/// within space. The verticies of the simplex are used to inform the input layer and in
21/// finding the _centroid_ of the facet. The centroid defines the output layer of the facet,
22/// serving as the final piece in a pseudo sink-source dyanamic.
23#[derive(Clone, Debug)]
24pub struct SurfaceNetwork<T = f32> {
25    pub(crate) headspace: Triad,
26    pub(crate) model: SurfaceModel<T>,
27    pub(crate) points: Vec<Point<T>>,
28}
29
30impl<T> SurfaceNetwork<T> {
31    /// Create a new surface network for the given triad
32    pub fn new(headspace: Triad) -> Self
33    where
34        T: num_traits::Float + num_traits::FromPrimitive,
35    {
36        let config = SurfaceModelConfig::default();
37        let features = ModelFeatures::new(3, 64, 3, 1);
38        let model = SurfaceModel::zeros(config, features).with_attention();
39        Self {
40            headspace,
41            model,
42            points: Vec::new(),
43        }
44    }
45    /// returns an immutable reference to the network's headspace
46    pub const fn headspace(&self) -> Triad {
47        self.headspace
48    }
49    /// returns a mutable reference to the network's headspace
50    pub fn headspace_mut(&mut self) -> &mut Triad {
51        &mut self.headspace
52    }
53    /// returns an immutable reference to the network's surface
54    pub const fn model(&self) -> &SurfaceModel<T> {
55        &self.model
56    }
57    /// returns a mutable reference to the network's surface
58    pub fn model_mut(&mut self) -> &mut SurfaceModel<T> {
59        &mut self.model
60    }
61    /// returns an immutable reference to the network's critical points
62    pub const fn points(&self) -> &Vec<Point<T>> {
63        &self.points
64    }
65    /// returns a mutable reference to the network's critical points
66    pub fn points_mut(&mut self) -> &mut Vec<Point<T>> {
67        &mut self.points
68    }
69    /// returns an immutable reference to the network's primary weights
70    pub const fn input(&self) -> &Params<T> {
71        self.model().input()
72    }
73    /// returns a mutable reference to the network's primary weights
74    pub fn input_mut(&mut self) -> &mut Params<T> {
75        self.model_mut().input_mut()
76    }
77    /// returns an immutable reference to the model's input layer bias
78    pub const fn input_bias(&self) -> &Array1<T> {
79        self.model().input().bias()
80    }
81    /// returns a mutable reference to the model's input layer bias
82    pub fn input_bias_mut(&mut self) -> &mut Array1<T> {
83        self.model_mut().input_mut().bias_mut()
84    }
85    /// returns an immutable reference to the model's input layer weights
86    pub const fn input_weights(&self) -> &Array2<T> {
87        self.model().input().weights()
88    }
89    /// returns a mutable reference to the model's input layer weights
90    pub fn input_weights_mut(&mut self) -> &mut Array2<T> {
91        self.model_mut().input_mut().weights_mut()
92    }
93    /// returns an immutable reference to the model's hidden layers
94    pub const fn hidden(&self) -> &Vec<Params<T>> {
95        self.model().hidden()
96    }
97    /// returns a mutable reference to the model's hidden layers
98    #[inline]
99    pub fn hidden_mut(&mut self) -> &mut Vec<Params<T>> {
100        self.model_mut().hidden_mut()
101    }
102    /// returns an immutable reference to the network's secondary weights
103    pub const fn output(&self) -> &Params<T> {
104        self.model().output()
105    }
106    /// returns a mutable reference to the network's secondary weights
107    #[inline]
108    pub fn output_mut(&mut self) -> &mut Params<T> {
109        self.model_mut().output_mut()
110    }
111    /// returns an immutable reference to the model's output layer bias
112    pub const fn output_bias(&self) -> &Array1<T> {
113        self.model().output().bias()
114    }
115    /// returns a mutable reference to the model's output layer bias
116    #[inline]
117    pub fn output_bias_mut(&mut self) -> &mut Array1<T> {
118        self.model_mut().output_mut().bias_mut()
119    }
120    /// returns an immutable reference to the model's output layer weights
121    pub const fn output_weights(&self) -> &Array2<T> {
122        self.model().output().weights()
123    }
124    /// returns a mutable reference to the model's output layer weights
125    #[inline]
126    pub fn output_weights_mut(&mut self) -> &mut Array2<T> {
127        self.model_mut().output_mut().weights_mut()
128    }
129
130    pub fn extend_surface<I>(&mut self, iter: I)
131    where
132        I: IntoIterator<Item = Point<T>>,
133    {
134        self.points.extend(iter);
135    }
136    /// set the critical points of the network
137    pub fn set_points<I>(&mut self, iter: I)
138    where
139        I: IntoIterator<Item = Point<T>>,
140    {
141        self.points = Vec::from_iter(iter);
142    }
143    /// enable the attention mechanism in the model
144    pub fn with_attention(self) -> Self
145    where
146        T: num_traits::FromPrimitive,
147    {
148        Self {
149            model: self.model.with_attention(),
150            ..self
151        }
152    }
153    /// consumes the current instance and returns another with the given points
154    pub fn with_points<I>(self, iter: I) -> Self
155    where
156        I: IntoIterator<Item = Point<T>>,
157    {
158        Self {
159            points: Vec::from_iter(iter),
160            ..self
161        }
162    }
163}
164
165impl<T> PartialEq for SurfaceNetwork<T>
166where
167    T: PartialEq,
168    SurfaceModel<T>: PartialEq,
169{
170    fn eq(&self, other: &Self) -> bool {
171        self.headspace == other.headspace
172            && self.model == other.model
173            && self.points == other.points
174    }
175}