mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-21 20:45:10 +00:00
Add Optimizer tests and remove initialTheta (#252)
* Add Optimizer tests * Remove Optimizer.initialTheta and rename Optimizer.setInitialTheta to setTheta
This commit is contained in:
parent
55749c7c92
commit
a40c50b48b
@ -9,8 +9,6 @@ use Phpml\Exception\InvalidArgumentException;
|
||||
|
||||
abstract class Optimizer
|
||||
{
|
||||
public $initialTheta;
|
||||
|
||||
/**
|
||||
* Unknown variables to be found
|
||||
*
|
||||
@ -37,11 +35,9 @@ abstract class Optimizer
|
||||
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));
|
||||
|
@ -89,7 +89,7 @@ class StochasticGD extends Optimizer
|
||||
$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));
|
||||
|
@ -57,7 +57,7 @@ class ConjugateGradientTest extends TestCase
|
||||
|
||||
$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);
|
||||
|
||||
@ -97,6 +97,6 @@ class ConjugateGradientTest extends TestCase
|
||||
$opimizer = new ConjugateGradient(2);
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$opimizer->setInitialTheta([0.15]);
|
||||
$opimizer->setTheta([0.15]);
|
||||
}
|
||||
}
|
||||
|
34
tests/Helper/Optimizer/OptimizerTest.php
Normal file
34
tests/Helper/Optimizer/OptimizerTest.php
Normal file
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user