use scirs2_core::ndarray::{Array, Dimension, IxDyn, ScalarOperand, Zip};
use scirs2_core::numeric::Float;
use std::fmt::Debug;
use crate::error::{OptimError, Result};
use crate::optimizers::Optimizer;
#[derive(Debug, Clone)]
pub struct Adam<A: Float + ScalarOperand + Debug> {
learning_rate: A,
beta1: A,
beta2: A,
epsilon: A,
weight_decay: A,
m: Option<Vec<Array<A, IxDyn>>>,
v: Option<Vec<Array<A, IxDyn>>>,
t: Vec<usize>,
}
impl<A: Float + ScalarOperand + Debug + Send + Sync> Adam<A> {
pub fn new(learning_rate: A) -> Self {
Self {
learning_rate,
beta1: A::from(0.9)
.expect("Adam: default beta1 (0.9) must be representable in A (f32/f64)"),
beta2: A::from(0.999)
.expect("Adam: default beta2 (0.999) must be representable in A (f32/f64)"),
epsilon: A::from(1e-8)
.expect("Adam: default epsilon (1e-8) must be representable in A (f32/f64)"),
weight_decay: A::zero(),
m: None,
v: None,
t: Vec::new(),
}
}
pub fn new_with_config(
learning_rate: A,
beta1: A,
beta2: A,
epsilon: A,
weight_decay: A,
) -> Self {
Self {
learning_rate,
beta1,
beta2,
epsilon,
weight_decay,
m: None,
v: None,
t: Vec::new(),
}
}
pub fn set_beta1(&mut self, beta1: A) -> &mut Self {
self.beta1 = beta1;
self
}
pub fn with_beta1(mut self, beta1: A) -> Self {
self.beta1 = beta1;
self
}
pub fn get_beta1(&self) -> A {
self.beta1
}
pub fn set_beta2(&mut self, beta2: A) -> &mut Self {
self.beta2 = beta2;
self
}
pub fn with_beta2(mut self, beta2: A) -> Self {
self.beta2 = beta2;
self
}
pub fn get_beta2(&self) -> A {
self.beta2
}
pub fn set_epsilon(&mut self, epsilon: A) -> &mut Self {
self.epsilon = epsilon;
self
}
pub fn with_epsilon(mut self, epsilon: A) -> Self {
self.epsilon = epsilon;
self
}
pub fn get_epsilon(&self) -> A {
self.epsilon
}
pub fn set_weight_decay(&mut self, weight_decay: A) -> &mut Self {
self.weight_decay = weight_decay;
self
}
pub fn with_weight_decay(mut self, weight_decay: A) -> Self {
self.weight_decay = weight_decay;
self
}
pub fn get_weight_decay(&self) -> A {
self.weight_decay
}
pub fn learning_rate(&self) -> A {
self.learning_rate
}
pub fn set_lr(&mut self, lr: A) {
self.learning_rate = lr;
}
pub fn reset(&mut self) {
self.m = None;
self.v = None;
self.t.clear();
}
pub fn timestep(&self, index: usize) -> usize {
self.t.get(index).copied().unwrap_or(0)
}
fn advance_state(&mut self, index: usize, dim: &IxDyn) -> Result<usize> {
let m = self.m.get_or_insert_with(Vec::new);
let v = self.v.get_or_insert_with(Vec::new);
while m.len() <= index {
m.push(Array::zeros(dim.clone()));
}
while v.len() <= index {
v.push(Array::zeros(dim.clone()));
}
while self.t.len() <= index {
self.t.push(0);
}
if m[index].raw_dim() != *dim || v[index].raw_dim() != *dim {
m[index] = Array::zeros(dim.clone());
v[index] = Array::zeros(dim.clone());
self.t[index] = 0;
}
let next = self.t[index].checked_add(1).ok_or_else(|| {
OptimError::InvalidConfig(
"Timestep counter overflow - too many optimization steps".to_string(),
)
})?;
self.t[index] = next;
Ok(next)
}
pub fn step_inplace_indexed<D: Dimension>(
&mut self,
index: usize,
params: &mut Array<A, D>,
gradients: &Array<A, D>,
) -> Result<()> {
if params.shape() != gradients.shape() {
return Err(OptimError::DimensionMismatch(format!(
"Incompatible shapes: parameters have shape {:?}, gradients have shape {:?}",
params.shape(),
gradients.shape()
)));
}
let dim = params.raw_dim().into_dyn();
let t = self.advance_state(index, &dim)?;
let exp = i32::try_from(t).map_err(|_| {
OptimError::InvalidConfig(
"Timestep too large for bias correction calculation".to_string(),
)
})?;
let beta1 = self.beta1;
let beta2 = self.beta2;
let lr = self.learning_rate;
let eps = self.epsilon;
let weight_decay = self.weight_decay;
let one = A::one();
let bias_correction1 = one - beta1.powi(exp);
let bias_correction2 = one - beta2.powi(exp);
let use_weight_decay = weight_decay > A::zero();
let m = self
.m
.as_mut()
.ok_or_else(|| OptimError::InvalidConfig("Adam state not initialized".to_string()))?;
let v = self
.v
.as_mut()
.ok_or_else(|| OptimError::InvalidConfig("Adam state not initialized".to_string()))?;
let mut params_view = params.view_mut().into_dyn();
let gradients_view = gradients.view().into_dyn();
Zip::from(&mut params_view)
.and(&gradients_view)
.and(&mut m[index])
.and(&mut v[index])
.for_each(|p, &g, m_i, v_i| {
let grad = if use_weight_decay {
g + weight_decay * *p
} else {
g
};
*m_i = *m_i * beta1 + grad * (one - beta1);
*v_i = *v_i * beta2 + grad * grad * (one - beta2);
let m_hat = *m_i / bias_correction1;
let v_hat = *v_i / bias_correction2;
*p = *p - lr * m_hat / (v_hat.sqrt() + eps);
});
Ok(())
}
pub fn step_inplace<D: Dimension>(
&mut self,
params: &mut Array<A, D>,
gradients: &Array<A, D>,
) -> Result<()> {
self.step_inplace_indexed(0, params, gradients)
}
pub fn step_indexed<D: Dimension>(
&mut self,
index: usize,
params: &Array<A, D>,
gradients: &Array<A, D>,
) -> Result<Array<A, D>> {
let mut updated = params.to_owned();
self.step_inplace_indexed(index, &mut updated, gradients)?;
Ok(updated)
}
}
impl<A, D> Optimizer<A, D> for Adam<A>
where
A: Float + ScalarOperand + Debug + Send + Sync,
D: Dimension,
{
fn step(&mut self, params: &Array<A, D>, gradients: &Array<A, D>) -> Result<Array<A, D>> {
self.step_indexed(0, params, gradients)
}
fn step_list(
&mut self,
params_list: &[&Array<A, D>],
gradients_list: &[&Array<A, D>],
) -> Result<Vec<Array<A, D>>> {
if params_list.len() != gradients_list.len() {
return Err(OptimError::InvalidConfig(format!(
"Number of parameter arrays ({}) does not match number of gradient arrays ({})",
params_list.len(),
gradients_list.len()
)));
}
let mut results = Vec::with_capacity(params_list.len());
for (index, (params, grads)) in params_list.iter().zip(gradients_list.iter()).enumerate() {
results.push(self.step_indexed(index, params, grads)?);
}
Ok(results)
}
fn get_learning_rate(&self) -> A {
self.learning_rate
}
fn set_learning_rate(&mut self, learning_rate: A) {
self.learning_rate = learning_rate;
}
}