pub struct SacTrainer<B: AutodiffBackend> { /* private fields */ }Expand description
Burn-backend Soft Actor-Critic trainer.
Generic only over the autodiff backend B; the network shapes are
fixed (a SacActor plus four
ContinuousQNetwork
critics) so the trainer can own the three concrete Adam optimizers
directly.
Implementations§
Source§impl<B: AutodiffBackend> SacTrainer<B>
impl<B: AutodiffBackend> SacTrainer<B>
Sourcepub fn new(
config: SacConfig,
obs_dim: usize,
action_dim: usize,
device: B::Device,
) -> Result<Self>
pub fn new( config: SacConfig, obs_dim: usize, action_dim: usize, device: B::Device, ) -> Result<Self>
Build a new SAC trainer for an obs_dim-dimensional observation
and action_dim-dimensional continuous action space.
All five networks are seeded off SacConfig::seed (the actor,
each critic, and the targets get distinct derived seeds so the
twin critics are decorrelated), and the targets are hard-copied
from their online counterparts so they start byte-equal.
Sourcepub fn action_dim(&self) -> usize
pub fn action_dim(&self) -> usize
Action dimension.
Sourcepub fn target_entropy(&self) -> f32
pub fn target_entropy(&self) -> f32
Effective target entropy (after the -action_dim heuristic).
Sourcepub fn q1(&self) -> &ContinuousQNetwork<B>
pub fn q1(&self) -> &ContinuousQNetwork<B>
Borrow the first online critic. Panics if called mid-step.
Sourcepub fn q2(&self) -> &ContinuousQNetwork<B>
pub fn q2(&self) -> &ContinuousQNetwork<B>
Borrow the second online critic. Panics if called mid-step.
Sourcepub fn buffer(&self) -> &ContinuousReplayBuffer
pub fn buffer(&self) -> &ContinuousReplayBuffer
Borrow the replay buffer.
Sourcepub fn buffer_mut(&mut self) -> &mut ContinuousReplayBuffer
pub fn buffer_mut(&mut self) -> &mut ContinuousReplayBuffer
Mutably borrow the replay buffer.
Sourcepub fn buffer_len(&self) -> usize
pub fn buffer_len(&self) -> usize
Number of transitions currently in the buffer.
Sourcepub fn total_env_steps(&self) -> usize
pub fn total_env_steps(&self) -> usize
Current env-step counter.
Sourcepub fn total_train_steps(&self) -> usize
pub fn total_train_steps(&self) -> usize
Number of completed gradient updates.
Sourcepub fn total_episodes(&self) -> usize
pub fn total_episodes(&self) -> usize
Number of completed episodes.
Sourcepub fn increment_env_step(&mut self)
pub fn increment_env_step(&mut self)
Caller invokes this once per environment step.
Sourcepub fn increment_episodes(&mut self, n: usize)
pub fn increment_episodes(&mut self, n: usize)
Caller invokes this when an episode terminates / truncates.
Sourcepub fn in_warmup(&self) -> bool
pub fn in_warmup(&self) -> bool
true while the trainer is still in the random-action warmup
window (env steps < learning_starts).
Sourcepub fn select_action(&mut self, obs: &[f32]) -> Vec<f32>
pub fn select_action(&mut self, obs: &[f32]) -> Vec<f32>
Select an action for obs (shape [obs_dim]).
During the warmup window (Self::in_warmup) the action is drawn
uniformly in (-1, 1) per dimension; afterwards it is a stochastic
sample from the actor. The returned action is in the tanh-squashed
(-1, 1) box — the env is responsible for rescaling to its own
action range.
Sourcepub fn eval_action(&self, obs: &[f32]) -> Vec<f32>
pub fn eval_action(&self, obs: &[f32]) -> Vec<f32>
Deterministic evaluation action tanh(mu) for obs (shape
[obs_dim]), bypassing exploration noise.
Sourcepub fn train(&mut self) -> Result<Option<SacStepStats>>
pub fn train(&mut self) -> Result<Option<SacStepStats>>
Run gradient_steps_per_env_step SAC gradient updates if the
buffer is ready.
Returns Ok(None) until the buffer holds min_buffer_size
transitions; afterwards returns the stats from the last of the
performed updates.
Sourcepub fn train_step(&mut self) -> Result<SacStepStats>
pub fn train_step(&mut self) -> Result<SacStepStats>
Run exactly one SAC gradient update (critics, actor, optional alpha, then a Polyak soft update of both targets).
Returns an error if any loss is non-finite. Assumes the buffer
holds at least one transition; callers typically gate this on
ContinuousReplayBuffer::is_ready via Self::train.
Auto Trait Implementations§
impl<B> !Freeze for SacTrainer<B>
impl<B> !RefUnwindSafe for SacTrainer<B>
impl<B> !UnwindSafe for SacTrainer<B>
impl<B> Send for SacTrainer<B>
impl<B> Sync for SacTrainer<B>
impl<B> Unpin for SacTrainer<B>where
<B as BackendTypes>::Device: Unpin,
<B as BackendTypes>::FloatTensorPrimitive: Unpin,
<B as BackendTypes>::QuantizedTensorPrimitive: Unpin,
<<B as AutodiffBackend>::InnerBackend as BackendTypes>::FloatTensorPrimitive: Unpin,
<<B as AutodiffBackend>::InnerBackend as BackendTypes>::QuantizedTensorPrimitive: Unpin,
impl<B> UnsafeUnpin for SacTrainer<B>where
<B as BackendTypes>::Device: UnsafeUnpin,
<B as BackendTypes>::FloatTensorPrimitive: UnsafeUnpin,
<B as BackendTypes>::QuantizedTensorPrimitive: UnsafeUnpin,
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