1 | <?php |
||
15 | class StochasticGD extends Optimizer |
||
16 | { |
||
17 | /** |
||
18 | * A (samples) |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $samples = []; |
||
23 | |||
24 | /** |
||
25 | * y (targets) |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $targets = []; |
||
30 | |||
31 | /** |
||
32 | * Callback function to get the gradient and cost value |
||
33 | * for a specific set of theta (ϴ) and a pair of sample & target |
||
34 | * |
||
35 | * @var \Closure|null |
||
36 | */ |
||
37 | protected $gradientCb = null; |
||
38 | |||
39 | /** |
||
40 | * Maximum number of iterations used to train the model |
||
41 | * |
||
42 | * @var int |
||
43 | */ |
||
44 | protected $maxIterations = 1000; |
||
45 | |||
46 | /** |
||
47 | * Learning rate is used to control the speed of the optimization.<br> |
||
48 | * |
||
49 | * Larger values of lr may overshoot the optimum or even cause divergence |
||
50 | * while small values slows down the convergence and increases the time |
||
51 | * required for the training |
||
52 | * |
||
53 | * @var float |
||
54 | */ |
||
55 | protected $learningRate = 0.001; |
||
56 | |||
57 | /** |
||
58 | * Minimum amount of change in the weights and error values |
||
59 | * between iterations that needs to be obtained to continue the training |
||
60 | * |
||
61 | * @var float |
||
62 | */ |
||
63 | protected $threshold = 1e-4; |
||
64 | |||
65 | /** |
||
66 | * Enable/Disable early stopping by checking the weight & cost values |
||
67 | * to see whether they changed large enough to continue the optimization |
||
68 | * |
||
69 | * @var bool |
||
70 | */ |
||
71 | protected $enableEarlyStop = true; |
||
72 | |||
73 | /** |
||
74 | * List of values obtained by evaluating the cost function at each iteration |
||
75 | * of the algorithm |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $costValues = []; |
||
80 | |||
81 | /** |
||
82 | * Initializes the SGD optimizer for the given number of dimensions |
||
83 | */ |
||
84 | public function __construct(int $dimensions) |
||
91 | |||
92 | public function setInitialTheta(array $theta) |
||
102 | |||
103 | /** |
||
104 | * Sets minimum value for the change in the theta values |
||
105 | * between iterations to continue the iterations.<br> |
||
106 | * |
||
107 | * If change in the theta is less than given value then the |
||
108 | * algorithm will stop training |
||
109 | * |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function setChangeThreshold(float $threshold = 1e-5) |
||
118 | |||
119 | /** |
||
120 | * Enable/Disable early stopping by checking at each iteration |
||
121 | * whether changes in theta or cost value are not large enough |
||
122 | * |
||
123 | * @return $this |
||
124 | */ |
||
125 | public function setEarlyStop(bool $enable = true) |
||
131 | |||
132 | /** |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function setLearningRate(float $learningRate) |
||
141 | |||
142 | /** |
||
143 | * @return $this |
||
144 | */ |
||
145 | public function setMaxIterations(int $maxIterations) |
||
151 | |||
152 | /** |
||
153 | * Optimization procedure finds the unknow variables for the equation A.ϴ = y |
||
154 | * for the given samples (A) and targets (y).<br> |
||
155 | * |
||
156 | * The cost function to minimize and the gradient of the function are to be |
||
157 | * handled by the callback function provided as the third parameter of the method. |
||
158 | */ |
||
159 | public function runOptimization(array $samples, array $targets, Closure $gradientCb): ?array |
||
198 | |||
199 | /** |
||
200 | * Returns the list of cost values for each iteration executed in |
||
201 | * last run of the optimization |
||
202 | */ |
||
203 | public function getCostValues(): array |
||
207 | |||
208 | protected function updateTheta(): float |
||
235 | |||
236 | /** |
||
237 | * Checks if the optimization is not effective enough and can be stopped |
||
238 | * in case large enough changes in the solution do not happen |
||
239 | */ |
||
240 | protected function earlyStop(array $oldTheta): bool |
||
263 | |||
264 | /** |
||
265 | * Clears the optimizer internal vars after the optimization process. |
||
266 | */ |
||
267 | protected function clear(): void |
||
273 | } |
||
274 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.