pub struct AdaptiveOptimizer {
pub algorithm: OptimizerAlgorithm,
pub states: HashMap<String, OptimizerState>,
pub global_step: u64,
/* private fields */
}Expand description
Adaptive gradient optimizer supporting Adam, AdaGrad, RMSProp, and AdamW.
§Example
use ipfrs_tensorlogic::adaptive_optimizer::{
AdaptiveOptimizer, OptimizerAlgorithm, ParameterGroup,
};
let mut opt = AdaptiveOptimizer::new(OptimizerAlgorithm::adam_default());
let mut groups = vec![
ParameterGroup::with_grad("w", vec![0.5, -0.3], vec![0.1, -0.2]),
];
let norm = opt.step(&mut groups).expect("example: should succeed in docs");
assert!(norm > 0.0);Fields§
§algorithm: OptimizerAlgorithmThe algorithm (and its hyper-parameters) used for all steps.
states: HashMap<String, OptimizerState>Per-group optimizer states; keyed by ParameterGroup::name.
global_step: u64Global step counter (incremented once per call to step).
Implementations§
Source§impl AdaptiveOptimizer
impl AdaptiveOptimizer
Sourcepub fn new(algorithm: OptimizerAlgorithm) -> Self
pub fn new(algorithm: OptimizerAlgorithm) -> Self
Create a new optimizer wrapping the given algorithm.
Sourcepub fn step(
&mut self,
groups: &mut [ParameterGroup],
) -> Result<f64, OptimizerError>
pub fn step( &mut self, groups: &mut [ParameterGroup], ) -> Result<f64, OptimizerError>
Perform one optimizer step across all groups.
Returns the global L2 gradient norm computed before any update.
§Errors
Returns OptimizerError::DimensionMismatch if params.len() != grad.len()
for any group, or OptimizerError::EmptyGroup if a group has zero parameters.
Sourcepub fn step_group(
&mut self,
group: &mut ParameterGroup,
) -> Result<(), OptimizerError>
pub fn step_group( &mut self, group: &mut ParameterGroup, ) -> Result<(), OptimizerError>
Perform one optimizer step for a single parameter group.
This is the low-level primitive used by step.
§Errors
Returns OptimizerError::DimensionMismatch or OptimizerError::EmptyGroup.
Sourcepub fn zero_grad(groups: &mut [ParameterGroup])
pub fn zero_grad(groups: &mut [ParameterGroup])
Zero-out all gradients in groups.
Sourcepub fn global_grad_norm(groups: &[ParameterGroup]) -> f64
pub fn global_grad_norm(groups: &[ParameterGroup]) -> f64
Compute the global L2 norm of all gradients across groups.
Sourcepub fn clip_grad_norm(groups: &mut [ParameterGroup], max_norm: f64)
pub fn clip_grad_norm(groups: &mut [ParameterGroup], max_norm: f64)
Scale all gradients so the global norm does not exceed max_norm.
If the current global norm is ≤ max_norm or is not finite (e.g. NaN/inf),
gradients are left unchanged.
Sourcepub fn reset_state(&mut self, group_name: &str)
pub fn reset_state(&mut self, group_name: &str)
Clear the optimizer state for a specific group (by name).
Sourcepub fn stats(&self, groups: &[ParameterGroup]) -> OptimizerStats
pub fn stats(&self, groups: &[ParameterGroup]) -> OptimizerStats
Return a statistics snapshot.
Trait Implementations§
Source§impl Clone for AdaptiveOptimizer
impl Clone for AdaptiveOptimizer
Source§fn clone(&self) -> AdaptiveOptimizer
fn clone(&self) -> AdaptiveOptimizer
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for AdaptiveOptimizer
impl RefUnwindSafe for AdaptiveOptimizer
impl Send for AdaptiveOptimizer
impl Sync for AdaptiveOptimizer
impl Unpin for AdaptiveOptimizer
impl UnsafeUnpin for AdaptiveOptimizer
impl UnwindSafe for AdaptiveOptimizer
Blanket Implementations§
impl<T> Allocation for T
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> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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 more