Skip to content

Commit 14eed45

Browse files
committed
Forego unnecessary logistic computation in Logit Boost
1 parent 359cc24 commit 14eed45

12 files changed

+21
-20
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
- 1.3.3
2+
- Forego unnecessary logistic computation in Logit Boost
3+
14
- 1.3.2
25
- Optimize Binary output layer
36

src/Classifiers/ExtraTreeClassifier.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,7 @@ public function proba(Dataset $dataset) : array
213213
}
214214

215215
/**
216-
* Terminate the branch by selecting the class outcome with the highest
217-
* probability.
216+
* Terminate the branch by selecting the class outcome with the highest probability.
218217
*
219218
* @param \Rubix\ML\Datasets\Labeled $dataset
220219
* @return \Rubix\ML\Graph\Nodes\Best
@@ -235,9 +234,9 @@ protected function terminate(Labeled $dataset) : Best
235234

236235
$p = $counts[$outcome] / $n;
237236

238-
$impurity = -($p * log($p));
237+
$entropy = -($p * log($p));
239238

240-
return new Best($outcome, $probabilities, $impurity, $n);
239+
return new Best($outcome, $probabilities, $entropy, $n);
241240
}
242241

243242
/**

src/Classifiers/LogisticRegression.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class LogisticRegression implements Estimator, Learner, Online, Probabilistic, R
5757
/**
5858
* The number of training samples to process at a time.
5959
*
60-
* @var int
60+
* @var positive-int
6161
*/
6262
protected int $batchSize;
6363

src/Classifiers/LogitBoost.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ public function train(Dataset $dataset) : void
427427
$zHat = $booster->predict($training);
428428

429429
$z = array_map([$this, 'updateZ'], $zHat, $z);
430-
431430
$out = array_map('Rubix\ML\sigmoid', $z);
432431

433432
$this->losses[$epoch] = $loss;
@@ -439,12 +438,10 @@ public function train(Dataset $dataset) : void
439438

440439
$zTest = array_map([$this, 'updateZ'], $zHat, $zTest);
441440

442-
$outTest = array_map('Rubix\ML\sigmoid', $zTest);
443-
444441
$predictions = [];
445442

446-
foreach ($outTest as $probability) {
447-
$predictions[] = $probability < 0.5 ? $classes[0] : $classes[1];
443+
foreach ($zTest as $value) {
444+
$predictions[] = $value < 0.0 ? $classes[0] : $classes[1];
448445
}
449446

450447
$score = $this->metric->score($predictions, $testing->labels());

src/Classifiers/MultilayerPerceptron.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class MultilayerPerceptron implements Estimator, Learner, Online, Probabilistic,
7474
/**
7575
* The number of training samples to process at a time.
7676
*
77-
* @var int
77+
* @var positive-int
7878
*/
7979
protected int $batchSize;
8080

src/Classifiers/SoftmaxClassifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class SoftmaxClassifier implements Estimator, Learner, Online, Probabilistic, Ve
5454
/**
5555
* The number of training samples to process at a time.
5656
*
57-
* @var int
57+
* @var positive-int
5858
*/
5959
protected int $batchSize;
6060

src/Clusterers/KMeans.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class KMeans implements Estimator, Learner, Online, Probabilistic, Verbose, Pers
6666
/**
6767
* The size of each mini batch in samples.
6868
*
69-
* @var int
69+
* @var positive-int
7070
*/
7171
protected int $batchSize;
7272

src/Datasets/Labeled.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ public function stratifiedFold(int $k = 10) : array
570570
* not enough samples to fill an entire batch, then the dataset will contain
571571
* as many samples and labels as possible.
572572
*
573-
* @param int $n
573+
* @param positive-int $n
574574
* @return list<self>
575575
*/
576576
public function batch(int $n = 50) : array
@@ -742,7 +742,8 @@ public function randomWeightedSubsetWithReplacement(int $n, array $weights) : se
742742
. ' but ' . count($weights) . ' given.');
743743
}
744744

745-
$numLevels = (int) round(sqrt(count($weights)));
745+
/** @var positive-int $numLevels */
746+
$numLevels = (int) round(sqrt(count($weights))) ?: 1;
746747

747748
$levels = array_chunk($weights, $numLevels, true);
748749

src/Datasets/Unlabeled.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ public function fold(int $k = 3) : array
303303
* not enough samples to fill an entire batch, then the dataset will contain
304304
* as many samples as possible.
305305
*
306-
* @param int $n
306+
* @param positive-int $n
307307
* @return list<self>
308308
*/
309309
public function batch(int $n = 50) : array
@@ -453,6 +453,7 @@ public function randomWeightedSubsetWithReplacement(int $n, array $weights) : se
453453
. ' but ' . count($weights) . ' given.');
454454
}
455455

456+
/** @var positive-int $numLevels */
456457
$numLevels = (int) round(sqrt(count($weights)));
457458

458459
$levels = array_chunk($weights, $numLevels, true);

src/Regressors/Adaline.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Adaline implements Estimator, Learner, Online, RanksFeatures, Verbose, Per
5858
/**
5959
* The number of training samples to process at a time.
6060
*
61-
* @var int
61+
* @var positive-int
6262
*/
6363
protected int $batchSize;
6464

src/Regressors/MLPRegressor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class MLPRegressor implements Estimator, Learner, Online, Verbose, Persistable
7373
/**
7474
* The number of training samples to process at a time.
7575
*
76-
* @var int
76+
* @var positive-int
7777
*/
7878
protected int $batchSize;
7979

tests/Classifiers/LogitBoostTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class LogitBoostTest extends TestCase
7676
protected function setUp() : void
7777
{
7878
$this->generator = new Agglomerate([
79-
'male' => new Blob([69.2, 195.7, 40.0], [2.0, 6.0, 0.6]),
80-
'female' => new Blob([63.7, 168.5, 38.1], [1.6, 5.0, 0.8]),
79+
'male' => new Blob([69.2, 195.7, 40.0], [2.0, 6.4, 0.6]),
80+
'female' => new Blob([63.7, 168.5, 38.1], [1.8, 5.0, 0.8]),
8181
]);
8282

8383
$this->estimator = new LogitBoost(new RegressionTree(3), 0.1, 0.5, 1000, 1e-4, 5, 0.1, new FBeta());

0 commit comments

Comments
 (0)