-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimizer.py
More file actions
902 lines (702 loc) · 33.2 KB
/
Copy pathoptimizer.py
File metadata and controls
902 lines (702 loc) · 33.2 KB
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
'''
JAX Optimization Algorithms Module
* Last Updated: 2025-03-15
* Author: HugoPhi, [GitHub](https://github.com/HugoPhi)
* Maintainer: hugonelsonm3@gmail.com
This module provides a collection of optimization algorithms implemented in JAX,
including Gradient Descent, Momentum, Nesterov Accelerated Gradient, AdaGrad,
RMSProp, AdaDelta, and Adam. These optimizers are designed to work seamlessly
with JAX's functional programming paradigm and automatic differentiation.
Key Features:
- Abstract base class `Optimizter` for defining custom optimizers
- Implementations of popular optimization algorithms:
- Gradient Descent (RawGD)
- Momentum
- Nesterov Accelerated Gradient (Nesterov)
- AdaGrad
- RMSProp
- AdaDelta
- Adam
- Support for batch processing and training state management
- Integration with JAX's JIT compilation for performance optimization
Structure:
- Abstract `Optimizter` class with core functionality for managing parameters and training state
- Concrete optimizer classes implementing specific algorithms
- Batch processing utilities for efficient training
Typical Usage:
1. Initialize an optimizer with model parameters and hyperparameters
2. Open the optimizer with training data and a loss function
3. Update parameters using the optimizer's `update` method
4. Close the optimizer when training is complete
Note: All optimizers assume the use of JAX's functional programming paradigm and
require proper management of model parameters and optimizer states.
'''
import jax.numpy as jnp
from jax import grad, tree, lax, random
from abc import ABC, abstractmethod
class Optimizter(ABC):
'''
An abstract base class for optimization algorithms. It serves as a container for model parameters
and provides methods to update the parameters during training. This class is designed to be
inherited by specific optimization algorithms (e.g., SGD, Adam, etc.).
The optimizer manages the training process by:
1. Storing model parameters.
2. Updating parameters using gradients computed from a loss function.
3. Handling batch processing and training state (e.g., open/close).
Subclasses must implement the abstract methods `__init__`, `update`, and `flash`.
Attributes:
params: The model parameters to be optimized.
lr: Learning rate (if applicable to the optimizer).
batch_size: Size of each training batch.
is_open: A boolean indicating whether the optimizer is in an active state (open for training).
steps: The number of optimization steps taken so far.
'''
@abstractmethod
def __init__(self):
'''
Initializes the optimizer. Subclasses must implement this method to set up optimizer-specific
attributes (e.g., learning rate, momentum, etc.) and initialize the model parameters.
'''
self.is_open = False
pass
@abstractmethod
def update(self):
'''
Updates the model parameters using the gradients computed from the loss function.
Subclasses must implement this method to define the specific optimization algorithm.
'''
pass
@abstractmethod
def flash(self):
'''
Resets the optimizer's internal state (e.g., momentum buffers, gradient accumulators).
This method is called when the optimizer is opened or re-initialized.
Subclasses must implement this method.
'''
pass
def open(self, loss_function, x_train: jnp.ndarray, y_train: jnp.ndarray, short_batch='drop', key=None):
'''
Prepares the optimizer for training by initializing its state and setting up the training data.
Args:
loss_function: A loss function that computes the scalar loss given model parameters,
input data, and true labels. It must be JIT-compiled.
Signature: `f(params, x, y_true) -> scalar`.
x_train: Input data for training. Shape: `(num_samples, ...)`.
y_train: True labels for training. Shape: `(num_samples, ...)`.
short_batch: The Strategy to handle short batch. including:
- 'drop': drop short batch, used when: dataset size >> batch size, num_batches = N // B
- 'pad': append arr[-B:] to trimmed arr, used when: dataset size >~ batch size, num_batches = N // B + 1
key: A random number generator key used for initialization.
Notes:
- The training data is divided into batches based on the `batch_size` attribute.
- If the dataset size is not divisible by `batch_size`, the last batch is dropped.
- The optimizer must be opened before calling `update` or accessing parameters.
'''
if self.is_open is True:
print('oprimizer is already opened.')
else:
self.is_open = True
self.flash()
self._loss = loss_function
self.key = key
if short_batch == 'drop':
self.num_batches = x_train.shape[0] // self.batch_size
trimmed_size = self.num_batches * self.batch_size
self.x_train = x_train[:trimmed_size]
self.y_train = y_train[:trimmed_size]
elif short_batch == 'pad':
self.num_batches = x_train.shape[0] // self.batch_size
trimmed_size = self.num_batches * self.batch_size
self.x_train = jnp.concatenate((x_train[:trimmed_size], x_train[-self.batch_size:]), axis=0) # pad last batch
self.y_train = jnp.concatenate((y_train[:trimmed_size], y_train[-self.batch_size:]), axis=0)
self.num_batches += 1
else:
raise ValueError(f'short_batch must be in "drop" or "pad", but get {short_batch} instead.')
print(f'[*] oprimizer opened with {self.num_batches} batches with batch size {self.batch_size}.')
def close(self):
'''
Closes the optimizer, preventing further updates or parameter access until it is reopened.
'''
if self.is_open is False:
print('oprimizer is already closed.')
else:
self.is_open = False
def get_params(self):
'''
Returns the current model parameters.
Returns:
The model parameters being optimized.
'''
return self.params
def get_steps(self):
'''
Returns the number of optimization steps taken so far.
Returns:
The number of steps (integer).
'''
return self.steps
class Adam(Optimizter):
'''
Adam (Adaptive Moment Estimation) optimizer, a popular stochastic optimization algorithm
that combines the benefits of Momentum and RMSProp. It adapts the learning rate for each
parameter using estimates of the first and second moments of the gradients.
Attributes:
params: The model parameters to be optimized.
lr: Learning rate (default: 0.01).
beta1: Exponential decay rate for the first moment estimates (default: 0.9).
beta2: Exponential decay rate for the second moment estimates (default: 0.999).
epsilon: Small constant for numerical stability (default: 1e-6).
batch_size: Size of each training batch (default: 32).
V: First moment vector (momentum-like term).
VV: Second moment vector (RMSProp-like term).
steps: Number of optimization steps taken so far.
is_open: Boolean indicating whether the optimizer is active.
'''
def __init__(self, params,
lr=0.01, beta1=0.9, beta2=0.999, epsilon=1e-6,
batch_size=32):
'''
Initializes the Adam optimizer.
Args:
params: Model parameters to be optimized.
lr: Learning rate (default: 0.01).
beta1: Exponential decay rate for the first moment estimates (default: 0.9).
beta2: Exponential decay rate for the second moment estimates (default: 0.999).
epsilon: Small constant for numerical stability (default: 1e-6).
batch_size: Size of each training batch (default: 32).
'''
super().__init__()
self.params = params
self.lr = lr
self.beta1 = beta1
self.beta2 = beta2
self.epsilon = epsilon
self.batch_size = batch_size
def flash(self):
'''
Resets the optimizer's internal state, including moment vectors and step count.
This method is called when the optimizer is opened or re-initialized.
'''
self.V = tree.map(lambda x: jnp.zeros_like(x), self.params)
self.VV = tree.map(lambda x: jnp.zeros_like(x), self.params)
self.steps = 0
def update(self):
'''
Performs a single optimization step using the Adam algorithm. It updates the model
parameters based on the gradients computed from the loss function.
Raises:
ValueError: If the optimizer is not open.
'''
if self.is_open is False:
raise ValueError('please open optimizer first!!!')
else:
ixs = jnp.arange(self.num_batches)
bxs = self.x_train.reshape(self.num_batches, self.batch_size, *self.x_train.shape[1:])
bys = self.y_train.reshape(self.num_batches, self.batch_size, *self.y_train.shape[1:])
if self.key is not None:
subkeys = random.split(self.key, self.num_batches + 1)
self.key, subkeys = subkeys[0], subkeys[1:] # update self.key & get subkeys
def one_batch(carry, ix):
def adam(d_w, w, v, vv):
# t = jnp.clip(carry['steps'], 0, 1e3) + 1
t = carry['steps'] + 1
new_v = self.beta1 * v + (1 - self.beta1) * d_w
new_vv = self.beta2 * vv + (1 - self.beta2) * d_w * d_w
v_hat = new_v / (1 - self.beta1**t)
vv_hat = new_vv / (1 - self.beta2**t)
step = - self.lr * v_hat / (jnp.sqrt(vv_hat) + self.epsilon)
new_w = w + step
return jnp.stack((
new_w,
new_v,
new_vv,
))
bx = bxs[ix]
by = bys[ix]
if self.key is not None:
kkey = subkeys[ix]
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by, kkey)
else:
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by)
pack = tree.map(adam, d_params, carry['params'], carry['V'], carry['VV']) # use Adam
carry['params'] = tree.map(lambda x: x[0], pack)
carry['V'] = tree.map(lambda x: x[1], pack)
carry['VV'] = tree.map(lambda x: x[2], pack)
carry['steps'] += 1
return carry, None
pack, _ = lax.scan(one_batch, {
'params': self.params,
'V': self.V,
'VV': self.VV,
'steps': self.steps,
}, ixs)
self.params, self.V, self.VV, self.steps = pack['params'], pack['V'], pack['VV'], pack['steps']
class RawGD(Optimizter):
'''
A simple Gradient Descent (GD) optimizer. This optimizer updates model parameters
by moving them in the direction of the negative gradient, scaled by a learning rate.
Attributes:
params: The model parameters to be optimized.
lr: Learning rate (default: 0.01).
batch_size: Size of each training batch (default: 32).
steps: Number of optimization steps taken so far.
open: Boolean indicating whether the optimizer is active.
'''
def __init__(self, params,
lr=0.01, batch_size=32):
'''
Initializes the Gradient Descent optimizer.
Args:
params: Model parameters to be optimized.
lr: Learning rate (default: 0.01).
batch_size: Size of each training batch (default: 32).
'''
super().__init__()
self.params = params
self.lr = lr
self.batch_size = batch_size
def flash(self):
'''
Resets the optimizer's internal state, including the step count.
This method is called when the optimizer is opened or re-initialized.
'''
self.steps = 0
self.is_open = False
def update(self):
'''
Performs a single optimization step using Gradient Descent. It updates the model
parameters based on the gradients computed from the loss function.
Raises:
ValueError: If the optimizer is not open.
'''
if self.is_open is False:
raise ValueError('please open optimizer first!!!')
else:
ixs = jnp.arange(self.num_batches)
bxs = self.x_train.reshape(self.num_batches, self.batch_size, *self.x_train.shape[1:])
bys = self.y_train.reshape(self.num_batches, self.batch_size, *self.y_train.shape[1:])
if self.key is not None:
subkeys = random.split(self.key, self.num_batches + 1)
self.key, subkeys = subkeys[0], subkeys[1:] # update self.key & get subkeys
def one_batch(carry, ix):
def gd(d_w, w):
new_w = w - self.lr * d_w
return new_w
bx = bxs[ix]
by = bys[ix]
if self.key is not None:
kkey = subkeys[ix]
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by, kkey)
else:
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by)
pack = tree.map(gd, d_params, carry['params'])
carry['params'] = pack
carry['steps'] += 1
return carry, None
pack, _ = lax.scan(one_batch, {
'params': self.params,
'steps': self.steps,
}, ixs)
self.params, self.steps = pack['params'], pack['steps']
class Momenum(Optimizter):
'''
Momentum optimizer, a variant of Gradient Descent that incorporates momentum to accelerate
convergence and reduce oscillations. It accumulates a velocity vector in the direction of
the gradient and uses it to update the parameters.
Attributes:
params: The model parameters to be optimized.
lr: Learning rate (default: 0.01).
beta: Momentum factor (default: 0.9).
batch_size: Size of each training batch (default: 32).
V: Velocity vector (accumulated gradients).
steps: Number of optimization steps taken so far.
open: Boolean indicating whether the optimizer is active.
'''
def __init__(self, params,
lr=0.01, beta=0.9,
batch_size=32):
'''
Initializes the Momentum optimizer.
Args:
params: Model parameters to be optimized.
lr: Learning rate (default: 0.01).
beta: Momentum factor (default: 0.9).
batch_size: Size of each training batch (default: 32).
'''
super().__init__()
self.params = params
self.lr = lr
self.beta = beta
self.batch_size = batch_size
def flash(self):
'''
Resets the optimizer's internal state, including the velocity vector and step count.
This method is called when the optimizer is opened or re-initialized.
'''
self.V = tree.map(lambda x: jnp.zeros_like(x), self.params)
self.steps = 0
self.is_open = 0
def update(self):
'''
Performs a single optimization step using Momentum. It updates the model parameters
based on the gradients computed from the loss function and the accumulated velocity.
Raises:
ValueError: If the optimizer is not open.
'''
if self.is_open is False:
raise ValueError('please open optimizer first!!!')
else:
ixs = jnp.arange(self.num_batches)
bxs = self.x_train.reshape(self.num_batches, self.batch_size, *self.x_train.shape[1:])
bys = self.y_train.reshape(self.num_batches, self.batch_size, *self.y_train.shape[1:])
if self.key is not None:
subkeys = random.split(self.key, self.num_batches + 1)
self.key, subkeys = subkeys[0], subkeys[1:] # update self.key & get subkeys
def one_batch(carry, ix):
def momentum(d_w, w, v):
new_v = self.beta * v + (1 - self.beta) * d_w
new_w = w - self.lr * new_v
return jnp.stack((new_w, new_v))
bx = bxs[ix]
by = bys[ix]
if self.key is not None:
kkey = subkeys[ix]
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by, kkey)
else:
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by)
pack = tree.map(momentum, d_params, carry['params'], carry['V'])
carry['params'] = tree.map(lambda x: x[0], pack)
carry['V'] = tree.map(lambda x: x[1], pack)
carry['steps'] += 1
return carry, None
pack, _ = lax.scan(one_batch, {
'params': self.params,
'V': self.V,
'steps': self.steps,
}, ixs)
self.params, self.V, self.steps = pack['params'], pack['V'], pack['steps']
class Nesterov(Optimizter):
'''
Nesterov Accelerated Gradient (NAG) optimizer, an extension of Momentum that improves
convergence by incorporating a "lookahead" step. It first makes a momentum-based update
and then corrects the update using the gradient at the new position.
Attributes:
params: The model parameters to be optimized.
lr: Learning rate (default: 0.01).
beta: Momentum factor (default: 0.9).
batch_size: Size of each training batch (default: 32).
V: Velocity vector (accumulated gradients).
steps: Number of optimization steps taken so far.
open: Boolean indicating whether the optimizer is active.
'''
def __init__(self, params,
lr=0.01, beta=0.9,
batch_size=32):
'''
Initializes the Nesterov Accelerated Gradient (NAG) optimizer.
Args:
params: Model parameters to be optimized.
lr: Learning rate (default: 0.01).
beta: Momentum factor (default: 0.9).
batch_size: Size of each training batch (default: 32).
'''
super().__init__()
self.params = params
self.lr = lr
self.beta = beta
self.batch_size = batch_size
def flash(self):
'''
Resets the optimizer's internal state, including the velocity vector and step count.
This method is called when the optimizer is opened or re-initialized.
'''
self.V = tree.map(lambda x: jnp.zeros_like(x), self.params)
self.steps = 0
self.is_open = False
def update(self):
'''
Performs a single optimization step using Nesterov Accelerated Gradient (NAG).
It updates the model parameters based on the gradients computed from the loss function
and the accumulated velocity, with a "lookahead" correction.
Raises:
ValueError: If the optimizer is not open.
'''
if self.is_open is False:
raise ValueError('please open optimizer first!!!')
else:
ixs = jnp.arange(self.num_batches)
bxs = self.x_train.reshape(self.num_batches, self.batch_size, *self.x_train.shape[1:])
bys = self.y_train.reshape(self.num_batches, self.batch_size, *self.y_train.shape[1:])
if self.key is not None:
subkeys = random.split(self.key, self.num_batches + 1)
self.key, subkeys = subkeys[0], subkeys[1:] # update self.key & get subkeys
def one_batch(carry, ix):
def nag(d_w, w, v):
# Nesterov update
v_prev = v
v = self.beta * v_prev - self.lr * d_w
new_w = w + self.beta * v - self.lr * d_w
return jnp.stack((new_w, v))
bx = bxs[ix]
by = bys[ix]
if self.key is not None:
kkey = subkeys[ix]
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by, kkey)
else:
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by)
pack = tree.map(nag, d_params, carry['params'], carry['V'])
carry['params'] = tree.map(lambda x: x[0], pack)
carry['V'] = tree.map(lambda x: x[1], pack)
carry['steps'] += 1
return carry, None
pack, _ = lax.scan(one_batch, {
'params': self.params,
'V': self.V,
'steps': self.steps,
}, ixs)
self.params, self.V, self.steps = pack['params'], pack['V'], pack['steps']
class AdaGrad(Optimizter):
'''
AdaGrad (Adaptive Gradient) optimizer, an algorithm that adapts the learning rate for each
parameter based on the historical gradients. It performs larger updates for infrequent
parameters and smaller updates for frequent parameters, making it well-suited for sparse data.
Attributes:
params: The model parameters to be optimized.
lr: Learning rate (default: 0.01).
epsilon: Small constant for numerical stability (default: 1e-8).
batch_size: Size of each training batch (default: 32).
G: Accumulated squared gradients for each parameter.
steps: Number of optimization steps taken so far.
open: Boolean indicating whether the optimizer is active.
'''
def __init__(self, params,
lr=0.01, epsilon=1e-8,
batch_size=32):
'''
Initializes the AdaGrad optimizer.
Args:
params: Model parameters to be optimized.
lr: Learning rate (default: 0.01).
epsilon: Small constant for numerical stability (default: 1e-8).
batch_size: Size of each training batch (default: 32).
'''
super().__init__()
self.params = params
self.lr = lr
self.epsilon = epsilon
self.batch_size = batch_size
def flash(self):
'''
Resets the optimizer's internal state, including the accumulated squared gradients
and step count. This method is called when the optimizer is opened or re-initialized.
'''
self.G = tree.map(lambda x: jnp.zeros_like(x), self.params)
self.steps = 0
self.is_open = False
def update(self):
'''
Performs a single optimization step using AdaGrad. It updates the model parameters
based on the gradients computed from the loss function and the accumulated squared gradients.
Raises:
ValueError: If the optimizer is not open.
'''
if self.is_open is False:
raise ValueError('please open optimizer first!!!')
else:
ixs = jnp.arange(self.num_batches)
bxs = self.x_train.reshape(self.num_batches, self.batch_size, *self.x_train.shape[1:])
bys = self.y_train.reshape(self.num_batches, self.batch_size, *self.y_train.shape[1:])
if self.key is not None:
subkeys = random.split(self.key, self.num_batches + 1)
self.key, subkeys = subkeys[0], subkeys[1:] # update self.key & get subkeys
def one_batch(carry, ix):
def adagrad(d_w, w, g):
new_g = g + d_w ** 2
new_w = w - self.lr * d_w / (jnp.sqrt(new_g) + self.epsilon)
return jnp.stack((new_w, new_g))
bx = bxs[ix]
by = bys[ix]
if self.key is not None:
kkey = subkeys[ix]
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by, kkey)
else:
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by)
pack = tree.map(adagrad, d_params, carry['params'], carry['G'])
carry['params'] = tree.map(lambda x: x[0], pack)
carry['G'] = tree.map(lambda x: x[1], pack)
carry['steps'] += 1
return carry, None
pack, _ = lax.scan(one_batch, {
'params': self.params,
'G': self.G,
'steps': self.steps,
}, ixs)
self.params, self.G, self.steps = pack['params'], pack['G'], pack['steps']
class RMSProp(Optimizter):
'''
RMSProp (Root Mean Square Propagation) optimizer, an adaptive learning rate optimization
algorithm that uses a moving average of squared gradients to normalize the gradient updates.
It addresses the issue of monotonically decreasing learning rates in AdaGrad by using an
exponentially decaying average of squared gradients.
Attributes:
params: The model parameters to be optimized.
lr: Learning rate (default: 0.01).
beta: Decay rate for the moving average of squared gradients (default: 0.9).
epsilon: Small constant for numerical stability (default: 1e-8).
batch_size: Size of each training batch (default: 32).
G: Exponentially decaying average of squared gradients for each parameter.
steps: Number of optimization steps taken so far.
open: Boolean indicating whether the optimizer is active.
'''
def __init__(self, params,
lr=0.01, beta=0.9, epsilon=1e-8,
batch_size=32):
'''
Initializes the RMSProp optimizer.
Args:
params: Model parameters to be optimized.
lr: Learning rate (default: 0.01).
beta: Decay rate for the moving average of squared gradients (default: 0.9).
epsilon: Small constant for numerical stability (default: 1e-8).
batch_size: Size of each training batch (default: 32).
'''
super().__init__()
self.params = params
self.lr = lr
self.beta = beta
self.epsilon = epsilon
self.batch_size = batch_size
def flash(self):
'''
Resets the optimizer's internal state, including the moving average of squared gradients
and step count. This method is called when the optimizer is opened or re-initialized.
'''
self.G = tree.map(lambda x: jnp.zeros_like(x), self.params)
self.steps = 0
self.is_open = False
def update(self):
'''
Performs a single optimization step using RMSProp. It updates the model parameters
based on the gradients computed from the loss function and the moving average of
squared gradients.
Raises:
ValueError: If the optimizer is not open.
'''
if self.is_open is False:
raise ValueError('please open optimizer first!!!')
else:
ixs = jnp.arange(self.num_batches)
bxs = self.x_train.reshape(self.num_batches, self.batch_size, *self.x_train.shape[1:])
bys = self.y_train.reshape(self.num_batches, self.batch_size, *self.y_train.shape[1:])
if self.key is not None:
subkeys = random.split(self.key, self.num_batches + 1)
self.key, subkeys = subkeys[0], subkeys[1:] # update self.key & get subkeys
def one_batch(carry, ix):
def rmsprop(d_w, w, g):
new_g = self.beta * g + (1 - self.beta) * d_w ** 2
new_w = w - self.lr * d_w / (jnp.sqrt(new_g) + self.epsilon)
return jnp.stack((new_w, new_g))
bx = bxs[ix]
by = bys[ix]
if self.key is not None:
kkey = subkeys[ix]
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by, kkey)
else:
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by)
pack = tree.map(rmsprop, d_params, carry['params'], carry['G'])
carry['params'] = tree.map(lambda x: x[0], pack)
carry['G'] = tree.map(lambda x: x[1], pack)
carry['steps'] += 1
return carry, None
pack, _ = lax.scan(one_batch, {
'params': self.params,
'G': self.G,
'steps': self.steps,
}, ixs)
self.params, self.G, self.steps = pack['params'], pack['G'], pack['steps']
class AdaDelta(Optimizter):
'''
AdaDelta (Adaptive Delta) optimizer, an adaptive learning rate optimization algorithm that
dynamically adjusts the learning rate based on a moving window of gradient updates. It
eliminates the need for a manually set learning rate by using a ratio of the root mean
square (RMS) of parameter updates to the RMS of gradients.
Attributes:
params: The model parameters to be optimized.
rho: Decay rate for the moving averages of squared gradients and updates (default: 0.9).
epsilon: Small constant for numerical stability (default: 1e-8).
batch_size: Size of each training batch (default: 32).
E_g2: Exponentially decaying average of squared gradients.
E_dx2: Exponentially decaying average of squared parameter updates.
steps: Number of optimization steps taken so far.
open: Boolean indicating whether the optimizer is active.
'''
def __init__(self, params,
rho=0.9, epsilon=1e-8,
batch_size=32):
'''
Initializes the AdaDelta optimizer.
Args:
params: Model parameters to be optimized.
rho: Decay rate for the moving averages of squared gradients and updates (default: 0.9).
epsilon: Small constant for numerical stability (default: 1e-8).
batch_size: Size of each training batch (default: 32).
'''
super().__init__()
self.params = params
self.rho = rho
self.epsilon = epsilon
self.batch_size = batch_size
def flash(self):
'''
Resets the optimizer's internal state, including the moving averages of squared gradients
and updates, and the step count. This method is called when the optimizer is opened or
re-initialized.
'''
self.E_g2 = tree.map(lambda x: jnp.zeros_like(x), self.params) # expontential average of squared gradient
self.E_dx2 = tree.map(lambda x: jnp.zeros_like(x), self.params) # expontential average of squared update
self.steps = 0
self.is_open = False
def update(self):
'''
Performs a single optimization step using AdaDelta. It updates the model parameters
based on the gradients computed from the loss function and the moving averages of
squared gradients and updates.
Raises:
ValueError: If the optimizer is not open.
'''
if self.is_open is False:
raise ValueError('please open optimizer first!!!')
else:
ixs = jnp.arange(self.num_batches)
bxs = self.x_train.reshape(self.num_batches, self.batch_size, *self.x_train.shape[1:])
bys = self.y_train.reshape(self.num_batches, self.batch_size, *self.y_train.shape[1:])
if self.key is not None:
subkeys = random.split(self.key, self.num_batches + 1)
self.key, subkeys = subkeys[0], subkeys[1:] # update self.key & get subkeys
def one_batch(carry, ix):
def adadelta(d_w, w, e_g2, e_dx2):
new_e_g2 = self.rho * e_g2 + (1 - self.rho) * d_w ** 2 # update squared gradient
delta_w = -jnp.sqrt(e_dx2 + self.epsilon) / jnp.sqrt(new_e_g2 + self.epsilon) * d_w
new_w = w + delta_w
new_e_dx2 = self.rho * e_dx2 + (1 - self.rho) * delta_w ** 2 # update squared update
return jnp.stack((new_w, new_e_g2, new_e_dx2))
bx = bxs[ix]
by = bys[ix]
if self.key is not None:
kkey = subkeys[ix]
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by, kkey)
else:
d_params = grad(self._loss, argnums=0)(carry['params'], bx, by)
pack = tree.map(adadelta, d_params, carry['params'], carry['E_g2'], carry['E_dx2'])
carry['params'] = tree.map(lambda x: x[0], pack)
carry['E_g2'] = tree.map(lambda x: x[1], pack)
carry['E_dx2'] = tree.map(lambda x: x[2], pack)
carry['steps'] += 1
return carry, None
pack, _ = lax.scan(one_batch, {
'params': self.params,
'E_g2': self.E_g2,
'E_dx2': self.E_dx2,
'steps': self.steps,
}, ixs)
self.params, self.E_g2, self.E_dx2, self.steps = pack['params'], pack['E_g2'], pack['E_dx2'], pack['steps']