concision-core 0.3.1

this crate implements the core modules for the concision framework
Documentation
/*
    appellation: impl_layer <module>
    authors: @FL03
*/
use crate::activate::Activator;
use crate::nn::layer::LayerBase;
use concision_params::RawParams;
use concision_traits::Forward;

impl<F, P, A> LayerBase<F, P>
where
    P: RawParams<Elem = A>,
{
    /// create a new [`LayerBase`] from the given activation function and parameters.
    pub const fn new(rho: F, params: P) -> Self {
        Self { rho, params }
    }
    /// create a new [`LayerBase`] from the given parameters assuming the logical default for
    /// the activation of type `F`.
    pub fn from_params(params: P) -> Self
    where
        F: Default,
    {
        Self::new(<F>::default(), params)
    }
    /// create a new [`LayerBase`] from the given activation function and shape.
    pub fn from_rho<Sh>(rho: F) -> Self
    where
        P: Default,
    {
        Self::new(rho, <P>::default())
    }
    /// returns an immutable reference to the layer's parameters
    pub const fn params(&self) -> &P {
        &self.params
    }
    /// returns a mutable reference to the layer's parameters
    pub const fn params_mut(&mut self) -> &mut P {
        &mut self.params
    }
    /// returns an immutable reference to the activation function of the layer
    pub const fn rho(&self) -> &F {
        &self.rho
    }
    /// returns a mutable reference to the activation function of the layer
    pub const fn rho_mut(&mut self) -> &mut F {
        &mut self.rho
    }
    #[inline]
    /// consumes the current instance and returns another with the given parameters.
    pub fn with_params<Y>(self, params: Y) -> LayerBase<F, Y>
    where
        F: Activator<Y>,
    {
        LayerBase {
            rho: self.rho,
            params,
        }
    }
    #[inline]
    /// consumes the current instance and returns another with the given activation function.
    /// This is useful during the creation of the model, when the activation function is not known yet.
    pub fn with_rho<G>(self, rho: G) -> LayerBase<G, P>
    where
        G: Activator<P>,
    {
        LayerBase {
            rho,
            params: self.params,
        }
    }
    #[inline]
    /// apply the configured activation function onto some input, producing some output
    pub fn activate<X, Y>(&self, input: X) -> Y
    where
        F: Activator<X, Output = Y>,
    {
        self.rho().activate(input)
    }
    /// given some input, complete a single forward pass through the layer
    pub fn forward<U, V>(&self, input: &U) -> V
    where
        Self: Forward<U, Output = V>,
    {
        <Self as Forward<U>>::forward(self, input)
    }
}