minidx-core 0.1.10

Core crate for minidx, a simple + compile-time neural-network library.
Documentation
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use crate::misc::Decay;
use crate::{Dtype, Float, Gradients, Unit};
use num_traits::FromPrimitive;
use serde::{Deserialize, Serialize};

/// Describes the training parameters at some instant, serialized during recording.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct TrainInfo {
    /// The learning rate used this update step.
    pub lr: f32,
    /// The L1 regularization applied each update step.
    pub l1_reg: f32,
    /// The L2 regularization applied each update step.
    pub l2_reg: f32,
    /// The maximum magnitude of any update to any parameter.
    pub grad_clip: Option<f32>,
    /// The 0-indexed count of update steps.
    pub step: usize,
}

/// An object responsible for tweaking gradient updates, such as to
/// add the effects of momentum, perform gradient clipping, etc.
pub trait GradAdjuster<G: Gradients> {
    fn adjust(&mut self, gradient_updates: G, loss: f32) -> G;
}

/// An object responsible for applying gradient updates, such as to
/// add the gradient updates to the weights while performing regularization.
pub trait GradApplyer {
    fn apply<G: Gradients>(
        &mut self,
        gradient_updates: G,
        weights: &mut G,
    ) -> Result<(), crate::Error>;

    fn advance_step(&mut self);
}

/// Describes the basic set of parameters used in training. Implements
/// optimizer traits, so it can be passed into training methods.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct TrainParams {
    /// The learning rate to use for update steps. See [Decay] for
    /// how to specify a fixed vs variable learning rate.
    pub lr: Decay,
    /// The L1 regularization to apply each update step.
    pub l1_reg: Option<Decay>,
    /// The L2 regularization to apply each update step.
    pub l2_reg: Option<Decay>,

    /// If set, the number of update steps for which the learning rate
    /// is ramped up at the beginning.
    pub soft_start_epochs: Option<usize>,

    /// If set, the maximum magnitude of any update to any parameter.
    pub grad_clip: Option<f32>,

    step: usize,
}

impl Default for TrainParams {
    fn default() -> Self {
        Self {
            lr: Decay::None(1.0e-6),
            l1_reg: None,
            l2_reg: None,
            soft_start_epochs: None,
            grad_clip: None,
            step: 0,
        }
    }
}

impl Into<TrainInfo> for &TrainParams {
    fn into(self) -> TrainInfo {
        TrainInfo {
            lr: self.current_lr(),
            l1_reg: self
                .l1_reg
                .as_ref()
                .map(|d| d.at_timestep(self.step))
                .unwrap_or(0.0),
            l2_reg: self
                .l2_reg
                .as_ref()
                .map(|d| d.at_timestep(self.step))
                .unwrap_or(0.0),
            grad_clip: self.grad_clip,
            step: self.step,
        }
    }
}

impl TrainParams {
    /// Constructs an object with the given training rate.
    pub fn with_lr(lr: f32) -> Self {
        Self {
            lr: Decay::None(lr),
            ..Default::default()
        }
    }

    /// Sets the l1 regularization weight, keeping all other parameters unaffected.
    ///
    /// This method can be chained in a builder-pattern kind of way.
    pub fn and_l1(mut self, l1: f32) -> Self {
        self.l1_reg = Some(Decay::None(l1));
        self
    }

    /// Sets the l2 regularization weight, keeping all other parameters unaffected.
    ///
    /// This method can be chained in a builder-pattern kind of way.
    pub fn and_l2(mut self, l2: f32) -> Self {
        self.l2_reg = Some(Decay::None(l2));
        self
    }

    /// Sets the learning rate (linear) decay, keeping all other parameters unaffected.
    ///
    /// This method can be chained in a builder-pattern kind of way.
    pub fn and_lr_decay(mut self, lr_decay: f32) -> Self {
        self.lr = Decay::Linear {
            start: self.lr.start_value(),
            decay: lr_decay,
        };
        self
    }

    /// Sets the learning rate (cosine) decay, keeping all other parameters unaffected.
    ///
    /// This method can be chained in a builder-pattern kind of way.
    pub fn and_lr_cosine_decay(mut self, final_lr: f32, over_steps: usize) -> Self {
        self.lr = Decay::Cosine {
            start: self.lr.start_value(),
            end: final_lr,
            num_steps: over_steps,
        };
        self
    }

    /// Sets the number of epochs the lr should linearly ramp up to the LR at the start of training.
    ///
    /// This method can be chained in a builder-pattern kind of way.
    pub fn and_soft_start(mut self, epochs: usize) -> Self {
        self.soft_start_epochs = Some(epochs);
        self
    }

    /// Sets the maximum magnitude a single gradient can be. Any gradient larger in magnitude is
    /// limited to this magnitude.
    pub fn and_gradient_clip(mut self, clip: f32) -> Self {
        self.grad_clip = Some(clip);
        self
    }

