pub struct PPOTrainerBurn<B, P, O>{ /* private fields */ }Expand description
Burn-backend PPO trainer.
Generic over:
B: AutodiffBackend— the Burn backend (e.g.Autodiff<NdArray<f32>>,Autodiff<Wgpu>, etc.).P: AutodiffModule<B>— the policy module type.O: Optimizer<P, B>— the Burn optimizer (typically built fromAdamConfig::new().init()).
The policy is held in Option<P> because Burn’s Optimizer::step
consumes the module by value. We use .take() / put-back across
each gradient step.
Implementations§
Source§impl<B, P, O> PPOTrainerBurn<B, P, O>
impl<B, P, O> PPOTrainerBurn<B, P, O>
Sourcepub fn new(
config: PPOConfig,
policy: P,
optimizer: BurnOptimizer<B, P, O>,
) -> Result<Self>
pub fn new( config: PPOConfig, policy: P, optimizer: BurnOptimizer<B, P, O>, ) -> Result<Self>
Build a new Burn PPO trainer.
Validates the config and stages the global gradient-norm cap
(PPOConfig::max_grad_norm) on the optimizer wrapper; train_step
applies it to the gradients of every minibatch step (issue #299).
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_step invocations.
Sourcepub fn total_steps(&self) -> usize
pub fn total_steps(&self) -> usize
Total completed gradient updates.
Sourcepub fn total_episodes(&self) -> usize
pub fn total_episodes(&self) -> usize
Total completed episodes (caller increments).
Sourcepub fn increment_episodes(&mut self, n: usize)
pub fn increment_episodes(&mut self, n: usize)
Increment the episode counter.
Sourcepub fn train_step<F>(
&mut self,
observations: Tensor<B, 2>,
actions: Tensor<B, 1, Int>,
old_log_probs: Tensor<B, 1>,
old_values: Tensor<B, 1>,
advantages: Tensor<B, 1>,
returns: Tensor<B, 1>,
evaluate_fn: F,
) -> Result<TrainingStats>
pub fn train_step<F>( &mut self, observations: Tensor<B, 2>, actions: Tensor<B, 1, Int>, old_log_probs: Tensor<B, 1>, old_values: Tensor<B, 1>, advantages: Tensor<B, 1>, returns: Tensor<B, 1>, evaluate_fn: F, ) -> Result<TrainingStats>
Train for one PPO update.
Implements the same algorithm as
PPOTrainer::train_step_with_policy on the tch path:
- Normalize advantages to zero mean / unit variance.
- For each of
n_epochsepochs, shuffle the buffer and iterate minibatches. - Compute surrogate / value / entropy losses.
total_loss = policy + vf_coef * value + ent_coef * entropy.- Backprop, build
GradientsParams, step the optimizer. - KL early stop if
approx_kl > target_kl. - Entropy-collapse guard.
The evaluate_fn closure receives (&policy, obs, actions) and
must return (log_probs, entropy, values) — exactly the same
shape as the policy network’s evaluate_actions method.
Auto Trait Implementations§
impl<B, P, O> Freeze for PPOTrainerBurn<B, P, O>
impl<B, P, O> RefUnwindSafe for PPOTrainerBurn<B, P, O>
impl<B, P, O> Send for PPOTrainerBurn<B, P, O>
impl<B, P, O> Sync for PPOTrainerBurn<B, P, O>
impl<B, P, O> Unpin for PPOTrainerBurn<B, P, O>
impl<B, P, O> UnsafeUnpin for PPOTrainerBurn<B, P, O>where
P: UnsafeUnpin,
O: UnsafeUnpin,
impl<B, P, O> UnwindSafe for PPOTrainerBurn<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