/
MultilayerPerceptron.php
593 lines (501 loc) · 16 KB
/
MultilayerPerceptron.php
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
<?php
namespace Rubix\ML\Classifiers;
use Rubix\ML\Online;
use Rubix\ML\Learner;
use Rubix\ML\Verbose;
use Rubix\ML\DataType;
use Rubix\ML\Encoding;
use Rubix\ML\Estimator;
use Rubix\ML\Persistable;
use Rubix\ML\Probabilistic;
use Rubix\ML\EstimatorType;
use Rubix\ML\Helpers\Params;
use Rubix\ML\Datasets\Dataset;
use Rubix\ML\Traits\LoggerAware;
use Rubix\ML\NeuralNet\Snapshot;
use Rubix\ML\NeuralNet\FeedForward;
use Rubix\ML\NeuralNet\Layers\Dense;
use Rubix\ML\NeuralNet\Layers\Hidden;
use Rubix\ML\Traits\AutotrackRevisions;
use Rubix\ML\NeuralNet\Optimizers\Adam;
use Rubix\ML\NeuralNet\Layers\Multiclass;
use Rubix\ML\CrossValidation\Metrics\FBeta;
use Rubix\ML\NeuralNet\Layers\Placeholder1D;
use Rubix\ML\NeuralNet\Optimizers\Optimizer;
use Rubix\ML\NeuralNet\Initializers\Xavier1;
use Rubix\ML\CrossValidation\Metrics\Metric;
use Rubix\ML\Specifications\DatasetIsLabeled;
use Rubix\ML\Specifications\DatasetIsNotEmpty;
use Rubix\ML\Specifications\SpecificationChain;
use Rubix\ML\NeuralNet\CostFunctions\CrossEntropy;
use Rubix\ML\Specifications\DatasetHasDimensionality;
use Rubix\ML\NeuralNet\CostFunctions\ClassificationLoss;
use Rubix\ML\Specifications\LabelsAreCompatibleWithLearner;
use Rubix\ML\Specifications\EstimatorIsCompatibleWithMetric;
use Rubix\ML\Specifications\SamplesAreCompatibleWithEstimator;
use Rubix\ML\Exceptions\InvalidArgumentException;
use Rubix\ML\Exceptions\RuntimeException;
use Generator;
use function is_nan;
use function count;
use function get_object_vars;
use function number_format;
/**
* Multilayer Perceptron
*
* A multiclass feed forward neural network classifier with user-defined hidden layers. The
* Multilayer Perceptron is a deep learning model capable of forming higher-order feature
* representations through layers of computation. In addition, the MLP features progress
* monitoring which stops training when it can no longer make progress. It utilizes network
* snapshotting to make sure that it always has the best model parameters even if progress
* declined during training.
*
* References:
* [1] G. E. Hinton. (1989). Connectionist learning procedures.
* [2] L. Prechelt. (1997). Early Stopping - but when?
*
* @category Machine Learning
* @package Rubix/ML
* @author Andrew DalPino
*/
class MultilayerPerceptron implements Estimator, Learner, Online, Probabilistic, Verbose, Persistable
{
use AutotrackRevisions, LoggerAware;
/**
* An array composing the user-specified hidden layers of the network in order.
*
* @var \Rubix\ML\NeuralNet\Layers\Hidden[]
*/
protected array $hiddenLayers;
/**
* The number of training samples to process at a time.
*
* @var positive-int
*/
protected int $batchSize;
/**
* The gradient descent optimizer used to update the network parameters.
*
* @var Optimizer
*/
protected Optimizer $optimizer;
/**
* The amount of L2 regularization applied to the weights of the output layer.
*
* @var float
*/
protected float $l2Penalty;
/**
* The maximum number of training epochs. i.e. the number of times to iterate before terminating.
*
* @var int<0,max>
*/
protected int $epochs;
/**
* The minimum change in the training loss necessary to continue training.
*
* @var float
*/
protected float $minChange;
/**
* The number of epochs without improvement in the validation score to wait before considering an early stop.
*
* @var positive-int
*/
protected int $window;
/**
* The proportion of training samples to use for validation and progress monitoring.
*
* @var float
*/
protected float $holdOut;
/**
* The function that computes the loss associated with an erroneous activation during training.
*
* @var ClassificationLoss
*/
protected ClassificationLoss $costFn;
/**
* The validation metric used to score the generalization performance of the model during training.
*
* @var Metric
*/
protected Metric $metric;
/**
* The underlying neural network instance.
*
* @var \Rubix\ML\NeuralNet\FeedForward|null
*/
protected ?FeedForward $network = null;
/**
* The unique class labels.
*
* @var string[]|null
*/
protected ?array $classes = null;
/**
* The validation scores at each epoch from the last training session.
*
* @var float[]|null
*/
protected ?array $scores = null;
/**
* The loss at each epoch from the last training session.
*
* @var float[]|null
*/
protected ?array $losses = null;
/**
* @param \Rubix\ML\NeuralNet\Layers\Hidden[] $hiddenLayers
* @param int $batchSize
* @param \Rubix\ML\NeuralNet\Optimizers\Optimizer|null $optimizer
* @param float $l2Penalty
* @param int $epochs
* @param float $minChange
* @param int $window
* @param float $holdOut
* @param \Rubix\ML\NeuralNet\CostFunctions\ClassificationLoss|null $costFn
* @param \Rubix\ML\CrossValidation\Metrics\Metric|null $metric
* @throws InvalidArgumentException
*/
public function __construct(
array $hiddenLayers = [],
int $batchSize = 128,
?Optimizer $optimizer = null,
float $l2Penalty = 1e-4,
int $epochs = 1000,
float $minChange = 1e-4,
int $window = 5,
float $holdOut = 0.1,
?ClassificationLoss $costFn = null,
?Metric $metric = null
) {
foreach ($hiddenLayers as $layer) {
if (!$layer instanceof Hidden) {
throw new InvalidArgumentException('Hidden layer'
. ' must implement the Hidden interface.');
}
}
if ($batchSize < 1) {
throw new InvalidArgumentException('Batch size must be'
. " greater than 0, $batchSize given.");
}
if ($l2Penalty < 0.0) {
throw new InvalidArgumentException('L2 Penalty must be'
. " greater than 0, $l2Penalty given.");
}
if ($epochs < 0) {
throw new InvalidArgumentException('Number of epochs'
. " must be greater than 0, $epochs given.");
}
if ($minChange < 0.0) {
throw new InvalidArgumentException('Minimum change must be'
. " greater than 0, $minChange given.");
}
if ($window < 1) {
throw new InvalidArgumentException('Window must be'
. " greater than 0, $window given.");
}
if ($holdOut < 0.0 or $holdOut > 0.5) {
throw new InvalidArgumentException('Hold out ratio must be'
. " between 0 and 0.5, $holdOut given.");
}
if ($metric) {
EstimatorIsCompatibleWithMetric::with($this, $metric)->check();
}
$this->hiddenLayers = $hiddenLayers;
$this->batchSize = $batchSize;
$this->optimizer = $optimizer ?? new Adam();
$this->l2Penalty = $l2Penalty;
$this->epochs = $epochs;
$this->minChange = $minChange;
$this->window = $window;
$this->holdOut = $holdOut;
$this->costFn = $costFn ?? new CrossEntropy();
$this->metric = $metric ?? new FBeta();
}
/**
* Return the estimator type.
*
* @internal
*
* @return EstimatorType
*/
public function type() : EstimatorType
{
return EstimatorType::classifier();
}
/**
* Return the data types that the estimator is compatible with.
*
* @internal
*
* @return list<\Rubix\ML\DataType>
*/
public function compatibility() : array
{
return [
DataType::continuous(),
];
}
/**
* Return the settings of the hyper-parameters in an associative array.
*
* @internal
*
* @return mixed[]
*/
public function params() : array
{
return [
'hidden layers' => $this->hiddenLayers,
'batch size' => $this->batchSize,
'optimizer' => $this->optimizer,
'l2 penalty' => $this->l2Penalty,
'epochs' => $this->epochs,
'min change' => $this->minChange,
'window' => $this->window,
'hold out' => $this->holdOut,
'cost fn' => $this->costFn,
'metric' => $this->metric,
];
}
/**
* Has the learner been trained?
*
* @return bool
*/
public function trained() : bool
{
return $this->network and $this->classes;
}
/**
* Return an iterable progress table with the steps from the last training session.
*
* @return \Generator<mixed[]>
*/
public function steps() : Generator
{
if (!$this->losses) {
return;
}
foreach ($this->losses as $epoch => $loss) {
yield [
'epoch' => $epoch,
'score' => $this->scores[$epoch] ?? null,
'loss' => $loss,
];
}
}
/**
* Return the validation score for each epoch from the last training session.
*
* @return float[]|null
*/
public function scores() : ?array
{
return $this->scores;
}
/**
* Return the loss for each epoch from the last training session.
*
* @return float[]|null
*/
public function losses() : ?array
{
return $this->losses;
}
/**
* Return the underlying neural network instance or null if not trained.
*
* @return \Rubix\ML\NeuralNet\FeedForward|null
*/
public function network() : ?FeedForward
{
return $this->network;
}
/**
* Train the learner with a dataset.
*
* @param \Rubix\ML\Datasets\Labeled $dataset
*/
public function train(Dataset $dataset) : void
{
SpecificationChain::with([
new DatasetIsLabeled($dataset),
new DatasetIsNotEmpty($dataset),
new LabelsAreCompatibleWithLearner($dataset, $this),
])->check();
$classes = $dataset->possibleOutcomes();
$hiddenLayers = $this->hiddenLayers;
$hiddenLayers[] = new Dense(count($classes), $this->l2Penalty, true, new Xavier1());
$this->network = new FeedForward(
new Placeholder1D($dataset->numFeatures()),
$hiddenLayers,
new Multiclass($classes, $this->costFn),
$this->optimizer
);
$this->network->initialize();
$this->classes = $classes;
$this->partial($dataset);
}
/**
* Train the network using mini-batch gradient descent with backpropagation.
*
* @param \Rubix\ML\Datasets\Labeled $dataset
*/
public function partial(Dataset $dataset) : void
{
if (!$this->network) {
$this->train($dataset);
return;
}
SpecificationChain::with([
new DatasetIsLabeled($dataset),
new DatasetIsNotEmpty($dataset),
new SamplesAreCompatibleWithEstimator($dataset, $this),
new LabelsAreCompatibleWithLearner($dataset, $this),
new DatasetHasDimensionality($dataset, $this->network->input()->width()),
])->check();
if ($this->logger) {
$this->logger->info("Training $this");
$numParams = number_format($this->network->numParams());
$this->logger->info("{$numParams} trainable parameters");
}
[$testing, $training] = $dataset->stratifiedSplit($this->holdOut);
[$minScore, $maxScore] = $this->metric->range()->list();
$bestScore = $minScore;
$bestEpoch = $numWorseEpochs = 0;
$loss = 0.0;
$snapshot = null;
$prevLoss = INF;
if ($testing->empty() and $this->logger) {
$this->logger->notice('Insufficient validation data, '
. 'some features are disabled');
}
$this->scores = $this->losses = [];
for ($epoch = 1; $epoch <= $this->epochs; ++$epoch) {
$batches = $training->randomize()->batch($this->batchSize);
$loss = 0.0;
foreach ($batches as $batch) {
$loss += $this->network->roundtrip($batch);
}
$loss /= count($batches);
$lossChange = abs($prevLoss - $loss);
$this->losses[$epoch] = $loss;
if (is_nan($loss)) {
if ($this->logger) {
$this->logger->warning('Numerical instability detected');
}
break;
}
if (!$testing->empty()) {
$predictions = $this->predict($testing);
$score = $this->metric->score($predictions, $testing->labels());
$this->scores[$epoch] = $score;
}
if ($this->logger) {
$lossDirection = $loss < $prevLoss ? '↓' : '↑';
$message = "Epoch: $epoch, "
. "{$this->costFn}: $loss, "
. "Loss Change: {$lossDirection}{$lossChange}, "
. "{$this->metric}: " . ($score ?? 'N/A');
$this->logger->info($message);
}
if (isset($score)) {
if ($score >= $maxScore) {
break;
}
if ($score > $bestScore) {
$bestScore = $score;
$bestEpoch = $epoch;
$snapshot = Snapshot::take($this->network);
$numWorseEpochs = 0;
} else {
++$numWorseEpochs;
}
if ($numWorseEpochs >= $this->window) {
break;
}
}
if ($lossChange < $this->minChange) {
break;
}
$prevLoss = $loss;
}
if ($snapshot and (end($this->scores) < $bestScore or is_nan($loss))) {
$snapshot->restore();
if ($this->logger) {
$this->logger->info("Model state restored to epoch $bestEpoch");
}
}
if ($this->logger) {
$this->logger->info('Training complete');
}
}
/**
* Make predictions from a dataset.
*
* @param Dataset $dataset
* @return list<string>
*/
public function predict(Dataset $dataset) : array
{
return array_map('Rubix\ML\argmax', $this->proba($dataset));
}
/**
* Estimate the joint probabilities for each possible outcome.
*
* @param Dataset $dataset
* @throws RuntimeException
* @return list<array<string,float>>
*/
public function proba(Dataset $dataset) : array
{
if (!$this->network or !$this->classes) {
throw new RuntimeException('Estimator has not been trained.');
}
DatasetHasDimensionality::with($dataset, $this->network->input()->width())->check();
$activations = $this->network->infer($dataset);
$probabilities = [];
foreach ($activations->asArray() as $dist) {
$probabilities[] = array_combine($this->classes, $dist) ?: [];
}
return $probabilities;
}
/**
* Export the network architecture as a graph in dot format.
*
* @throws RuntimeException
* @return Encoding
*/
public function exportGraphviz() : Encoding
{
if (!$this->network) {
throw new RuntimeException('Must train network first.');
}
return $this->network->exportGraphviz();
}
/**
* Return an associative array containing the data used to serialize the object.
*
* @return mixed[]
*/
public function __serialize() : array
{
$properties = get_object_vars($this);
unset($properties['losses'], $properties['scores']);
return $properties;
}
/**
* Return the string representation of the object.
*
* @internal
*
* @return string
*/
public function __toString() : string
{
return 'Multilayer Perceptron (' . Params::stringify($this->params()) . ')';
}
}