concision_data/
trainer.rs

1/*
2    Appellation: trainer <module>
3    Contrib: @FL03
4*/
5
6mod impl_trainer;
7
8use crate::Records;
9use crate::dataset::DatasetBase;
10use concision_core::Model;
11
12pub trait ModelTrainer<T> {
13    type Model: Model<T>;
14    /// returns a model trainer prepared to train the model; this is a convenience method
15    /// that creates a new trainer instance and returns it. Trainers are lazily evaluated
16    /// meaning that the training process won't begin until the user calls the `begin` method.
17    fn trainer<'a, U, V>(
18        &mut self,
19        dataset: DatasetBase<U, V>,
20        model: &'a mut Self::Model,
21    ) -> Trainer<'a, Self::Model, T, DatasetBase<U, V>>
22    where
23        Self: Sized,
24        T: Default,
25        for<'b> &'b mut Self::Model: Model<T>,
26    {
27        Trainer::new(model, dataset)
28    }
29}
30
31/// The [`Trainer`] is a generalized model trainer that works to provide a common interface for
32/// training models over datasets.
33pub struct Trainer<'a, M, T, R>
34where
35    M: Model<T>,
36    R: Records,
37{
38    /// the training dataset
39    pub(crate) dataset: DatasetBase<R::Inputs, R::Targets>,
40    pub(crate) model: &'a mut M,
41    /// the accumulated loss
42    pub(crate) loss: T,
43}
44
45impl<'a, M, T, R> core::ops::Deref for Trainer<'a, M, T, R>
46where
47    M: Model<T>,
48    R: Records,
49{
50    type Target = M;
51
52    fn deref(&self) -> &Self::Target {
53        self.model
54    }
55}
56impl<'a, M, T, R> core::ops::DerefMut for Trainer<'a, M, T, R>
57where
58    M: Model<T>,
59    R: Records,
60{
61    fn deref_mut(&mut self) -> &mut Self::Target {
62        self.model
63    }
64}