use ruthril::core::statistics::Statistics;
pub trait Trainer {
type Model;
type Data;
type Error;
fn train(&self, model: &mut Self::Model, data: &Self::Data) -> Result<(), Self::Error>;
fn validate_training(&self, losses: &[f64]) -> TrainingMetrics {
TrainingMetrics {
mean_loss: Statistics::mean(losses).unwrap_or(f64::INFINITY),
loss_variance: Statistics::variance(losses).unwrap_or(0.0),
loss_std: Statistics::std_deviation(losses).unwrap_or(0.0),
}
}
fn has_converged(&self, recent_losses: &[f64], threshold: f64) -> bool {
if let Some(std) = Statistics::std_deviation(recent_losses) {
std < threshold
} else {
false
}
}
}
#[derive(Debug, Clone)]
pub struct TrainingMetrics {
pub mean_loss: f64,
pub loss_variance: f64,
pub loss_std: f64,
}