use crate::graph::network::Network;
use crate::trainer::stream::BatchStream;
use crate::utils::tabular_data::Dataset;
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct StepReport {
pub train_loss: f32,
pub eval_loss: Option<f32>,
pub fitness: f32,
pub informative: Vec<f32>,
}
pub struct StepContext<'a> {
pub data: Option<&'a RunData<'a>>,
pub fitness: Option<&'a crate::engine::fitness::Fitness>,
pub metrics: &'a [crate::engine::fitness::Metric],
pub env: StepEnv,
pub net_hash: &'a str,
pub net_seed: u64,
}
#[derive(Clone, Copy, Debug)]
pub struct StepEnv {
pub step: usize,
pub run_seed: u64,
pub pop_size: usize,
pub live_count: usize,
pub checkpoint_every: usize,
pub smoothing_window: usize,
}
pub struct RunData<'a> {
pub dataset: &'a Dataset,
pub stream: &'a BatchStream,
}
impl<'a> RunData<'a> {
pub fn train_batch(&self, step: u64) -> flodl::tensor::Result<(flodl::Tensor, flodl::Tensor)> {
self.stream.train_batch(self.dataset, step)
}
pub fn eval_batch(&self, step: u64) -> flodl::tensor::Result<(flodl::Tensor, flodl::Tensor)> {
self.stream.eval_batch(self.dataset, step)
}
}
#[derive(Clone, Copy, Debug)]
pub struct StreamShape {
pub batch_size: usize,
pub eval_batch_size: usize,
}
impl StreamShape {
pub fn none() -> Self {
Self {
batch_size: 16,
eval_batch_size: 16,
}
}
pub fn uniform(n: usize) -> Self {
Self {
batch_size: n,
eval_batch_size: n,
}
}
}
pub trait Trainer: Send {
fn loss(&self) -> Option<crate::trainer::LossFn<'_>> {
None
}
fn scheduled_lr(&self, _step: usize) -> Option<f64> {
None
}
fn stream_shape(&self) -> Option<StreamShape> {
None
}
fn describe(&self) -> Option<serde_json::Value> {
None
}
fn make_optimizer(&self, net: &Network) -> Box<dyn flodl::nn::optim::Optimizer>;
fn train_step(
&mut self,
net: &mut Network,
optimizer: &mut dyn flodl::nn::optim::Optimizer,
step: usize,
ctx: &StepContext<'_>,
) -> flodl::tensor::Result<StepReport>;
}
impl Trainer for Box<dyn Trainer> {
fn loss(&self) -> Option<crate::trainer::LossFn<'_>> {
self.as_ref().loss()
}
fn scheduled_lr(&self, step: usize) -> Option<f64> {
self.as_ref().scheduled_lr(step)
}
fn stream_shape(&self) -> Option<StreamShape> {
self.as_ref().stream_shape()
}
fn describe(&self) -> Option<serde_json::Value> {
self.as_ref().describe()
}
fn make_optimizer(&self, net: &Network) -> Box<dyn flodl::nn::optim::Optimizer> {
self.as_ref().make_optimizer(net)
}
fn train_step(
&mut self,
net: &mut Network,
optimizer: &mut dyn flodl::nn::optim::Optimizer,
step: usize,
ctx: &StepContext<'_>,
) -> flodl::tensor::Result<StepReport> {
self.as_mut().train_step(net, optimizer, step, ctx)
}
}
pub trait IntoBoxedTrainer {
fn into_boxed(self) -> Box<dyn Trainer>;
}
impl<T: Trainer + 'static> IntoBoxedTrainer for T {
fn into_boxed(self) -> Box<dyn Trainer> {
Box::new(self)
}
}