-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOptimizer.java
656 lines (543 loc) · 21.7 KB
/
Optimizer.java
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
/*
* MIT License
*
* Copyright (c) 2019 Sebastian Gössl
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package optimization;
import java.util.Arrays;
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleUnaryOperator;
/**
* Mathematical optimizer.
* Takes a single Optimizable object & optimizes it.
* KeepTraining has to be implemented to check when the optimization process
* is completed.
* Override publish to output intermediate results.
*
* @author Sebastian Gössl
* @version 1.1 13.9.2019
* @param <T> Input & output type
*/
public abstract class Optimizer<T> {
/**
* Object to optimize.
*/
private final Optimizable<T> optimizable;
/**
* Contructs a new Optimizer with the given Optimizable.
*
* @param optimizable object to optimize
*/
public Optimizer(Optimizable<T> optimizable) {
this.optimizable = optimizable;
}
/**
* Returns if the optimization process should continue.
*
* @param optimizable object to optimize
* @param iteration current iteration
* @param cost current cost
* @return if the optimization process should continue
*/
public abstract boolean keepTraining(Optimizable<T> optimizable,
int iteration, double cost);
/**
* Optional method that gets called every iteration to output intermediate
* results.
*
* @param optimizable object to optimize
* @param iteration current iteration
* @param cost current cost
*/
public void publish(Optimizable<T> optimizable,
int iteration, double cost) {}
/**
* Applies the given operand to all elements of the given array and
* returns the result.
* The given array is not modified.
*
* @param operand operand
* @param operator operator
* @return result
*/
private double[] apply(double[] operand, DoubleUnaryOperator operator) {
final double[] result = new double[operand.length];
Arrays.setAll(result, (i) -> operator.applyAsDouble(operand[i]));
return result;
}
/**
* Applies the given operand to all elements of the given arrays
* elementwise and returns the result.
* The given arrays are not modified.
*
* @param operand1 first operand
* @param operand2 second operand
* @param operator operator
* @return result
*/
private double[] apply(double[] operand1, double[] operand2,
DoubleBinaryOperator operator) {
final double[] result =
new double[Math.min(operand1.length, operand2.length)];
Arrays.setAll(result,
(i) -> operator.applyAsDouble(operand1[i], operand2[i]));
return result;
}
/**
* Performs a single gradient descent iteration.
*
* @param learningRate learning rate
* @param input input sets
* @param output ouput sets
* @return cost after iteration
*/
private double gradientDescent(double learningRate,
T[] input, T[] output) {
final double[] costPrime = optimizable.costPrime(input, output);
final double[] update = apply(costPrime, (x) -> learningRate * x);
final double[] oldParams = optimizable.getParameters();
final double[] newParams = apply(oldParams, update, (x, y) -> x - y);
optimizable.setParameters(newParams);
return optimizable.cost(input, output);
}
/**
* Performs a single gradient descent iteration with momentum.
*
* @param learningRate learning rate
* @param momentum momentum
* @param input input sets
* @param output ouput sets
* @param velocity current parameter velocities (gets updated)
* @return cost after iteration
*/
private double gradientDescentMomentum(double learningRate,
double momentum, T[] input, T[] output, double[] velocity) {
final double[] costPrime = optimizable.costPrime(input, output);
final double[] velocityNew = apply(costPrime, velocity,
(x, y) -> learningRate*x + momentum*y);
final double[] oldParams = optimizable.getParameters();
final double[] newParams = apply(oldParams, velocityNew,
(x, y) -> x - y);
optimizable.setParameters(newParams);
System.arraycopy(velocityNew, 0, velocity, 0, velocityNew.length);
return optimizable.cost(input, output);
}
/**
* Performs a single Nesterov gradient descent iteration.
*
* @param learningRate learning rate
* @param momentum momentum
* @param input input sets
* @param output ouput sets
* @param velocity current parameter velocities (gets updated)
* @return cost after iteration
*/
private double gradientDescentNesterov(double learningRate,
double momentum, T[] input, T[] output, double[] velocity) {
final double[] costPrime = optimizable.costPrime(input, output);
final double[] velocityNew = apply(velocity, costPrime,
(x, y) -> momentum*x - learningRate*y);
final double[] update = apply(velocity, velocityNew,
(x, y) -> (1 + momentum)*y - momentum*x);
final double[] oldParams = optimizable.getParameters();
final double[] newParams = apply(oldParams, update, (x, y) -> x + y);
optimizable.setParameters(newParams);
System.arraycopy(velocityNew, 0, velocity, 0, velocityNew.length);
return optimizable.cost(input, output);
}
/**
* Performs a single AdaGrad gradient descent iteration.
*
* @param learningRate learning rate
* @param input input sets
* @param output ouput sets
* @param gradientsMean parameter gradients mean (gets updated)
* @return cost after iteration
*/
private double gradientDescentAdagrad(double learningRate,
T[] input, T[] output, double[] gradientsMean) {
final double epsilon = 1e-8;
final double[] costPrime = optimizable.costPrime(input, output);
final double[] gradientsMeanNew = apply(gradientsMean, costPrime,
(x, y) -> x + y*y);
final double[] update = apply(costPrime, gradientsMeanNew,
(x, y) -> learningRate / (Math.sqrt(y) + epsilon) * x);
final double[] oldParams = optimizable.getParameters();
final double[] newParams = apply(oldParams, update, (x, y) -> x - y);
optimizable.setParameters(newParams);
System.arraycopy(gradientsMeanNew, 0,
gradientsMean, 0, gradientsMeanNew.length);
return optimizable.cost(input, output);
}
/**
* Performs a single RMSProp gradient descent iteration.
*
* @param learningRate learning rate
* @param decay decay of the gradient means
* @param input input sets
* @param output ouput sets
* @param gradientsMean parameter gradients mean (gets updated)
* @return cost after iteration
*/
private double gradientDescentRmsprop(double learningRate, double decay,
T[] input, T[] output, double[] gradientsMean) {
final double epsilon = 1e-8;
final double[] costPrime = optimizable.costPrime(input, output);
final double[] gradientsMeanNew = apply(gradientsMean, costPrime,
(x, y) -> decay*x + (1 - decay)*y*y);
final double[] update = apply(costPrime, gradientsMeanNew,
(x, y) -> learningRate / (Math.sqrt(y) + epsilon) * x);
final double[] oldParams = optimizable.getParameters();
final double[] newParams = apply(oldParams, update, (x, y) -> x - y);
optimizable.setParameters(newParams);
System.arraycopy(gradientsMeanNew, 0,
gradientsMean, 0, gradientsMeanNew.length);
return optimizable.cost(input, output);
}
/**
* Performs a single Adam gradient descent iteration.
*
* @param learningRate learning rate
* @param iteration current iteration
* @param beta1 gradient average decay
* @param beta2 squared gradient average decay
* @param input input sets
* @param output ouput sets
* @param firstMoment gradient average (gets updated)
* @param secondMoment squared gradient average (gets updated)
* @return cost after iteration
*/
private double gradientDescentAdam(double learningRate, int iteration,
double beta1, double beta2, T[] input, T[] output,
double[] firstMoment, double[] secondMoment) {
final double epsilon = 1e-8;
final double[] costPrime = optimizable.costPrime(input, output);
final double[] firstMomentNew = apply(firstMoment, costPrime,
(x, y) -> beta1*x + (1 - beta1)*y);
final double[] secondMomentNew = apply(secondMoment, costPrime,
(x, y) -> beta2*x + (1 - beta2)*y*y);
final double[] firstUnbias = apply(firstMomentNew,
(x) -> x / (1 - Math.pow(beta1, iteration + 1)));
final double[] secondUnbias = apply(secondMomentNew,
(x) -> x / (1 - Math.pow(beta2, iteration + 1)));
final double[] update = apply(firstUnbias, secondUnbias,
(x, y) -> learningRate / (Math.sqrt(y) + epsilon) * x);
final double[] oldParams = optimizable.getParameters();
final double[] newParams = apply(oldParams, update, (x, y) -> x - y);
optimizable.setParameters(newParams);
System.arraycopy(firstMomentNew, 0,
firstMoment, 0, firstMomentNew.length);
System.arraycopy(secondMomentNew, 0,
secondMoment, 0, secondMomentNew.length);
return optimizable.cost(input, output);
}
/**
* Loops while keepTraining returns true and calls the given function every
* iteration with all input & outputs sets.
*
* @param foo iteration function
* @param input input sets
* @param output ouput sets
* @return last cost
*/
private double batch(IterationFunction<T> foo, T[] input, T[] output) {
double lastCost = optimizable.cost(input, output);
for(int i=0; keepTraining(optimizable, i, lastCost); i++) {
final double currentCost = foo.apply(i, input, output);
publish(optimizable, i, currentCost);
lastCost = currentCost;
}
return lastCost;
}
/**
* Loops while keepTraining returns true and calls the given function every
* iteration with a single input & output sets.
*
* @param foo iteration function
* @param input input sets
* @param output ouput sets
* @return last cost
*/
private double stochastic(IterationFunction<T> foo, T[] input, T[] output) {
double lastCost = optimizable.cost(
Arrays.copyOfRange(input, 0, 1),
Arrays.copyOfRange(output, 0, 1));
for(int i=0; keepTraining(optimizable, i, lastCost); i++) {
final int setNr = i % input.length;
final T[] inputSet = Arrays.copyOfRange(input, setNr, setNr + 1);
final T[] outputSet = Arrays.copyOfRange(output, setNr, setNr + 1);
final double currentCost = foo.apply(i, inputSet, outputSet);
publish(optimizable, i, currentCost);
lastCost = currentCost;
}
return lastCost;
}
/**
* Loops while keepTraining returns true and calls the given function every
* iteration with a small batch of input & output sets.
*
* @param foo iteration function
* @param batchSize batchsize
* @param input input sets
* @param output ouput sets
* @return last cost
*/
private double miniBatch(IterationFunction<T> foo,
int batchSize, T[] input, T[] output) {
final int numberOfBatches =
(int)Math.ceil((double)input.length / batchSize);
double lastCost = optimizable.cost(
Arrays.copyOfRange(input, 0, batchSize),
Arrays.copyOfRange(output, 0, batchSize));
for(int i=0; keepTraining(optimizable, i, lastCost); i++) {
final int batchNr = i % numberOfBatches;
final int batchStart = batchSize * batchNr;
final int batchEnd =
Math.min(batchSize*(batchNr + 1), input.length);
final T[] inputSets =
Arrays.copyOfRange(input, batchStart, batchEnd);
final T[] outputSets =
Arrays.copyOfRange(output, batchStart, batchEnd);
final double currentCost = foo.apply(i, inputSets, outputSets);
publish(optimizable, i, currentCost);
lastCost = currentCost;
}
return lastCost;
}
/**
* Performs batch gradient descent optimization.
*
* @param learningRate learning rate
* @param input input sets
* @param output output sets
* @return last cost
*/
public double batchGradientDescent(double learningRate,
T[] input, T[] output) {
return batch(
(i, in, out) -> gradientDescent(learningRate, in, out),
input, output);
}
/**
* Performs stochastic gradient descent optimization.
*
* @param learningRate learning rate
* @param input input sets
* @param output output sets
* @return last cost
*/
public double stochasticGradientDescent(double learningRate,
T[] input, T[] output) {
return stochastic(
(i, in, out) -> gradientDescent(learningRate, in, out),
input, output);
}
/**
* Performs mini-batch gradient descent optimization.
*
* @param learningRate learning rate
* @param batchSize batch size
* @param input input sets
* @param output output sets
* @return last cost
*/
public double miniBatchGradientDescent(double learningRate, int batchSize,
T[] input, T[] output) {
return miniBatch(
(i, in, out) -> gradientDescent(learningRate, in, out),
batchSize, input, output);
}
/**
* Performs stochastic gradient descent optimization with momentum.
* Momentum = 0.9.
*
* @param learningRate learning rate
* @param input input sets
* @param output output sets
* @return last cost
*/
public double stochasticGradientDescentMomentum(double learningRate,
T[] input, T[] output) {
return stochasticGradientDescentMomentum(
learningRate, 0.9, input, output);
}
/**
* Performs stochastic gradient descent optimization with momentum.
*
* @param learningRate learning rate
* @param momentum momentum
* @param input input sets
* @param output output sets
* @return last cost
*/
public double stochasticGradientDescentMomentum(double learningRate,
double momentum, T[] input, T[] output) {
final double[] velocity =
new double[optimizable.getParameters().length];
return stochastic(
(i, in, out) -> gradientDescentMomentum(
learningRate, momentum, in, out, velocity),
input, output);
}
/**
* Performs Nesterov stochastic gradient descent optimization.
* Momentum = 0.9.
*
* @param learningRate learning rate
* @param input input sets
* @param output output sets
* @return last cost
*/
public double stochasticGradientDescentNesterov(double learningRate,
T[] input, T[] output) {
return stochasticGradientDescentNesterov(
learningRate, 0.9, input, output);
}
/**
* Performs Nesterov stochastic gradient descent optimization.
*
* @param learningRate learning rate
* @param momentum momentum
* @param input input sets
* @param output output sets
* @return last cost
*/
public double stochasticGradientDescentNesterov(double learningRate,
double momentum, T[] input, T[] output) {
final double[] velocity =
new double[optimizable.getParameters().length];
return stochastic(
(i, in, out) -> gradientDescentNesterov(
learningRate, momentum, in, out, velocity),
input, output);
}
/**
* Performs AdaGrad stochastic gradient descent optimization.
* Learning rate = 0.01.
*
* @param input input sets
* @param output output sets
* @return last cost
*/
public double stochasticGradientDescentAdagrad(T[] input, T[] output) {
return stochasticGradientDescentAdagrad(0.01, input, output);
}
/**
* Performs AdaGrad stochastic gradient descent optimization.
*
* @param learningRate learning rate
* @param input input sets
* @param output output sets
* @return last cost
*/
public double stochasticGradientDescentAdagrad(double learningRate,
T[] input, T[] output) {
final double[] gradientsMean =
new double[optimizable.getParameters().length];
return stochastic(
(i, in, out) -> gradientDescentAdagrad(
learningRate, in, out, gradientsMean),
input, output);
}
/**
* Performs RMSProp stochastic gradient descent optimization.
* Learning rate = 0.01.
* decay = 0.9.
*
* @param input input sets
* @param output output sets
* @return last cost
*/
public double stochasticGradientDescentRmsprop(T[] input, T[] output) {
return stochasticGradientDescentRmsprop(0.01, 0.9, input, output);
}
/**
* Performs RMSProp stochastic gradient descent optimization.
*
* @param learningRate learning rate
* @param decay decay
* @param input input sets
* @param output output sets
* @return last cost
*/
public double stochasticGradientDescentRmsprop(double learningRate,
double decay, T[] input, T[] output) {
final double[] gradientsMean =
new double[optimizable.getParameters().length];
return stochastic(
(i, in, out) -> gradientDescentRmsprop(
learningRate, decay, in, out, gradientsMean),
input, output);
}
/**
* Performs Adam stochastic gradient descent optimization.
* Learning rate = 0.001.
* Beta1 = 0.9.
* Beta2 = 0.999.
*
* @param input input sets
* @param output output sets
* @return last cost
*/
public double stochasticGradientDescentAdam(T[] input, T[] output) {
return stochasticGradientDescentAdam(1e-3, 0.9, 0.999, input, output);
}
/**
* Performs Adam stochastic gradient descent optimization.
*
* @param learningRate learning rate
* @param beta1 gradient average decay
* @param beta2 squared gradient average decay
* @param input input sets
* @param output output sets
* @return last cost
*/
public double stochasticGradientDescentAdam(double learningRate,
double beta1, double beta2, T[] input, T[] output) {
final double[] firstMoment =
new double[optimizable.getParameters().length];
final double[] secondMoment =
new double[optimizable.getParameters().length];
return stochastic(
(i, in, out) -> (gradientDescentAdam(learningRate, i,
beta1, beta2, in, out, firstMoment, secondMoment)),
input, output);
}
/**
* Iteration function interface used for splitting the loops & iteration
* functions.
*
* @param <T> input & output set type
*/
private interface IterationFunction<T> {
/**
* Single iteration that takes the current iteration, input & output
* sets and optimizes the optimizable and returns the cost after the
* iteration.
*
* @param iteration current iteration
* @param input input sets
* @param output output sets
* @return cost after iteration
*/
double apply(int iteration, T[] input, T[] output);
}
}