1 | <?php |
||
18 | class Perceptron implements Classifier, IncrementalEstimator |
||
19 | { |
||
20 | use Predictable, OneVsRest; |
||
21 | |||
22 | /** |
||
23 | * @var Optimizer|GD|StochasticGD|null |
||
24 | */ |
||
25 | protected $optimizer; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | protected $labels = []; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $featureCount = 0; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $weights = []; |
||
41 | |||
42 | /** |
||
43 | * @var float |
||
44 | */ |
||
45 | protected $learningRate; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $maxIterations; |
||
51 | |||
52 | /** |
||
53 | * @var Normalizer |
||
54 | */ |
||
55 | protected $normalizer; |
||
56 | |||
57 | /** |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $enableEarlyStop = true; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $costValues = []; |
||
66 | |||
67 | /** |
||
68 | * Initalize a perceptron classifier with given learning rate and maximum |
||
69 | * number of iterations used while training the perceptron |
||
70 | * |
||
71 | * @param float $learningRate Value between 0.0(exclusive) and 1.0(inclusive) |
||
72 | * @param int $maxIterations Must be at least 1 |
||
73 | * |
||
74 | * @throws InvalidArgumentException |
||
75 | */ |
||
76 | public function __construct(float $learningRate = 0.001, int $maxIterations = 1000, bool $normalizeInputs = true) |
||
93 | |||
94 | public function partialTrain(array $samples, array $targets, array $labels = []): void |
||
98 | |||
99 | public function trainBinary(array $samples, array $targets, array $labels): void |
||
119 | |||
120 | /** |
||
121 | * Normally enabling early stopping for the optimization procedure may |
||
122 | * help saving processing time while in some cases it may result in |
||
123 | * premature convergence.<br> |
||
124 | * |
||
125 | * If "false" is given, the optimization procedure will always be executed |
||
126 | * for $maxIterations times |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function setEarlyStop(bool $enable = true) |
||
136 | |||
137 | /** |
||
138 | * Returns the cost values obtained during the training. |
||
139 | */ |
||
140 | public function getCostValues(): array |
||
144 | |||
145 | protected function resetBinary(): void |
||
153 | |||
154 | /** |
||
155 | * Trains the perceptron model with Stochastic Gradient Descent optimization |
||
156 | * to get the correct set of weights |
||
157 | */ |
||
158 | protected function runTraining(array $samples, array $targets) |
||
173 | |||
174 | /** |
||
175 | * Executes a Gradient Descent algorithm for |
||
176 | * the given cost function |
||
177 | */ |
||
178 | protected function runGradientDescent(array $samples, array $targets, Closure $gradientFunc, bool $isBatch = false) |
||
193 | |||
194 | /** |
||
195 | * Checks if the sample should be normalized and if so, returns the |
||
196 | * normalized sample |
||
197 | */ |
||
198 | protected function checkNormalizedSample(array $sample): array |
||
208 | |||
209 | /** |
||
210 | * Calculates net output of the network as a float value for the given input |
||
211 | * |
||
212 | * @return int|float |
||
213 | */ |
||
214 | protected function output(array $sample) |
||
227 | |||
228 | /** |
||
229 | * Returns the class value (either -1 or 1) for the given input |
||
230 | */ |
||
231 | protected function outputClass(array $sample): int |
||
235 | |||
236 | /** |
||
237 | * Returns the probability of the sample of belonging to the given label. |
||
238 | * |
||
239 | * The probability is simply taken as the distance of the sample |
||
240 | * to the decision plane. |
||
241 | * |
||
242 | * @param mixed $label |
||
243 | */ |
||
244 | protected function predictProbability(array $sample, $label): float |
||
256 | |||
257 | /** |
||
258 | * @return mixed |
||
259 | */ |
||
260 | protected function predictSampleBinary(array $sample) |
||
268 | } |
||
269 |