1 | <?php |
||
12 | class StochasticGD extends Optimizer |
||
13 | { |
||
14 | /** |
||
15 | * A (samples) |
||
16 | * |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $samples; |
||
20 | |||
21 | /** |
||
22 | * y (targets) |
||
23 | * |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $targets; |
||
27 | |||
28 | /** |
||
29 | * Callback function to get the gradient and cost value |
||
30 | * for a specific set of theta (ϴ) and a pair of sample & target |
||
31 | * |
||
32 | * @var \Closure |
||
33 | */ |
||
34 | protected $gradientCb; |
||
35 | |||
36 | /** |
||
37 | * Maximum number of iterations used to train the model |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $maxIterations = 1000; |
||
42 | |||
43 | /** |
||
44 | * Learning rate is used to control the speed of the optimization.<br> |
||
45 | * |
||
46 | * Larger values of lr may overshoot the optimum or even cause divergence |
||
47 | * while small values slows down the convergence and increases the time |
||
48 | * required for the training |
||
49 | * |
||
50 | * @var float |
||
51 | */ |
||
52 | protected $learningRate = 0.001; |
||
53 | |||
54 | /** |
||
55 | * Minimum amount of change in the weights and error values |
||
56 | * between iterations that needs to be obtained to continue the training |
||
57 | * |
||
58 | * @var float |
||
59 | */ |
||
60 | protected $threshold = 1e-4; |
||
61 | |||
62 | /** |
||
63 | * Enable/Disable early stopping by checking the weight & cost values |
||
64 | * to see whether they changed large enough to continue the optimization |
||
65 | * |
||
66 | * @var bool |
||
67 | */ |
||
68 | protected $enableEarlyStop = true; |
||
69 | /** |
||
70 | * List of values obtained by evaluating the cost function at each iteration |
||
71 | * of the algorithm |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $costValues= []; |
||
76 | |||
77 | /** |
||
78 | * Initializes the SGD optimizer for the given number of dimensions |
||
79 | * |
||
80 | * @param int $dimensions |
||
81 | */ |
||
82 | public function __construct(int $dimensions) |
||
89 | |||
90 | /** |
||
91 | * Sets minimum value for the change in the theta values |
||
92 | * between iterations to continue the iterations.<br> |
||
93 | * |
||
94 | * If change in the theta is less than given value then the |
||
95 | * algorithm will stop training |
||
96 | * |
||
97 | * @param float $threshold |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function setChangeThreshold(float $threshold = 1e-5) |
||
107 | |||
108 | /** |
||
109 | * Enable/Disable early stopping by checking at each iteration |
||
110 | * whether changes in theta or cost value are not large enough |
||
111 | * |
||
112 | * @param bool $enable |
||
113 | * |
||
114 | * @return $this |
||
115 | */ |
||
116 | public function setEarlyStop(bool $enable = true) |
||
122 | |||
123 | /** |
||
124 | * @param float $learningRate |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function setLearningRate(float $learningRate) |
||
134 | |||
135 | /** |
||
136 | * @param int $maxIterations |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function setMaxIterations(int $maxIterations) |
||
146 | |||
147 | /** |
||
148 | * Optimization procedure finds the unknow variables for the equation A.ϴ = y |
||
149 | * for the given samples (A) and targets (y).<br> |
||
150 | * |
||
151 | * The cost function to minimize and the gradient of the function are to be |
||
152 | * handled by the callback function provided as the third parameter of the method. |
||
153 | * |
||
154 | * @param array $samples |
||
155 | * @param array $targets |
||
156 | * @param \Closure $gradientCb |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | public function runOptimization(array $samples, array $targets, \Closure $gradientCb) |
||
199 | |||
200 | /** |
||
201 | * @return float |
||
202 | */ |
||
203 | protected function updateTheta() |
||
230 | |||
231 | /** |
||
232 | * Checks if the optimization is not effective enough and can be stopped |
||
233 | * in case large enough changes in the solution do not happen |
||
234 | * |
||
235 | * @param array $oldTheta |
||
236 | * |
||
237 | * @return boolean |
||
238 | */ |
||
239 | protected function earlyStop($oldTheta) |
||
260 | |||
261 | /** |
||
262 | * Returns the list of cost values for each iteration executed in |
||
263 | * last run of the optimization |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | public function getCostValues() |
||
271 | } |
||
272 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.