pub struct BcTrainer<B, P, O>{ /* private fields */ }Expand description
Burn-backend Behavioral Cloning trainer.
Generic over:
B: AutodiffBackend— the Burn backend (e.g.Autodiff<NdArray<f32>>).P: AutodiffModule<B>— the policy module (only its logits head is trained; the value head is ignored).O: Optimizer<P, B>— the Burn optimizer (typicallyAdamConfig::new().init()).
The policy is held in Option<P> because Burn’s Optimizer::step
consumes the module by value; each minibatch .take()s it and puts back
the updated copy.
Implementations§
Source§impl<B, P, O> BcTrainer<B, P, O>
impl<B, P, O> BcTrainer<B, P, O>
Sourcepub fn new(
config: BcConfig,
policy: P,
optimizer: BurnOptimizer<B, P, O>,
) -> Result<Self>
pub fn new( config: BcConfig, policy: P, optimizer: BurnOptimizer<B, P, O>, ) -> Result<Self>
Build a new Burn behavioral-cloning trainer.
Validates the config and seeds the trainer’s minibatch-shuffle RNG
from BcConfig::seed.
Sourcepub fn policy(&self) -> &P
pub fn policy(&self) -> &P
Borrow the policy. Panics if the trainer is mid-step (the policy has
been moved into the optimizer); only safe to call between
train_epoch invocations.
Sourcepub fn total_steps(&self) -> usize
pub fn total_steps(&self) -> usize
Total completed gradient updates (one per minibatch across all epochs).
Sourcepub fn total_epochs(&self) -> usize
pub fn total_epochs(&self) -> usize
Total completed epochs.
Sourcepub fn train_epoch<F>(
&mut self,
demos: &Demonstrations,
forward_fn: F,
) -> Result<BcEpochStats>
pub fn train_epoch<F>( &mut self, demos: &Demonstrations, forward_fn: F, ) -> Result<BcEpochStats>
Train for one full supervised pass over the demonstration dataset.
- Draw a seeded shuffle of all example indices, partitioned into
minibatches of
BcConfig::batch_size(ceil(len / batch_size)minibatches). - For each minibatch: gather
(obs, actions), produce logits viaforward_fn, compute the cross-entropycompute_bc_loss, backprop, and step the optimizer once. - Aggregate the example-weighted mean loss and action-match accuracy over the epoch.
forward_fn lets the caller pick how logits are produced from the
policy — e.g. |p, o| p.forward(o).0 to drop
MlpBurnPolicy’s value head.
Returns finite loss and accuracy in [0, 1]. Returns an Err if the
dataset is empty.
Auto Trait Implementations§
impl<B, P, O> Freeze for BcTrainer<B, P, O>
impl<B, P, O> RefUnwindSafe for BcTrainer<B, P, O>
impl<B, P, O> Send for BcTrainer<B, P, O>
impl<B, P, O> Sync for BcTrainer<B, P, O>
impl<B, P, O> Unpin for BcTrainer<B, P, O>
impl<B, P, O> UnsafeUnpin for BcTrainer<B, P, O>where
P: UnsafeUnpin,
O: UnsafeUnpin,
impl<B, P, O> UnwindSafe for BcTrainer<B, P, O>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more