Skip to main content

TrajectoryBatch

Struct TrajectoryBatch 

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

Source

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

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn get_mini_batches(&self, mini_batchsize: usize) -> Vec<TrajectoryBatch<T>>

Get mini-batches for optimization

Trait Implementations§

Source§

impl<T: Clone + Float + Debug + Send + Sync + 'static> Clone for TrajectoryBatch<T>

Source§

fn clone(&self) -> TrajectoryBatch<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Float + Debug + Send + Sync + 'static> Debug for TrajectoryBatch<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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> 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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