concision_neural/traits/
train.rs

1/*
2    Appellation: train <module>
3    Contrib: @FL03
4*/
5
6/// This trait defines the training process for the network
7pub trait Train<X, Y> {
8    type Output;
9
10    fn train(&mut self, input: &X, target: &Y) -> crate::NeuralResult<Self::Output>;
11
12    fn train_for(
13        &mut self,
14        input: &X,
15        target: &Y,
16        epochs: usize,
17    ) -> crate::NeuralResult<Self::Output> {
18        let mut output = None;
19
20        for _ in 0..epochs {
21            output = match self.train(input, target) {
22                Ok(o) => Some(o),
23                Err(e) => {
24                    #[cfg(feature = "tracing")]
25                    tracing::error!("Training failed: {e}");
26                    return Err(e);
27                }
28            }
29        }
30        output.ok_or_else(crate::error::NeuralError::training_failed)
31    }
32}