use crate::graph::network::Network;
use crate::trainer::{StepTrainer, TabularContext, TabularStep};
use crate::utils::race_steps::{deterministic_train_step, eval_one_step};
use flodl::nn::optim::Optimizer;
use flodl::tensor::Result;
use flodl::{Tensor, Variable};
pub struct TabularTrainer {
pub loss_fn: crate::trainer::BoxedLossFn,
pub learning_rate: f32,
pub grad_clip: f32,
pub batch_size: usize,
pub eval_batch_size: usize,
pub lr_schedule: Option<Box<dyn Fn(usize) -> f64 + Send + Sync>>,
pub loss_label: Option<String>,
pub(crate) holdout: Option<HoldoutScorer>,
}
type HoldoutScorer = (
Box<dyn Fn(&mut Network, usize) -> Result<f32> + Send>,
usize,
);
impl TabularTrainer {
pub fn new(
loss_fn: impl Fn(&Variable, &Variable) -> Result<Variable> + Send + Sync + 'static,
) -> Self {
Self {
loss_fn: Box::new(loss_fn),
..Self::default()
}
}
pub fn with_loss_label(mut self, label: impl Into<String>) -> Self {
self.loss_label = Some(label.into());
self
}
pub fn with_learning_rate(mut self, lr: f32) -> Self {
self.learning_rate = lr;
self
}
pub fn with_grad_clip(mut self, clip: f32) -> Self {
self.grad_clip = clip;
self
}
pub fn with_batch_size(mut self, size: usize) -> Self {
self.batch_size = size;
self
}
pub fn with_eval_batch_size(mut self, size: usize) -> Self {
self.eval_batch_size = size;
self
}
pub fn with_lr_schedule(
mut self,
schedule: impl Fn(usize) -> f64 + Send + Sync + 'static,
) -> Self {
self.lr_schedule = Some(Box::new(schedule));
self
}
pub fn with_holdout_scorer(
mut self,
scorer: impl Fn(&mut Network, usize) -> Result<f32> + Send + 'static,
matches: usize,
) -> Self {
self.holdout = Some((Box::new(scorer), matches));
self
}
}
impl Default for TabularTrainer {
fn default() -> Self {
Self {
loss_fn: Box::new(crate::utils::score::cross_entropy_onehot_loss),
learning_rate: crate::engine::config::DEFAULT_LR,
grad_clip: 1.0,
batch_size: 16,
eval_batch_size: 16,
loss_label: None,
lr_schedule: None,
holdout: None,
}
}
}
impl StepTrainer for TabularTrainer {
fn make_optimizer(&self, net: &Network) -> Box<dyn Optimizer> {
use flodl::nn::Module;
Box::new(flodl::nn::Adam::new(
&net.parameters(),
self.learning_rate as f64,
))
}
fn describe(&self) -> Option<serde_json::Value> {
Some(serde_json::json!({
"trainer": "tabular",
"optimizer": "adam",
"loss": self.loss_label,
"learning_rate": self.learning_rate,
"grad_clip": self.grad_clip,
"batch_size": self.batch_size,
"eval_batch_size": self.eval_batch_size,
"lr_schedule": self.lr_schedule.is_some(),
}))
}
fn holdout_score(&mut self, net: &mut Network, game_i: usize) -> Result<f32> {
match &self.holdout {
Some((scorer, _)) => scorer(net, game_i),
None => crate::trainer::StepTrainer::holdout_score(self, net, game_i),
}
}
fn holdout_matches(&self) -> Option<usize> {
self.holdout.as_ref().map(|(_, n)| *n)
}
}
impl TabularStep for TabularTrainer {
fn loss(&self) -> crate::trainer::LossFn<'_> {
&*self.loss_fn
}
fn scheduled_lr(&self, step: usize) -> Option<f64> {
self.lr_schedule.as_ref().map(|f| f(step))
}
fn stream_shape(&self) -> Option<crate::trainer::StreamShape> {
Some(crate::trainer::StreamShape {
batch_size: self.batch_size,
eval_batch_size: self.eval_batch_size,
})
}
fn train_step(
&mut self,
net: &mut Network,
optimizer: &mut dyn Optimizer,
step: usize,
ctx: &TabularContext<'_>,
) -> Result<crate::trainer::TabularStepReport> {
let fitness = ctx.fitness;
let loss_fn: crate::trainer::LossFn<'_> = &*self.loss_fn;
let h: u64 = ctx
.net_hash
.bytes()
.fold(0u64, |a, b| a.wrapping_add(b as u64));
let batch: (Tensor, Tensor) = ctx.data.train_batch(step as u64)?;
if let Some(lr) = self.scheduled_lr(step) {
optimizer.set_lr(lr);
}
let train_loss = deterministic_train_step(
ctx.net_seed,
step as u64,
h,
net,
optimizer,
loss_fn,
&batch,
self.grad_clip,
)?;
let eval_batch: (Tensor, Tensor) = ctx.data.eval_batch(step as u64)?;
let report = eval_one_step(net, loss_fn, fitness, ctx.metrics, &eval_batch)?;
Ok(crate::trainer::TabularStepReport {
train_loss,
eval_loss: report.eval_loss,
fitness: report.fitness,
informative: report.metrics,
challenged_inputs: 0,
})
}
}