Skip to main content

NtmOptimizer

Struct NtmOptimizer 

Source
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(&params, &grads).expect("ntm step");
assert_eq!(next.len(), 4);

Implementations§

Source§

impl<A: Float + ScalarOperand + Debug> NtmOptimizer<A>

Source

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).

Source

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.

Source

pub fn with_read_sharpness(self, beta: A) -> Self

Sets the read-attention sharpness factor β.

Source

pub fn with_erase_gate(self, gate: A) -> Self

Sets the erase-gate scalar (intended to live in [0, 1]).

Source

pub fn with_addressing(self, mode: AddressingMode) -> Self

Selects the addressing mode for the read head.

Source

pub fn with_memory_weight(self, weight: A) -> Self

Sets the weight applied to the (tiled) read vector in the final update.

Source

pub fn with_gradient_weight(self, weight: A) -> Self

Sets the weight applied to the raw gradient in the final update.

Source

pub fn with_seed(self, seed: u64) -> Self

Overrides the RNG seed (and rebuilds the internal RNG).

Source

pub fn config(&self) -> &NtmConfig<A>

Returns the current configuration.

Source

pub fn memory(&self) -> &Array2<A>

Read-only access to the memory matrix.

Source

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.

Source

pub fn last_read_weights(&self) -> &Array1<A>

Returns the read attention vector emitted by the most recent step.

Source

pub fn last_write_weights(&self) -> &Array1<A>

Returns the write attention vector emitted by the most recent step.

Source

pub fn step_count(&self) -> usize

Returns the number of Optimizer::step calls completed so far.

Source

pub fn reset(&mut self)

Resets the optimizer to its post-construction state: memory and previous attentions become zero and the step counter is cleared.

Trait Implementations§

Source§

impl<A: Float + ScalarOperand + Debug> Debug for NtmOptimizer<A>

Source§

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

Formats the value using the given formatter. Read more
Source§

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

Updates parameters using the given gradients Read more
Source§

fn get_learning_rate(&self) -> A

Gets the current learning rate
Source§

fn set_learning_rate(&mut self, learning_rate: A)

Sets a new learning rate
Source§

fn step_list( &mut self, params_list: &[&Array<A, D>], gradients_list: &[&Array<A, D>], ) -> Result<Vec<Array<A, D>>>

Updates multiple parameter arrays at once Read more

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>

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