concision_neural/layers/traits/
layers.rs

1/*
2    appellation: layers <module>
3    authors: @FL03
4*/
5use super::{Activator, ActivatorGradient};
6
7use cnc::params::ParamsBase;
8use cnc::{Backward, Forward, Tensor};
9use ndarray::{Data, Dimension, RawData};
10/// A layer within a neural-network containing a set of parameters and an activation function.
11/// Here, this manifests as a wrapper around the parameters of the layer with a generic
12/// activation function and corresponding traits to denote desired behaviors.
13///
14pub trait Layer<S, D>
15where
16    D: Dimension,
17    S: RawData<Elem = Self::Scalar>,
18{
19    type Scalar;
20
21    /// returns an immutable reference to the parameters of the layer
22    fn params(&self) -> &ParamsBase<S, D>;
23    /// returns a mutable reference to the parameters of the layer
24    fn params_mut(&mut self) -> &mut ParamsBase<S, D>;
25    /// update the layer parameters
26    fn set_params(&mut self, params: ParamsBase<S, D>) {
27        *self.params_mut() = params;
28    }
29    /// backward propagate error through the layer
30    fn backward<X, Y, Z, Delta>(
31        &mut self,
32        input: X,
33        error: Y,
34        gamma: Self::Scalar,
35    ) -> cnc::Result<Z>
36    where
37        S: Data,
38        Self: ActivatorGradient<X, Input = Y, Delta = Delta>,
39        Self::Scalar: Clone,
40        ParamsBase<S, D>: Backward<X, Delta, Elem = Self::Scalar, Output = Z>,
41    {
42        // compute the delta using the activation function
43        let delta = self.activate_gradient(error);
44        // apply the backward function of the inherited layer
45        self.params_mut().backward(&input, &delta, gamma)
46    }
47    /// complete a forward pass through the layer
48    fn forward<X, Y>(&self, input: &X) -> cnc::Result<Y>
49    where
50        Y: Tensor<S::Elem, D, Repr = S>,
51        ParamsBase<S, D>: Forward<X, Output = Y>,
52        Self: Activator<Y, Output = Y>,
53    {
54        self.params().forward_then(input, |y| self.activate(y))
55    }
56}