    pub fn current_lr(&self) -> f32 {
        let lr = self.lr.at_timestep(self.step);
        match self.soft_start_epochs {
            Some(soft_start_epochs) => {
                if self.step < soft_start_epochs {
                    lr / soft_start_epochs as f32 * (1 + self.step) as f32
                } else {
                    lr
                }
            }
            None => lr,
        }
    }

    fn clip_grad<G: Dtype>(&self, grad: G) -> G {
        if let Some(clip) = self.grad_clip {
            let clip = G::from_f32(clip).unwrap();
            let neg_clip = G::default() - clip;
            if grad > clip {
                clip
            } else if grad < neg_clip {
                neg_clip
            } else {
                grad
            }
        } else {
            grad
        }
    }

    /// Returns the [TrainParams] structure. Exists on [TrainParams] to match the signature
    /// of other updater implementations.
    pub fn train_params(&self) -> &Self {
        self
    }
}

impl<G: Gradients> GradAdjuster<G> for TrainParams {
    fn adjust(&mut self, mut gradient_updates: G, loss: f32) -> G {
        let l = G::Concrete::from_f32(-loss * self.current_lr()).unwrap();
        gradient_updates
            .grad_iter_mut()
            .for_each(|g| *g = self.clip_grad(*g) * l);
        gradient_updates
    }
}

impl GradApplyer for TrainParams {
    fn apply<G: Gradients>(
        &mut self,
        gradient_updates: G,
        weights: &mut G,
    ) -> Result<(), crate::Error> {
        let l1 = self.l1_reg.as_ref().map(|d| d.at_timestep(self.step));
        let l2 = self.l2_reg.as_ref().map(|d| d.at_timestep(self.step));

        weights
            .grad_iter_mut_with_class()
            .zip(gradient_updates.into_grads())
            .for_each(|((w, c), u)| {
                let reg_penalty = if c.should_regularize() {
                    G::Concrete::from_f32(if let Some(l1) = l1 {
                        if *w > G::Concrete::default() {
                            l1
                        } else if *w < G::Concrete::default() {
                            -l1
                        } else {
                            0.0
                        }
                    } else {
                        0.0
                    })
                    .unwrap()
                        + if let Some(l2) = l2 {
                            (*w) * (G::Concrete::ONE + G::Concrete::ONE)
                                * G::Concrete::from_f32(l2).unwrap()
                        } else {
                            G::Concrete::default()
                        }
                } else {
                    G::Concrete::default()
                };

                *w += u - reg_penalty;
            });

        Ok(())
    }

    fn advance_step(&mut self) {
        self.step += 1;
    }
}

/// Implements momentum computation in addition to the basics provided by [TrainParams].
///
/// Implements optimizer traits, so it can be passed into training methods.
pub struct Momentum<G: Gradients> {
    params: TrainParams,
    velocity: G,
    momentum_coeff: f32,

    similarity_term: Option<f32>,
}

impl<G: Gradients> Momentum<G> {
    /// Constructs a new object with the given coefficient for momentum.
    ///
    /// Typical values are 0 to 1, with 0.5 being a good starting value.
    /// 0.9 is used by torch, but leads to oscillations with high learning
    /// rates or small batch sizes.
    pub fn new(params: TrainParams, momentum_coeff: f32) -> Momentum<G> {
        Self {
            params,
            momentum_coeff,
            velocity: G::empty(),
            similarity_term: None, // Typical value 0.5=>1.5,
        }
    }

    /// Sets the penalty term for updates that deviate (in cosine distance) from
    /// the velocity. Typical values are 0.5 to 1.5.
    pub fn and_similarity_penalty(mut self, similarity_term: f32) -> Self {
        self.similarity_term = Some(similarity_term);
        self
    }

    fn update(&mut self, gradient_updates: G, loss: f32) -> G {
        let mc = G::Concrete::from_f32(self.momentum_coeff).unwrap();
        let loss = G::Concrete::from_f32(-loss * self.params.current_lr()).unwrap();

        let sim_mul = G::Concrete::from_f32(if let Some(term) = self.similarity_term {
            self.velocity
                .cosine_similarity(&gradient_updates)
                .map(|x| (2.0 * (x + 1.0) + term).log2() + 0.25)
                .unwrap_or(1.0)
        } else {
            1.0
        })
        .unwrap();

        // v = coeff * last_v + gradient_updates
        self.velocity
            .grad_iter_mut()
            .zip(gradient_updates.into_grads())
            .for_each(|(v, g)| {
                *v = (*v * mc) + (self.params.clip_grad(g) * loss * sim_mul);
            });

        self.velocity.clone()
    }

