pub struct LinearSoftmaxPolicy<T: Float + Debug + Send + Sync + 'static> { /* private fields */ }Expand description
Categorical policy π(a|s) = softmax(W φ(s)) with an analytic score function.
Actions are one-hot rows of width n_actions. The score function is the
textbook softmax result
∇_W log π(a|s) = (e_a − p(s)) φ(s)ᵀ , p(s) = softmax(W φ(s))which is exactly rank one — so this policy is also an exact source of
Kronecker factors for K-FAC (φ is the input factor, e_a − p the output
factor).
Implementations§
Source§impl<T: Float + Debug + Send + Sync + 'static> LinearSoftmaxPolicy<T>
impl<T: Float + Debug + Send + Sync + 'static> LinearSoftmaxPolicy<T>
Sourcepub fn new(n_actions: usize, n_features: usize) -> Result<Self>
pub fn new(n_actions: usize, n_features: usize) -> Result<Self>
Create a zero-initialized policy (uniform over actions).
Sourcepub fn from_weights(weights: Array2<T>) -> Result<Self>
pub fn from_weights(weights: Array2<T>) -> Result<Self>
Create a policy from an explicit weight matrix (n_actions, n_features).
Sourcepub fn n_features(&self) -> usize
pub fn n_features(&self) -> usize
Observation/feature dimension.
Sourcepub fn logits(&self, observations: &Array2<T>) -> Result<Array2<T>>
pub fn logits(&self, observations: &Array2<T>) -> Result<Array2<T>>
Logits W φ(s) for a batch, shape (n, n_actions).
Sourcepub fn probabilities(&self, observations: &Array2<T>) -> Result<Array2<T>>
pub fn probabilities(&self, observations: &Array2<T>) -> Result<Array2<T>>
Action probabilities for a batch, shape (n, n_actions).
Sourcepub fn sample_actions_with(
&self,
observations: &Array2<T>,
uniform: impl FnMut() -> f64,
) -> Result<(Array2<T>, Array1<T>, Vec<usize>)>
pub fn sample_actions_with( &self, observations: &Array2<T>, uniform: impl FnMut() -> f64, ) -> Result<(Array2<T>, Array1<T>, Vec<usize>)>
Sample one action per observation using a caller-supplied uniform source.
Returns the one-hot action matrix together with the log-probabilities of
the sampled actions — exactly the two arrays a
super::TrajectoryBatch needs. Taking the uniform source as a closure
keeps rollouts fully reproducible in tests.
Trait Implementations§
Source§impl<T: Clone + Float + Debug + Send + Sync + 'static> Clone for LinearSoftmaxPolicy<T>
impl<T: Clone + Float + Debug + Send + Sync + 'static> Clone for LinearSoftmaxPolicy<T>
Source§fn clone(&self) -> LinearSoftmaxPolicy<T>
fn clone(&self) -> LinearSoftmaxPolicy<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T: Float + Debug + Send + Sync + 'static> PolicyNetwork<T> for LinearSoftmaxPolicy<T>
impl<T: Float + Debug + Send + Sync + 'static> PolicyNetwork<T> for LinearSoftmaxPolicy<T>
Source§fn evaluate_actions(
&self,
observations: &Array2<T>,
actions: &Array2<T>,
) -> Result<PolicyEvaluation<T>>
fn evaluate_actions( &self, observations: &Array2<T>, actions: &Array2<T>, ) -> Result<PolicyEvaluation<T>>
Source§fn get_action_distribution(
&self,
observations: &Array2<T>,
) -> Result<ActionDistribution<T>>
fn get_action_distribution( &self, observations: &Array2<T>, ) -> Result<ActionDistribution<T>>
Source§fn update_parameters(
&mut self,
deltas: &HashMap<String, Array1<T>>,
) -> Result<()>
fn update_parameters( &mut self, deltas: &HashMap<String, Array1<T>>, ) -> Result<()>
Source§fn log_prob_gradient(
&self,
observations: &Array2<T>,
actions: &Array2<T>,
coefficients: &Array1<T>,
) -> Result<HashMap<String, Array1<T>>>
fn log_prob_gradient( &self, observations: &Array2<T>, actions: &Array2<T>, coefficients: &Array1<T>, ) -> Result<HashMap<String, Array1<T>>>
∂/∂θ Σᵢ cᵢ · log π(aᵢ | sᵢ). Read moreSource§fn entropy_gradient(
&self,
observations: &Array2<T>,
) -> Result<HashMap<String, Array1<T>>>
fn entropy_gradient( &self, observations: &Array2<T>, ) -> Result<HashMap<String, Array1<T>>>
∂/∂θ (1/N) Σᵢ H[π(·|sᵢ)]. Read moreSource§fn score_matrix(
&self,
observations: &Array2<T>,
actions: &Array2<T>,
) -> Result<Array2<T>>
fn score_matrix( &self, observations: &Array2<T>, actions: &Array2<T>, ) -> Result<Array2<T>>
g_i = ∇_θ log π(aᵢ|sᵢ), one per row, flattened
with flatten_named’s canonical layout. Read moreSource§fn kronecker_factors(
&self,
observations: &Array2<T>,
actions: &Array2<T>,
) -> Result<Vec<KroneckerBlock<T>>>
fn kronecker_factors( &self, observations: &Array2<T>, actions: &Array2<T>, ) -> Result<Vec<KroneckerBlock<T>>>
Auto Trait Implementations§
impl<T> Freeze for LinearSoftmaxPolicy<T>
impl<T> RefUnwindSafe for LinearSoftmaxPolicy<T>where
T: RefUnwindSafe,
impl<T> Send for LinearSoftmaxPolicy<T>
impl<T> Sync for LinearSoftmaxPolicy<T>
impl<T> Unpin for LinearSoftmaxPolicy<T>
impl<T> UnsafeUnpin for LinearSoftmaxPolicy<T>
impl<T> UnwindSafe for LinearSoftmaxPolicy<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.