concision_neural/train/
mod.rs

1/*
2    Appellation: train <module>
3    Contrib: @FL03
4*/
5//! This module implements various training mechanisms for neural networks. Here, implemented
6//! trainers are lazily evaluated providing greater flexibility and performance.
7#[doc(inline)]
8pub use self::trainer::Trainer;
9
10pub mod trainer;
11
12pub(crate) mod impls {
13    pub mod impl_trainer;
14}
15
16pub(crate) mod prelude {
17    pub use super::trainer::*;
18    pub use super::Train;
19}
20
21
22/// This trait defines the training process for the network
23pub trait Train<X, Y> {
24    type Output;
25
26    fn train(&mut self, input: &X, target: &Y) -> crate::Result<Self::Output>;
27
28    fn train_for(&mut self, input: &X, target: &Y, epochs: usize) -> crate::Result<Self::Output> {
29        let mut output = None;
30
31        for _ in 0..epochs {
32            output = match self.train(input, target) {
33                Ok(o) => Some(o),
34                Err(e) => {
35                    #[cfg(feature = "tracing")]
36                    tracing::error!("Training failed: {e}");
37                    return Err(e);
38                }
39            }
40        }
41        output.ok_or_else(|| crate::error::NeuralError::TrainingFailed("No output".into()))
42    }
43}