    /// Returns the [TrainParams] structure.
    pub fn train_params(&self) -> &TrainParams {
        &self.params
    }
}

impl<G: Gradients> GradAdjuster<G> for Momentum<G> {
    fn adjust(&mut self, gradient_updates: G, loss: f32) -> G {
        self.update(gradient_updates, loss)
    }
}

impl<G: Gradients> GradApplyer for Momentum<G> {
    fn apply<G2: Gradients>(
        &mut self,
        gradient_updates: G2,
        weights: &mut G2,
    ) -> Result<(), crate::Error> {
        self.params.apply(gradient_updates, weights)
    }

    fn advance_step(&mut self) {
        self.params.advance_step();
    }
}

enum RMSPropBase<G: Gradients> {
    NoMomentum(TrainParams),
    Momentum(Momentum<G>),
}

impl<G: Gradients> RMSPropBase<G> {
    /// Returns the [TrainParams] structure.
    pub fn train_params(&self) -> &TrainParams {
        match self {
            RMSPropBase::NoMomentum(p) => p,
            RMSPropBase::Momentum(m) => m.train_params(),
        }
    }
}

impl<G: Gradients> GradAdjuster<G> for RMSPropBase<G> {
    fn adjust(&mut self, gradient_updates: G, loss: f32) -> G {
        use RMSPropBase::*;
        match self {
            NoMomentum(params) => params.adjust(gradient_updates, loss),
            Momentum(m) => m.adjust(gradient_updates, loss),
        }
    }
}

impl<G: Gradients> GradApplyer for RMSPropBase<G> {
    fn apply<G2: Gradients>(
        &mut self,
        gradient_updates: G2,
        weights: &mut G2,
    ) -> Result<(), crate::Error> {
        use RMSPropBase::*;
        match self {
            NoMomentum(params) => params.apply(gradient_updates, weights),
            Momentum(m) => m.apply(gradient_updates, weights),
        }
    }

    fn advance_step(&mut self) {
        match self {
            RMSPropBase::NoMomentum(p) => p.advance_step(),
            RMSPropBase::Momentum(m) => m.advance_step(),
        }
    }
}

/// Implements rmsprop on top of basic training parameters or [Momentum].
pub struct RMSProp<G: Gradients>
where
    G::Concrete: Float,
{
    base: RMSPropBase<G>,
    accumulator: G,
    beta: f32,
}

impl<G: Gradients> RMSProp<G>
where
    G::Concrete: Float,
{
    /// Constructs a new rmsprop optimizer.
    pub fn new(params: TrainParams, beta: f32) -> RMSProp<G> {
        Self {
            beta,
            base: RMSPropBase::NoMomentum(params),
            accumulator: G::empty(),
        }
    }

    /// Constructs a new rmsprop optimizer with momentum.
    pub fn new_with_momentum(params: TrainParams, momentum_coeff: f32, beta: f32) -> RMSProp<G> {
        Self {
            beta,
            base: RMSPropBase::Momentum(Momentum::new(params, momentum_coeff)),
            accumulator: G::empty(),
        }
    }

    /// Sets the penalty term for updates that deviate (in cosine distance) from
    /// the velocity. Typical values are 0.5 to 1.5.
    pub fn and_similarity_penalty(mut self, similarity_term: f32) -> Self {
        if let RMSPropBase::Momentum(m) = &mut self.base {
            m.similarity_term = Some(similarity_term);
        }
        self
    }

    /// Returns the [TrainParams] structure.
    pub fn train_params(&self) -> &TrainParams {
        self.base.train_params()
    }
}

impl<G: Gradients> GradAdjuster<G> for RMSProp<G>
where
    G::Concrete: Float,
{
    fn adjust(&mut self, mut gradient_updates: G, loss: f32) -> G {
        let b = G::Concrete::from_f32(self.beta).unwrap();
        self.accumulator
            .grad_iter_mut()
            .zip(gradient_updates.grad_iter_mut())
            .for_each(|(a, u)| {
                let new_a = (*a * b) + (G::Concrete::ONE - b) * (*u) * (*u);
                *a = new_a;

                // rmsprop divides the learning rate by sqrt(accumulator + epsilon).
                // the learning rate will be multiplied in next, so we just apply it to
                // the whole gradient.
                *u /= (new_a + G::Concrete::SMOL).sqrt();
            });

        self.base.adjust(gradient_updates, loss)
    }
}

impl<G: Gradients> GradApplyer for RMSProp<G>
where
    G::Concrete: Float,
{
    fn apply<G2: Gradients>(
        &mut self,
        gradient_updates: G2,
        weights: &mut G2,
    ) -> Result<(), crate::Error> {
        self.base.apply(gradient_updates, weights)
    }

    fn advance_step(&mut self) {
        self.base.advance_step();
    }
}