use crate::{ItemLazy, renderer::MetricsRenderer};
use burn_core::{module::AutodiffModule, tensor::Gradients};
use burn_optim::{
GradientsParams, ModuleOptimizer, MultiGradientsParams,
lr_scheduler::module_lr_scheduler::ModuleLearningRate,
};
pub struct TrainOutput<TO> {
pub grads: GradientsParams,
pub item: TO,
}
impl<TO> TrainOutput<TO> {
pub fn new<M: AutodiffModule>(module: &M, grads: Gradients, item: TO) -> Self {
let grads = GradientsParams::from_grads(grads, module);
Self { grads, item }
}
}
pub trait TrainStep {
type Input: Send + 'static;
type Output: ItemLazy + 'static;
fn step(&self, item: Self::Input) -> TrainOutput<Self::Output>;
fn optimize(
self,
optim: &mut ModuleOptimizer,
lr_module: ModuleLearningRate,
grads: GradientsParams,
) -> Self
where
Self: AutodiffModule + Sized,
{
optim.step(lr_module, self, grads)
}
fn optimize_multi(
self,
optim: &mut ModuleOptimizer,
lr_module: ModuleLearningRate,
grads: MultiGradientsParams,
) -> Self
where
Self: AutodiffModule + Sized,
{
optim.step_multi(lr_module, self, grads)
}
}
pub trait InferenceStep {
type Input: Send + 'static;
type Output: ItemLazy + 'static;
fn step(&self, item: Self::Input) -> Self::Output;
}
pub struct LearningResult<M> {
pub model: M,
pub renderer: Box<dyn MetricsRenderer>,
}