Skip to content

Commit

Permalink
Add Optimizer tests and remove initialTheta (#252)
Browse files Browse the repository at this point in the history
* Add  Optimizer tests

* Remove Optimizer.initialTheta and rename Optimizer.setInitialTheta to setTheta
  • Loading branch information
marmichalski authored and akondas committed Mar 4, 2018
1 parent 55749c7 commit a40c50b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
6 changes: 1 addition & 5 deletions src/Helper/Optimizer/Optimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

abstract class Optimizer
{
public $initialTheta;

/**
* Unknown variables to be found
*
Expand All @@ -37,11 +35,9 @@ public function __construct(int $dimensions)
for ($i = 0; $i < $this->dimensions; ++$i) {
$this->theta[] = (random_int(0, PHP_INT_MAX) / PHP_INT_MAX) + 0.1;
}

$this->initialTheta = $this->theta;
}

public function setInitialTheta(array $theta)
public function setTheta(array $theta)
{
if (count($theta) != $this->dimensions) {
throw new InvalidArgumentException(sprintf('Number of values in the weights array should be %s', $this->dimensions));
Expand Down
2 changes: 1 addition & 1 deletion src/Helper/Optimizer/StochasticGD.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function __construct(int $dimensions)
$this->dimensions = $dimensions;
}

public function setInitialTheta(array $theta)
public function setTheta(array $theta)
{
if (count($theta) != $this->dimensions + 1) {
throw new InvalidArgumentException(sprintf('Number of values in the weights array should be %s', $this->dimensions + 1));
Expand Down
4 changes: 2 additions & 2 deletions tests/Helper/Optimizer/ConjugateGradientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testRunOptimizationWithCustomInitialTheta(): void

$optimizer = new ConjugateGradient(1);
// set very weak theta to trigger very bad result
$optimizer->setInitialTheta([0.0000001, 0.0000001]);
$optimizer->setTheta([0.0000001, 0.0000001]);

$theta = $optimizer->runOptimization($samples, $targets, $callback);

Expand Down Expand Up @@ -97,6 +97,6 @@ public function testThrowExceptionOnInvalidTheta(): void
$opimizer = new ConjugateGradient(2);

$this->expectException(InvalidArgumentException::class);
$opimizer->setInitialTheta([0.15]);
$opimizer->setTheta([0.15]);
}
}
34 changes: 34 additions & 0 deletions tests/Helper/Optimizer/OptimizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Phpml\Tests\Helper\Optimizer;

use Phpml\Exception\InvalidArgumentException;
use Phpml\Helper\Optimizer\Optimizer;
use PHPUnit\Framework\TestCase;

class OptimizerTest extends TestCase
{
public function testThrowExceptionWithInvalidTheta(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Number of values in the weights array should be 3');
/** @var Optimizer $optimizer */
$optimizer = $this->getMockForAbstractClass(Optimizer::class, [3]);

$optimizer->setTheta([]);
}

public function testSetTheta(): void
{
/** @var Optimizer $optimizer */
$optimizer = $this->getMockForAbstractClass(Optimizer::class, [2]);
$object = $optimizer->setTheta([0.3, 1]);

$theta = $this->getObjectAttribute($optimizer, 'theta');

$this->assertSame($object, $optimizer);
$this->assertSame([0.3, 1], $theta);
}
}

0 comments on commit a40c50b

Please sign in to comment.