pub struct NtmOptimizer<A: Float + ScalarOperand + Debug> { /* private fields */ }Expand description
Memory-augmented NTM-style optimizer.
NtmOptimizer keeps an N × W memory matrix between calls to
Optimizer::step. On each step it constructs a query key from the
gradient, attends to the memory via content-based (cosine similarity)
addressing, optionally fuses the result with a shifted copy of the
previous attention, reads a vector from memory, writes the new key
back via the NTM erase/add rule, and finally combines the read vector
with the raw gradient to produce the parameter update.
See the module-level documentation for the full algorithm.
§Examples
use scirs2_core::ndarray::Array1;
use optirs_core::optimizers::{NtmOptimizer, Optimizer};
let mut opt: NtmOptimizer<f64> = NtmOptimizer::new(8, 4, 0.01);
let params = Array1::from_vec(vec![1.0, -1.0, 0.5, 0.0]);
let grads = Array1::from_vec(vec![0.2, -0.2, 0.1, 0.0]);
let next = opt.step(¶ms, &grads).expect("ntm step");
assert_eq!(next.len(), 4);Implementations§
Source§impl<A: Float + ScalarOperand + Debug> NtmOptimizer<A>
impl<A: Float + ScalarOperand + Debug> NtmOptimizer<A>
Sourcepub fn new(memory_slots: usize, memory_width: usize, learning_rate: A) -> Self
pub fn new(memory_slots: usize, memory_width: usize, learning_rate: A) -> Self
Constructs an NtmOptimizer with default hyperparameters and the
supplied memory shape and learning rate.
The memory matrix is initialised to all zeros, as are the previous
attention vectors. The RNG seed defaults to 42 (mirroring the
default NtmConfig).
Sourcepub fn with_config(config: NtmConfig<A>) -> Self
pub fn with_config(config: NtmConfig<A>) -> Self
Constructs an NtmOptimizer from a fully populated NtmConfig.
The memory matrix is initialised to zero. A seeded RNG is allocated (and immediately dropped) so a downstream caller that adds a stochastic initialisation strategy in the future does not need to change the public constructor signature.
Sourcepub fn with_read_sharpness(self, beta: A) -> Self
pub fn with_read_sharpness(self, beta: A) -> Self
Sets the read-attention sharpness factor β.
Sourcepub fn with_erase_gate(self, gate: A) -> Self
pub fn with_erase_gate(self, gate: A) -> Self
Sets the erase-gate scalar (intended to live in [0, 1]).
Sourcepub fn with_addressing(self, mode: AddressingMode) -> Self
pub fn with_addressing(self, mode: AddressingMode) -> Self
Selects the addressing mode for the read head.
Sourcepub fn with_memory_weight(self, weight: A) -> Self
pub fn with_memory_weight(self, weight: A) -> Self
Sets the weight applied to the (tiled) read vector in the final update.
Sourcepub fn with_gradient_weight(self, weight: A) -> Self
pub fn with_gradient_weight(self, weight: A) -> Self
Sets the weight applied to the raw gradient in the final update.
Sourcepub fn with_seed(self, seed: u64) -> Self
pub fn with_seed(self, seed: u64) -> Self
Overrides the RNG seed (and rebuilds the internal RNG).
Sourcepub fn memory_mut(&mut self) -> &mut Array2<A>
pub fn memory_mut(&mut self) -> &mut Array2<A>
Mutable access to the memory matrix. Intended primarily for tests that need to seed memory contents directly.
Sourcepub fn last_read_weights(&self) -> &Array1<A>
pub fn last_read_weights(&self) -> &Array1<A>
Returns the read attention vector emitted by the most recent step.
Sourcepub fn last_write_weights(&self) -> &Array1<A>
pub fn last_write_weights(&self) -> &Array1<A>
Returns the write attention vector emitted by the most recent step.
Sourcepub fn step_count(&self) -> usize
pub fn step_count(&self) -> usize
Returns the number of Optimizer::step calls completed so far.
Trait Implementations§
Source§impl<A: Float + ScalarOperand + Debug> Debug for NtmOptimizer<A>
impl<A: Float + ScalarOperand + Debug> Debug for NtmOptimizer<A>
Source§impl<A, D> Optimizer<A, D> for NtmOptimizer<A>
impl<A, D> Optimizer<A, D> for NtmOptimizer<A>
Source§fn step(
&mut self,
params: &Array<A, D>,
gradients: &Array<A, D>,
) -> Result<Array<A, D>>
fn step( &mut self, params: &Array<A, D>, gradients: &Array<A, D>, ) -> Result<Array<A, D>>
Source§fn get_learning_rate(&self) -> A
fn get_learning_rate(&self) -> A
Source§fn set_learning_rate(&mut self, learning_rate: A)
fn set_learning_rate(&mut self, learning_rate: A)
Auto Trait Implementations§
impl<A> Freeze for NtmOptimizer<A>where
A: Freeze,
impl<A> RefUnwindSafe for NtmOptimizer<A>where
A: RefUnwindSafe,
impl<A> Send for NtmOptimizer<A>where
A: Send,
impl<A> Sync for NtmOptimizer<A>where
A: Sync,
impl<A> Unpin for NtmOptimizer<A>where
A: Unpin,
impl<A> UnsafeUnpin for NtmOptimizer<A>where
A: UnsafeUnpin,
impl<A> UnwindSafe for NtmOptimizer<A>where
A: UnwindSafe + 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> 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.