-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathFeedForward.php
271 lines (228 loc) · 6 KB
/
FeedForward.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
<?php
namespace Rubix\ML\NeuralNet;
use Tensor\Matrix;
use Rubix\ML\Encoding;
use Rubix\ML\Datasets\Dataset;
use Rubix\ML\Datasets\Labeled;
use Rubix\ML\NeuralNet\Layers\Input;
use Rubix\ML\NeuralNet\Layers\Output;
use Rubix\ML\NeuralNet\Layers\Parametric;
use Rubix\ML\NeuralNet\Optimizers\Adaptive;
use Rubix\ML\NeuralNet\Optimizers\Optimizer;
use Traversable;
use function array_reverse;
/**
* Feed Forward
*
* A feed forward neural network implementation consisting of an input and
* output layer and any number of intermediate hidden layers.
*
* @internal
*
* @category Machine Learning
* @package Rubix/ML
* @author Andrew DalPino
*/
class FeedForward implements Network
{
/**
* The input layer to the network.
*
* @var Input
*/
protected Input $input;
/**
* The hidden layers of the network.
*
* @var list<Layers\Hidden>
*/
protected array $hidden = [
//
];
/**
* The pathing of the backward pass through the hidden layers.
*
* @var list<Layers\Hidden>
*/
protected array $backPass = [
//
];
/**
* The output layer of the network.
*
* @var Output
*/
protected Output $output;
/**
* The gradient descent optimizer used to train the network.
*
* @var Optimizer
*/
protected Optimizer $optimizer;
/**
* @param Input $input
* @param Layers\Hidden[] $hidden
* @param Output $output
* @param Optimizer $optimizer
*/
public function __construct(Input $input, array $hidden, Output $output, Optimizer $optimizer)
{
$hidden = array_values($hidden);
$backPass = array_reverse($hidden);
$this->input = $input;
$this->hidden = $hidden;
$this->output = $output;
$this->optimizer = $optimizer;
$this->backPass = $backPass;
}
/**
* Return the input layer.
*
* @return Input
*/
public function input() : Input
{
return $this->input;
}
/**
* Return an array of hidden layers indexed left to right.
*
* @return list<Layers\Hidden>
*/
public function hidden() : array
{
return $this->hidden;
}
/**
* Return the output layer.
*
* @return Output
*/
public function output() : Output
{
return $this->output;
}
/**
* Return all the layers in the network.
*
* @return Traversable<Layers\Layer>
*/
public function layers() : Traversable
{
yield $this->input;
yield from $this->hidden;
yield $this->output;
}
/**
* Return the number of trainable parameters in the network.
*
* @return int
*/
public function numParams() : int
{
$numParams = 0;
foreach ($this->layers() as $layer) {
if ($layer instanceof Parametric) {
foreach ($layer->parameters() as $parameter) {
$numParams += $parameter->param()->size();
}
}
}
return $numParams;
}
/**
* Initialize the parameters of the layers and warm the optimizer cache.
*/
public function initialize() : void
{
$fanIn = 1;
foreach ($this->layers() as $layer) {
$fanIn = $layer->initialize($fanIn);
}
if ($this->optimizer instanceof Adaptive) {
foreach ($this->layers() as $layer) {
if ($layer instanceof Parametric) {
foreach ($layer->parameters() as $param) {
$this->optimizer->warm($param);
}
}
}
}
}
/**
* Run an inference pass and return the activations at the output layer.
*
* @param Dataset $dataset
* @return Matrix
*/
public function infer(Dataset $dataset) : Matrix
{
$input = Matrix::quick($dataset->samples())->transpose();
foreach ($this->layers() as $layer) {
$input = $layer->infer($input);
}
return $input->transpose();
}
/**
* Perform a forward and backward pass of the network in one call. Returns
* the loss from the backward pass.
*
* @param Labeled $dataset
* @return float
*/
public function roundtrip(Labeled $dataset) : float
{
$input = Matrix::quick($dataset->samples())->transpose();
$this->feed($input);
$loss = $this->backpropagate($dataset->labels());
return $loss;
}
/**
* Feed a batch through the network and return a matrix of activations at the output later.
*
* @param Matrix $input
* @return Matrix
*/
public function feed(Matrix $input) : Matrix
{
foreach ($this->layers() as $layer) {
$input = $layer->forward($input);
}
return $input;
}
/**
* Backpropagate the gradient of the cost function and return the loss.
*
* @param list<string|int|float> $labels
* @return float
*/
public function backpropagate(array $labels) : float
{
[$gradient, $loss] = $this->output->back($labels, $this->optimizer);
foreach ($this->backPass as $layer) {
$gradient = $layer->back($gradient, $this->optimizer);
}
return $loss;
}
/**
* Export the network architecture as a graph in dot format.
*
* @return Encoding
*/
public function exportGraphviz() : Encoding
{
$dot = 'digraph Tree {' . PHP_EOL;
$dot .= ' node [shape=box, fontname=helvetica];' . PHP_EOL;
$layerNum = 0;
foreach ($this->layers() as $layer) {
++$layerNum;
$dot .= " N$layerNum [label=\"$layer\",style=\"rounded\"]" . PHP_EOL;
if ($layerNum > 1) {
$parentId = $layerNum - 1;
$dot .= " N{$parentId} -> N{$layerNum};" . PHP_EOL;
}
}
$dot .= '}';
return new Encoding($dot);
}
}