pub struct GradientCentralization { /* private fields */ }Expand description
Gradient Centralization optimizer wrapper.
Wraps any optimizer and applies gradient centralization before the optimizer step. GC normalizes gradients by subtracting their mean, which improves training dynamics.
§Example
use tensorlogic_train::*;
use scirs2_core::ndarray::Array2;
use std::collections::HashMap;
// Create base optimizer
let config = OptimizerConfig { learning_rate: 0.001, ..Default::default() };
let adam = AdamOptimizer::new(config);
// Wrap with gradient centralization
let mut gc_adam = GradientCentralization::new(
Box::new(adam),
GcConfig::default(),
);
// Use as normal optimizer - GC is applied automatically
let mut params = HashMap::new();
params.insert("w1".to_string(), Array2::zeros((10, 5)));
let mut grads = HashMap::new();
grads.insert("w1".to_string(), Array2::ones((10, 5)));
gc_adam.step(&mut params, &grads).unwrap();Implementations§
Source§impl GradientCentralization
impl GradientCentralization
Sourcepub fn new(inner_optimizer: Box<dyn Optimizer>, config: GcConfig) -> Self
pub fn new(inner_optimizer: Box<dyn Optimizer>, config: GcConfig) -> Self
Create a new gradient centralization optimizer.
Sourcepub fn with_default(inner_optimizer: Box<dyn Optimizer>) -> Self
pub fn with_default(inner_optimizer: Box<dyn Optimizer>) -> Self
Create with default configuration.
Sourcepub fn config_mut(&mut self) -> &mut GcConfig
pub fn config_mut(&mut self) -> &mut GcConfig
Get mutable GC configuration.
Sourcepub fn reset_stats(&mut self)
pub fn reset_stats(&mut self)
Reset statistics.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for GradientCentralization
impl !RefUnwindSafe for GradientCentralization
impl !Send for GradientCentralization
impl !Sync for GradientCentralization
impl Unpin for GradientCentralization
impl !UnwindSafe for GradientCentralization
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
Mutably borrows from an owned value. Read more
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>
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 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>
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