nabla_ml/
nab_optimizers.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
use crate::nab_array::NDArray;

pub struct NablaOptimizer;


impl NablaOptimizer {

    /// Performs Stochastic Gradient Descent (SGD) update
    /// 
    /// w = w - learning_rate * gradient
    ///
    /// # Arguments
    ///
    /// * `weights` - NDArray of current weights to update
    /// * `gradient` - NDArray of gradients for the weights
    /// * `learning_rate` - Learning rate for the update
    ///
    /// # Example
    ///
    /// ```
    /// use nabla_ml::nab_array::NDArray;
    /// use nabla_ml::nab_optimizers::NablaOptimizer;
    ///
    /// let mut weights = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
    /// let gradients = NDArray::from_vec(vec![0.1, 0.2, 0.3]);
    /// let learning_rate = 0.1;
    ///
    /// NablaOptimizer::sgd_update(&mut weights, &gradients, learning_rate);
    /// ```
    pub fn sgd_update(weights: &mut NDArray, gradient: &NDArray, learning_rate: f64) {
        let update = gradient.multiply_scalar(learning_rate);
        *weights = weights.subtract(&update);
    }

    /// Performs SGD update with momentum
    /// 
    /// v = momentum * v - learning_rate * gradient
    /// w = w + v
    ///
    /// # Arguments
    ///
    /// * `weights` - NDArray of current weights to update
    /// * `gradient` - NDArray of gradients for the weights
    /// * `velocity` - Mutable reference to momentum velocity
    /// * `learning_rate` - Learning rate for the update
    /// * `momentum` - Momentum coefficient (default: 0.9)
    pub fn sgd_momentum_update(
        weights: &mut NDArray,
        gradient: &NDArray,
        velocity: &mut NDArray,
        learning_rate: f64,
        momentum: f64,
    ) {
        // Update velocity
        *velocity = velocity.multiply_scalar(momentum)
            .subtract(&gradient.multiply_scalar(learning_rate));
        
        // Update weights using velocity
        *weights = weights.clone().add(velocity);
    }

    /// Performs RMSprop update
    /// 
    /// cache = decay_rate * cache + (1 - decay_rate) * gradient^2
    /// w = w - learning_rate * gradient / (sqrt(cache) + epsilon)
    ///
    /// # Arguments
    ///
    /// * `weights` - NDArray of current weights to update
    /// * `gradient` - NDArray of gradients for the weights
    /// * `cache` - Running average of squared gradients
    /// * `learning_rate` - Learning rate for the update
    /// * `decay_rate` - Decay rate for running average (default: 0.9)
    /// * `epsilon` - Small value for numerical stability (default: 1e-8)
    ///
    /// # Example
    ///
    /// ```
    /// use nabla_ml::nab_array::NDArray;
    /// use nabla_ml::nab_optimizers::NablaOptimizer;
    ///
    /// let mut weights = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
    /// let gradients = NDArray::from_vec(vec![0.1, 0.2, 0.3]);
    /// let mut cache = NDArray::zeros(vec![3]);
    /// let learning_rate = 0.01;
    /// let decay_rate = 0.9;
    /// let epsilon = 1e-8;
    ///
    /// NablaOptimizer::rmsprop_update(
    ///     &mut weights, 
    ///     &gradients, 
    ///     &mut cache,
    ///     learning_rate,
    ///     decay_rate,
    ///     epsilon
    /// );
    /// ```
    pub fn rmsprop_update(
        weights: &mut NDArray,
        gradient: &NDArray,
        cache: &mut NDArray,
        learning_rate: f64,
        decay_rate: f64,
        epsilon: f64,
    ) {
        // Update cache
        *cache = cache.multiply_scalar(decay_rate)
            .add(&gradient.multiply(gradient).multiply_scalar(1.0 - decay_rate));
        
        // Compute update
        let update = gradient.divide(
            &cache.sqrt().add_scalar(epsilon)
        ).multiply_scalar(learning_rate);
        
        // Update weights
        *weights = weights.subtract(&update);
    }

