Skip to main content

SacTrainer

Struct SacTrainer 

Source
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>

Source

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.

Source

pub fn config(&self) -> &SacConfig

Borrow the configuration.

Source

pub fn obs_dim(&self) -> usize

Observation dimension.

Source

pub fn action_dim(&self) -> usize

Action dimension.

Source

pub fn target_entropy(&self) -> f32

Effective target entropy (after the -action_dim heuristic).

Source

pub fn actor(&self) -> &SacActor<B>

Borrow the actor. Panics if called mid-step.

Source

pub fn q1(&self) -> &ContinuousQNetwork<B>

Borrow the first online critic. Panics if called mid-step.

Source

pub fn q2(&self) -> &ContinuousQNetwork<B>

Borrow the second online critic. Panics if called mid-step.

Source

pub fn buffer(&self) -> &ContinuousReplayBuffer

Borrow the replay buffer.

Source

pub fn buffer_mut(&mut self) -> &mut ContinuousReplayBuffer

Mutably borrow the replay buffer.

Source

pub fn buffer_len(&self) -> usize

Number of transitions currently in the buffer.

Source

pub fn alpha(&self) -> f64

Current entropy temperature alpha = exp(log_alpha).

Source

pub fn total_env_steps(&self) -> usize

Current env-step counter.

Source

pub fn total_train_steps(&self) -> usize

Number of completed gradient updates.

Source

pub fn total_episodes(&self) -> usize

Number of completed episodes.

Source

pub fn increment_env_step(&mut self)

Caller invokes this once per environment step.

Source

pub fn increment_episodes(&mut self, n: usize)

Caller invokes this when an episode terminates / truncates.

Source

pub fn in_warmup(&self) -> bool

true while the trainer is still in the random-action warmup window (env steps < learning_starts).

Source

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.

Source

pub fn eval_action(&self, obs: &[f32]) -> Vec<f32>

Deterministic evaluation action tanh(mu) for obs (shape [obs_dim]), bypassing exploration noise.

Source

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.

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more