pub struct TrajectoryBatch<T: Float + Debug + Send + Sync + 'static> {
pub observations: Array2<T>,
pub actions: Array2<T>,
pub log_probs: Array1<T>,
pub rewards: Array1<T>,
pub values: Array1<T>,
pub dones: Array1<bool>,
pub advantages: Array1<T>,
pub returns: Array1<T>,
pub final_observation: Option<Array1<T>>,
}Expand description
Trajectory data for RL optimization
Fields§
§observations: Array2<T>Observations
actions: Array2<T>Actions taken
log_probs: Array1<T>Log probabilities of actions
rewards: Array1<T>Rewards received
values: Array1<T>Value function estimates
dones: Array1<bool>Done flags (episode termination)
advantages: Array1<T>Advantage estimates
returns: Array1<T>Target returns
final_observation: Option<Array1<T>>Observation reached after the final transition of the batch (s_T).
GAE and V-trace both need the value of the state that follows the last
stored transition in order to bootstrap. That state is not part of the
batch — observations.row(len - 1) is s_{T-1}, the state the last action
was taken from. Bootstrapping on s_{T-1} is an off-by-one error that
biases every advantage in the batch, so the successor state is carried
explicitly here.
None means “no successor available” (e.g. the batch ends on a terminal
transition, or the caller did not record it); consumers then bootstrap with
zero. When the final transition is terminal the bootstrap is masked out by
dones regardless of this field.
Implementations§
Source§impl<T: Float + Debug + Send + Sync + 'static + FromPrimitive> TrajectoryBatch<T>
impl<T: Float + Debug + Send + Sync + 'static + FromPrimitive> TrajectoryBatch<T>
Sourcepub fn new(
observations: Array2<T>,
actions: Array2<T>,
log_probs: Array1<T>,
rewards: Array1<T>,
values: Array1<T>,
dones: Array1<bool>,
) -> Result<Self>
pub fn new( observations: Array2<T>, actions: Array2<T>, log_probs: Array1<T>, rewards: Array1<T>, values: Array1<T>, dones: Array1<bool>, ) -> Result<Self>
Create a new trajectory batch
Sourcepub fn with_final_observation(
self,
final_observation: Array1<T>,
) -> Result<Self>
pub fn with_final_observation( self, final_observation: Array1<T>, ) -> Result<Self>
Attach the successor observation s_T used to bootstrap the final step.
See TrajectoryBatch::final_observation. Returns an error if the
dimensionality does not match the batch’s observation dimension.
Sourcepub fn compute_gae(&mut self, gamma: T, lambda: T, nextvalue: T) -> Result<()>
pub fn compute_gae(&mut self, gamma: T, lambda: T, nextvalue: T) -> Result<()>
Compute Generalized Advantage Estimation (GAE) without normalizing.
δ_t = r_t + γ·(1 − done_t)·V(s_{t+1}) − V(s_t)
A_t = δ_t + γ·λ·(1 − done_t)·A_{t+1}
R_t = A_t + V(s_t)done_t is read from self.dones[t] for every t, including the last
one: a batch whose final transition terminates the episode must not
bootstrap. nextvalue is V(s_T), the value of the successor of the final
transition (see TrajectoryBatch::final_observation); it is ignored when
the final transition is terminal.
Sourcepub fn compute_advantages(
&mut self,
gamma: T,
lambda: T,
nextvalue: T,
) -> Result<()>
pub fn compute_advantages( &mut self, gamma: T, lambda: T, nextvalue: T, ) -> Result<()>
Compute GAE advantages/returns and normalize the advantages to zero mean and unit variance (the usual policy-gradient variance reduction).
Normalization is skipped for batches of fewer than two samples (where the sample standard deviation is zero and normalizing would annihilate the signal) and whenever the spread is numerically negligible.
Sourcepub fn compute_discounted_returns(
&mut self,
gamma: T,
nextvalue: T,
) -> Result<()>
pub fn compute_discounted_returns( &mut self, gamma: T, nextvalue: T, ) -> Result<()>
Fill returns with plain discounted Monte-Carlo returns
G_t = r_t + γ·(1 − done_t)·G_{t+1}, bootstrapping the final step with
nextvalue when the final transition is non-terminal.
Used by baseline-free REINFORCE, where the advantage is the return.
advantages is set to G_t − V(s_t) so downstream code that reads
advantages stays meaningful when a value baseline happens to be present.
Sourcepub fn get_mini_batches(&self, mini_batchsize: usize) -> Vec<TrajectoryBatch<T>>
pub fn get_mini_batches(&self, mini_batchsize: usize) -> Vec<TrajectoryBatch<T>>
Get mini-batches for optimization
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for TrajectoryBatch<T>
impl<T> RefUnwindSafe for TrajectoryBatch<T>where
T: RefUnwindSafe,
impl<T> Send for TrajectoryBatch<T>
impl<T> Sync for TrajectoryBatch<T>
impl<T> Unpin for TrajectoryBatch<T>
impl<T> UnsafeUnpin for TrajectoryBatch<T>
impl<T> UnwindSafe for TrajectoryBatch<T>where
T: RefUnwindSafe,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.