    /// Performs Adam (Adaptive Moment Estimation) update
    /// 
    /// m = beta1 * m + (1 - beta1) * gradient           // Update first moment
    /// v = beta2 * v + (1 - beta2) * gradient^2         // Update second moment
    /// m_hat = m / (1 - beta1^t)                        // Bias correction
    /// v_hat = v / (1 - beta2^t)                        // Bias correction
    /// w = w - learning_rate * m_hat / (sqrt(v_hat) + epsilon)
    ///
    /// # Arguments
    ///
    /// * `weights` - NDArray of current weights to update
    /// * `gradient` - NDArray of gradients for the weights
    /// * `m` - First moment vector (momentum)
    /// * `v` - Second moment vector (uncentered variance)
    /// * `t` - Current timestep (starting from 1)
    /// * `learning_rate` - Learning rate for the update
    /// * `beta1` - Exponential decay rate for first moment (default: 0.9)
    /// * `beta2` - Exponential decay rate for second moment (default: 0.999)
    /// * `epsilon` - Small value for numerical stability (default: 1e-8)
    ///
    /// # Example
    ///
    /// ```
    /// use nabla_ml::nab_array::NDArray;
    /// use nabla_ml::nab_optimizers::NablaOptimizer;
    ///
    /// let mut weights = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
    /// let gradients = NDArray::from_vec(vec![0.1, 0.2, 0.3]);
    /// let mut m = NDArray::zeros(vec![3]);
    /// let mut v = NDArray::zeros(vec![3]);
    /// let t = 1;
    /// let learning_rate = 0.001;
    /// let beta1 = 0.9;
    /// let beta2 = 0.999;
    /// let epsilon = 1e-8;
    ///
    /// NablaOptimizer::adam_update(
    ///     &mut weights,
    ///     &gradients,
    ///     &mut m,
    ///     &mut v,
    ///     t,
    ///     learning_rate,
    ///     beta1,
    ///     beta2,
    ///     epsilon
    /// );
    /// ```
    pub fn adam_update(
        weights: &mut NDArray,
        gradient: &NDArray,
        m: &mut NDArray,
        v: &mut NDArray,
        t: usize,
        learning_rate: f64,
        beta1: f64,
        beta2: f64,
        epsilon: f64,
    ) {
        // Update biased first moment estimate
        *m = m.multiply_scalar(beta1)
            .add(&gradient.multiply_scalar(1.0 - beta1));
        
        // Update biased second raw moment estimate
        *v = v.multiply_scalar(beta2)
            .add(&gradient.multiply(gradient).multiply_scalar(1.0 - beta2));
        
        // Compute bias-corrected first moment estimate
        let m_hat = m.multiply_scalar(1.0 / (1.0 - beta1.powi(t as i32)));
        
        // Compute bias-corrected second raw moment estimate
        let v_hat = v.multiply_scalar(1.0 / (1.0 - beta2.powi(t as i32)));
        
        // Compute the update
        let update = m_hat.divide(&v_hat.sqrt().add_scalar(epsilon))
            .multiply_scalar(learning_rate);
        
        // Apply update to weights
        *weights = weights.subtract(&update);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sgd_update() {
        // Initialize test data
        let mut weights = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
        let gradients = NDArray::from_vec(vec![0.1, 0.2, 0.3]);
        let learning_rate = 0.1;

        // Store initial weights
        let initial_weights = weights.clone();

        // Perform update
        NablaOptimizer::sgd_update(&mut weights, &gradients, learning_rate);

        // Verify weights were updated correctly
        for i in 0..weights.data().len() {
            let expected = initial_weights.data()[i] - learning_rate * gradients.data()[i];
            assert!((weights.data()[i] - expected).abs() < 1e-6);
        }
    }

    #[test]
    fn test_sgd_momentum() {
        // Initialize test data
        let mut weights = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
        let gradients = NDArray::from_vec(vec![0.1, 0.2, 0.3]);
        let mut velocity = NDArray::zeros(vec![3]);
        let learning_rate = 0.1;
        let momentum = 0.9;

        // Store initial weights
        let initial_weights = weights.clone();

        // Perform update
        NablaOptimizer::sgd_momentum_update(
            &mut weights,
            &gradients,
            &mut velocity,
            learning_rate,
            momentum
        );

        // Verify weights changed
        assert!(weights.data() != initial_weights.data());

        // Verify velocity is non-zero
        assert!(velocity.data().iter().any(|&x| x != 0.0));

        // Verify momentum effect (velocity should be -learning_rate * gradients)
        for i in 0..velocity.data().len() {
            assert!((velocity.data()[i] + learning_rate * gradients.data()[i]).abs() < 1e-6);
        }
    }

    #[test]
    fn test_rmsprop_update() {
        // Initialize test data
        let mut weights = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
        let gradients = NDArray::from_vec(vec![0.1, 0.2, 0.3]);
        let mut cache = NDArray::zeros(vec![3]);
        let learning_rate = 0.01;
        let decay_rate = 0.9;
        let epsilon = 1e-8;

        // Store initial values
        let initial_weights = weights.clone();
        let initial_cache = cache.clone();

        // Perform update
        NablaOptimizer::rmsprop_update(
            &mut weights,
            &gradients,
            &mut cache,
            learning_rate,
            decay_rate,
            epsilon
        );

        // Verify weights changed
        assert!(weights.data() != initial_weights.data(),
            "Weights should be updated");

        // Verify cache was updated
        assert!(cache.data() != initial_cache.data(),
            "Cache should be updated");

        // Verify cache contains squared gradient information
        for i in 0..cache.data().len() {
            let expected_cache = (1.0 - decay_rate) * gradients.data()[i].powi(2);
            assert!((cache.data()[i] - expected_cache).abs() < 1e-6,
                "Cache should contain squared gradient information");
        }

        // Test multiple updates to verify cache accumulation
        let prev_cache = cache.clone();
        NablaOptimizer::rmsprop_update(
            &mut weights,
            &gradients,
            &mut cache,
            learning_rate,
            decay_rate,
            epsilon
        );

        // Verify cache decay
        for i in 0..cache.data().len() {
            assert!(cache.data()[i] > prev_cache.data()[i],
                "Cache should accumulate gradient information");
        }
    }

    #[test]
    fn test_adam_update() {
        // Initialize test data
        let mut weights = NDArray::from_vec(vec![1.0, 2.0, 3.0]);
        let gradients = NDArray::from_vec(vec![0.1, 0.2, 0.3]);
        let mut m = NDArray::zeros(vec![3]);
        let mut v = NDArray::zeros(vec![3]);
        let t = 1;
        let learning_rate = 0.001;
        let beta1 = 0.9;
        let beta2 = 0.999;
        let epsilon = 1e-8;

        // Store initial values
        let initial_weights = weights.clone();
        let initial_m = m.clone();
        let initial_v = v.clone();

        // Perform update
        NablaOptimizer::adam_update(
            &mut weights,
            &gradients,
            &mut m,
            &mut v,
            t,
            learning_rate,
            beta1,
            beta2,
            epsilon
        );

        // Verify weights changed
        assert!(weights.data() != initial_weights.data(),
            "Weights should be updated");

        // Verify moment estimates changed
        assert!(m.data() != initial_m.data(),
            "First moment should be updated");
        assert!(v.data() != initial_v.data(),
            "Second moment should be updated");

        // Verify first moment update
        for i in 0..m.data().len() {
            let expected_m = (1.0 - beta1) * gradients.data()[i];
            assert!((m.data()[i] - expected_m).abs() < 1e-6,
                "First moment should be correctly updated");
        }

        // Verify second moment update
        for i in 0..v.data().len() {
            let expected_v = (1.0 - beta2) * gradients.data()[i].powi(2);
            assert!((v.data()[i] - expected_v).abs() < 1e-6,
                "Second moment should be correctly updated");
        }

        // Test multiple updates
        let prev_m = m.clone();
        let prev_v = v.clone();
        
        NablaOptimizer::adam_update(
            &mut weights,
            &gradients,
            &mut m,
            &mut v,
            t + 1,
            learning_rate,
            beta1,
            beta2,
            epsilon
        );

        // Verify moment accumulation
        assert!(m.data().iter().zip(prev_m.data().iter())
            .all(|(&new, &old)| new != old),
            "First moment should accumulate");
        assert!(v.data().iter().zip(prev_v.data().iter())
            .all(|(&new, &old)| new != old),
            "Second moment should accumulate");
    }
}