Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 16 | class Perceptron implements Classifier, PartialTrainer |
||
| 17 | { |
||
| 18 | use Predictable, PartiallyTrainable, OneVsRest { |
||
| 19 | reset as public resetOneVsRest; |
||
| 20 | PartiallyTrainable::train insteadof OneVsRest; |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var array |
||
| 25 | */ |
||
| 26 | protected $labels = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var \Phpml\Helper\Optimizer\Optimizer |
||
| 30 | */ |
||
| 31 | protected $optimizer; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | protected $featureCount = 0; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array |
||
| 40 | */ |
||
| 41 | protected $weights; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var float |
||
| 45 | */ |
||
| 46 | protected $learningRate; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | protected $maxIterations; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var Normalizer |
||
| 55 | */ |
||
| 56 | protected $normalizer; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | protected $enableEarlyStop = true; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $costValues = []; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Initalize a perceptron classifier with given learning rate and maximum |
||
| 70 | * number of iterations used while training the perceptron <br> |
||
| 71 | * |
||
| 72 | * Learning rate should be a float value between 0.0(exclusive) and 1.0(inclusive) <br> |
||
| 73 | * Maximum number of iterations can be an integer value greater than 0 |
||
| 74 | * @param int $learningRate |
||
| 75 | * @param int $maxIterations |
||
| 76 | */ |
||
| 77 | public function __construct(float $learningRate = 0.001, int $maxIterations = 1000, |
||
| 95 | |||
| 96 | public function partialTrain(array $samples, array $targets, array $labels = array()) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param array $samples |
||
| 102 | * @param array $targets |
||
| 103 | * @param array $labels |
||
| 104 | */ |
||
| 105 | public function trainBinary(array $samples, array $targets, array $labels) |
||
| 123 | |||
| 124 | public function resetTrainer() { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Normally enabling early stopping for the optimization procedure may |
||
| 135 | * help saving processing time while in some cases it may result in |
||
| 136 | * premature convergence.<br> |
||
| 137 | * |
||
| 138 | * If "false" is given, the optimization procedure will always be executed |
||
| 139 | * for $maxIterations times |
||
| 140 | * |
||
| 141 | * @param bool $enable |
||
| 142 | */ |
||
| 143 | public function setEarlyStop(bool $enable = true) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Returns the cost values obtained during the training. |
||
| 152 | * |
||
| 153 | * @return array |
||
| 154 | */ |
||
| 155 | public function getCostValues() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Trains the perceptron model with Stochastic Gradient Descent optimization |
||
| 162 | * to get the correct set of weights |
||
| 163 | * |
||
| 164 | * @param array $samples |
||
| 165 | * @param array $targets |
||
| 166 | */ |
||
| 167 | protected function runTraining(array $samples, array $targets) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Executes a Gradient Descent algorithm for |
||
| 185 | * the given cost function |
||
| 186 | * |
||
| 187 | * @param array $samples |
||
| 188 | * @param array $targets |
||
| 189 | */ |
||
| 190 | protected function runGradientDescent(array $samples, array $targets, \Closure $gradientFunc, bool $isBatch = false) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Checks if the sample should be normalized and if so, returns the |
||
| 208 | * normalized sample |
||
| 209 | * |
||
| 210 | * @param array $sample |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | protected function checkNormalizedSample(array $sample) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Calculates net output of the network as a float value for the given input |
||
| 227 | * |
||
| 228 | * @param array $sample |
||
| 229 | * @return int |
||
| 230 | */ |
||
| 231 | protected function output(array $sample) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Returns the class value (either -1 or 1) for the given input |
||
| 247 | * |
||
| 248 | * @param array $sample |
||
| 249 | * @return int |
||
| 250 | */ |
||
| 251 | protected function outputClass(array $sample) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns the probability of the sample of belonging to the given label. |
||
| 258 | * |
||
| 259 | * The probability is simply taken as the distance of the sample |
||
| 260 | * to the decision plane. |
||
| 261 | * |
||
| 262 | * @param array $sample |
||
| 263 | * @param mixed $label |
||
| 264 | */ |
||
| 265 | View Code Duplication | protected function predictProbability(array $sample, $label) |
|
| 276 | |||
| 277 | /** |
||
| 278 | * @param array $sample |
||
| 279 | * @return mixed |
||
| 280 | */ |
||
| 281 | protected function predictSampleBinary(array $sample) |
||
| 289 | } |
||
| 290 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..