pub struct A2cTrainer<B, P, O>{ /* private fields */ }Expand description
Burn-backend A2C trainer.
Generic over:
B: AutodiffBackend— the Burn backend (e.g.Autodiff<NdArray<f32>>).P: AutodiffModule<B>— the shared actor-critic policy module.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; we .take() it and put back the updated
copy across the single gradient step.
Implementations§
Source§impl<B, P, O> A2cTrainer<B, P, O>
impl<B, P, O> A2cTrainer<B, P, O>
Sourcepub fn new(
config: A2cConfig,
policy: P,
optimizer: BurnOptimizer<B, P, O>,
) -> Result<Self>
pub fn new( config: A2cConfig, policy: P, optimizer: BurnOptimizer<B, P, O>, ) -> Result<Self>
Build a new Burn A2C trainer.
Validates the config and stages the global gradient-norm clip
(A2cConfig::max_grad_norm) on the optimizer wrapper.
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 (one per train_step).
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>,
advantages: Tensor<B, 1>,
returns: Tensor<B, 1>,
evaluate_fn: F,
) -> Result<A2cStats>
pub fn train_step<F>( &mut self, observations: Tensor<B, 2>, actions: Tensor<B, 1, Int>, advantages: Tensor<B, 1>, returns: Tensor<B, 1>, evaluate_fn: F, ) -> Result<A2cStats>
Train for one A2C update.
Performs exactly one gradient step over the whole rollout:
- Optionally normalize advantages to zero-mean/unit-variance when
A2cConfig::normalize_advantagesis set. - Evaluate the policy to get
(log_probs, entropy, values). policy_loss = -mean(log_prob * advantage)(no ratio, no clip).value_loss = mean((V(s) - returns)^2)(plain MSE, no clip).entropy_loss = -mean(entropy).- `total = policy_loss + value_coef * value_loss
- entropy_coef * entropy_loss`.
- Backprop, build
GradientsParams, step the optimizer once. - Entropy-collapse guard (shared with PPO).
Note the absence of old_log_probs / old_values: A2C is
on-policy with a single update, so there is no behaviour policy to
form an importance ratio against, and no old value baseline to
clip against.
The evaluate_fn closure receives (&policy, obs, actions) and
must return (log_probs, entropy, values) — exactly the shape of
MlpBurnPolicy::evaluate_actions.
Auto Trait Implementations§
impl<B, P, O> Freeze for A2cTrainer<B, P, O>
impl<B, P, O> RefUnwindSafe for A2cTrainer<B, P, O>
impl<B, P, O> Send for A2cTrainer<B, P, O>
impl<B, P, O> Sync for A2cTrainer<B, P, O>
impl<B, P, O> Unpin for A2cTrainer<B, P, O>
impl<B, P, O> UnsafeUnpin for A2cTrainer<B, P, O>where
P: UnsafeUnpin,
O: UnsafeUnpin,
impl<B, P, O> UnwindSafe for A2cTrainer<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