-
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathAvgPool1D.php
More file actions
302 lines (257 loc) · 7.83 KB
/
AvgPool1D.php
File metadata and controls
302 lines (257 loc) · 7.83 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
<?php
namespace Rubix\ML\NeuralNet\Layers;
use Tensor\Matrix;
use Rubix\ML\Deferred;
use Rubix\ML\NeuralNet\Optimizers\Optimizer;
use Rubix\ML\Exceptions\InvalidArgumentException;
use Rubix\ML\Exceptions\RuntimeException;
/**
* AvgPool1D
*
* A 1-dimensional average pooling layer that downsamples the input by computing
* the average value over sliding windows. It is commonly used after convolutional
* layers to reduce the sequence length while preserving average feature information.
*
* @category Machine Learning
* @package Rubix/ML
* @author Boorinio
*/
class AvgPool1D implements Hidden
{
/**
* The size of the pooling window.
*
* @var positive-int
*/
protected int $poolSize;
/**
* The length of the input sequence.
*
* @var positive-int
*/
protected int $inputLength;
/**
* The stride of the pooling operation.
*
* @var positive-int
*/
protected int $stride;
/**
* The number of input channels.
*
* @var positive-int|null
*/
protected ?int $inputChannels = null;
/**
* The computed output length.
*
* @var positive-int
*/
protected int $outputLength;
/**
* @param int $poolSize Size of the pooling window
* @param int $inputLength Length of the input sequence
* @param int $stride Stride of the pooling operation (default: same as poolSize)
* @throws InvalidArgumentException
*/
public function __construct(int $poolSize, int $inputLength, int $stride = 0)
{
if ($poolSize < 1) {
throw new InvalidArgumentException('Pool size must be'
. " greater than 0, $poolSize given.");
}
if ($inputLength < 1) {
throw new InvalidArgumentException('Input length must be'
. " greater than 0, $inputLength given.");
}
if ($stride < 0) {
throw new InvalidArgumentException('Stride cannot be'
. " negative, $stride given.");
}
$stride = $stride > 0 ? $stride : $poolSize;
$outputLength = (int) floor(($inputLength - $poolSize) / $stride) + 1;
if ($outputLength < 1) {
throw new InvalidArgumentException('Output length must be'
. " greater than 0, $outputLength given. Check pool size and stride values.");
}
$this->poolSize = $poolSize;
$this->inputLength = $inputLength;
$this->stride = $stride;
$this->outputLength = $outputLength;
}
/**
* Return the width of the layer (same as input channels).
*
* @internal
*
* @throws RuntimeException
* @return positive-int
*/
public function width() : int
{
if ($this->inputChannels === null) {
throw new RuntimeException('Layer has not been initialized.');
}
return $this->inputChannels;
}
/**
* Return the output length after pooling.
*
* @internal
*
* @return positive-int
*/
public function outputLength() : int
{
return $this->outputLength;
}
/**
* Initialize the layer with the fan in from the previous layer and return
* the fan out for this layer.
*
* @internal
*
* @param positive-int $fanIn
* @return positive-int
*/
public function initialize(int $fanIn) : int
{
$this->inputChannels = $fanIn;
return $fanIn * $this->outputLength;
}
/**
* Compute a forward pass through the layer.
*
* @internal
*
* @param Matrix $input
* @throws RuntimeException
* @return Matrix
*/
public function forward(Matrix $input) : Matrix
{
if ($this->inputChannels === null) {
throw new RuntimeException('Layer has not been initialized.');
}
$inputChannels = $input->m();
if ($inputChannels !== $this->inputChannels) {
throw new RuntimeException('Input channels mismatch:'
. " expected {$this->inputChannels}, got {$inputChannels}.");
}
$batchSize = (int) ($input->n() / $this->inputLength);
$inputArray = $input->asArray();
$output = [];
$scale = 1.0 / $this->poolSize;
foreach ($inputArray as $channel) {
$outputRow = [];
for ($b = 0; $b < $batchSize; ++$b) {
$sampleOffset = $b * $this->inputLength;
for ($t = 0; $t < $this->outputLength; ++$t) {
$startPos = $t * $this->stride;
$sum = 0.0;
for ($p = 0; $p < $this->poolSize; ++$p) {
$pos = $startPos + $p;
$sum += $channel[$sampleOffset + $pos];
}
$outputRow[] = $sum * $scale;
}
}
$output[] = $outputRow;
}
return Matrix::quick($output);
}
/**
* Compute an inference pass through the layer.
*
* @internal
*
* @param Matrix $input
* @throws RuntimeException
* @return Matrix
*/
public function infer(Matrix $input) : Matrix
{
return $this->forward($input);
}
/**
* Calculate the gradient and update the parameters of the layer.
*
* @internal
*
* @param Deferred $prevGradient
* @param Optimizer $optimizer
* @throws RuntimeException
* @return Deferred
*/
public function back(Deferred $prevGradient, Optimizer $optimizer) : Deferred
{
if ($this->inputChannels === null) {
throw new RuntimeException('Layer has not been initialized.');
}
$inputChannels = $this->inputChannels;
$inputLength = $this->inputLength;
$poolSize = $this->poolSize;
$stride = $this->stride;
$outputLength = $this->outputLength;
return new Deferred(
[$this, 'gradient'],
[$prevGradient, $inputChannels, $inputLength, $poolSize, $stride, $outputLength]
);
}
/**
* Calculate the gradient for the previous layer.
*
* @internal
*
* @param Deferred $prevGradient
* @param int $inputChannels
* @param int $inputLength
* @param int $poolSize
* @param int $stride
* @param int $outputLength
* @return Matrix
*/
public function gradient(
Deferred $prevGradient,
int $inputChannels,
int $inputLength,
int $poolSize,
int $stride,
int $outputLength
) : Matrix {
$dOut = $prevGradient();
$dOutArray = $dOut->asArray();
$batchSize = (int) (count($dOutArray[0]) / $outputLength);
// Initialize gradient with zeros
$dInput = array_fill(0, $inputChannels, array_fill(0, $inputLength * $batchSize, 0.0));
$scale = 1.0 / $poolSize;
// Distribute gradients evenly across pool window positions
foreach ($dOutArray as $c => $dOutRow) {
for ($b = 0; $b < $batchSize; ++$b) {
$sampleOffset = $b * $inputLength;
$outputOffset = $b * $outputLength;
for ($t = 0; $t < $outputLength; ++$t) {
$startPos = $t * $stride;
$grad = $dOutRow[$outputOffset + $t] * $scale;
for ($p = 0; $p < $poolSize; ++$p) {
$pos = $sampleOffset + $startPos + $p;
$dInput[$c][$pos] += $grad;
}
}
}
}
return Matrix::quick($dInput);
}
/**
* Return the string representation of the object.
*
* @internal
*
* @return string
*/
public function __toString() : string
{
return "AvgPool1D (pool size: {$this->poolSize}, input length: {$this->inputLength},"
. " stride: {$this->stride})";
}
}