concision_core/nn/traits/neural_network.rs
1/*
2 Appellation: neural_network <module>
3 Created At: 2025.12.10:16:20:19
4 Contrib: @FL03
5*/
6use concision_params::RawParams;
7use concision_traits::{RawStore, RawStoreMut, Store};
8use ndarray::{Dimension, RawData};
9
10pub trait NetworkParams<S, D, A = <S as RawData>::Elem>
11where
12 D: Dimension,
13 S: RawData<Elem = A>,
14{
15}
16/// The [`NetworkConfig`] trait defines an interface for compatible configurations within the
17/// framework, providing a layout and a key-value store to manage hyperparameters.
18pub trait NetworkConfig<K, V> {
19 /// the type of key-value store used to handle the hyperparameters of the network
20 type Store: RawStore<K, V>;
21
22 /// returns a reference to the key-value store
23 fn store(&self) -> &Self::Store;
24 /// returns a mutable reference to the key-value store
25 fn store_mut(&mut self) -> &mut Self::Store;
26 /// get a reference to a value in the store by key
27 fn get<'a>(&'a self, key: &K) -> Option<&'a V>
28 where
29 Self::Store: 'a,
30 {
31 self.store().get(key)
32 }
33 /// returns a mutable reference to a value in the store by key
34 fn get_mut<'a>(&'a mut self, key: &K) -> Option<&'a mut V>
35 where
36 Self::Store: 'a + RawStoreMut<K, V>,
37 {
38 self.store_mut().get_mut(key)
39 }
40 /// returns the entry associated with the given key
41 fn hyperparam<'a>(&'a mut self, key: K) -> <Self::Store as Store<K, V>>::Entry<'a>
42 where
43 Self::Store: 'a + Store<K, V>,
44 {
45 self.store_mut().entry(key)
46 }
47}
48
49/// The [`NeuralNetwork`] trait is used to define the network itself as well as each of its
50/// constituent parts.
51pub trait NeuralNetwork<A>
52where
53 Self::Params<A>: RawParams<Elem = A>,
54{
55 /// The configuration of the neural network defines its architecture and hyperparameters.
56 type Config: NetworkConfig<String, A>;
57 /// The parameters of the neural network define its weights and biases.
58 type Params<_A>;
59
60 /// returns a reference to the network configuration;
61 fn config(&self) -> &Self::Config;
62 /// returns a reference to the network parameters
63 fn params(&self) -> &Self::Params<A>;
64 /// returns a mutable reference to the network parameters
65 fn params_mut(&mut self) -> &mut Self::Params<A>;
66}
67
68/// A trait defining common constants for neural networks.
69pub trait NetworkConsts {
70 const NAME: &'static str;
71 const VERSION: &'static str;
72}