2016-08-10 20:43:47 +00:00
|
|
|
<?php
|
|
|
|
|
2016-11-20 21:53:17 +00:00
|
|
|
declare(strict_types=1);
|
2016-08-10 20:43:47 +00:00
|
|
|
|
|
|
|
namespace tests\Phpml\NeuralNetwork\Training;
|
|
|
|
|
|
|
|
use Phpml\NeuralNetwork\Network\MultilayerPerceptron;
|
|
|
|
use Phpml\NeuralNetwork\Training\Backpropagation;
|
|
|
|
|
|
|
|
class BackpropagationTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testBackpropagationForXORLearning()
|
|
|
|
{
|
|
|
|
$network = new MultilayerPerceptron([2, 2, 1]);
|
|
|
|
$training = new Backpropagation($network);
|
|
|
|
|
|
|
|
$training->train(
|
|
|
|
[[1, 0], [0, 1], [1, 1], [0, 0]],
|
|
|
|
[[1], [1], [0], [0]],
|
2016-08-21 12:03:20 +00:00
|
|
|
$desiredError = 0.3,
|
2016-11-20 21:56:18 +00:00
|
|
|
40000
|
2016-08-10 20:43:47 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(0, $network->setInput([1, 1])->getOutput()[0], '', $desiredError);
|
|
|
|
$this->assertEquals(0, $network->setInput([0, 0])->getOutput()[0], '', $desiredError);
|
|
|
|
$this->assertEquals(1, $network->setInput([1, 0])->getOutput()[0], '', $desiredError);
|
|
|
|
$this->assertEquals(1, $network->setInput([0, 1])->getOutput()[0], '', $desiredError);
|
|
|
|
}
|
|
|
|
}
|