Ability to update learningRate in MLP (#160)

* Allow people to update the learning rate

* Test for learning rate setter
This commit is contained in:
David Monllaó 2017-12-05 21:09:06 +01:00 committed by Arkadiusz Kondas
parent c4f58f7f6f
commit c4ad117d28
4 changed files with 45 additions and 0 deletions

View File

@ -45,6 +45,12 @@ $mlp->partialTrain(
``` ```
You can update the learning rate between partialTrain runs:
```
$mlp->setLearningRate(0.1);
```
## Predict ## Predict
To predict sample label use predict method. You can provide one sample or array of samples: To predict sample label use predict method. You can provide one sample or array of samples:

View File

@ -95,6 +95,12 @@ abstract class MultilayerPerceptron extends LayeredNetwork implements Estimator,
} }
} }
public function setLearningRate(float $learningRate): void
{
$this->learningRate = $learningRate;
$this->backpropagation->setLearningRate($this->learningRate);
}
/** /**
* @param mixed $target * @param mixed $target
*/ */

View File

@ -25,6 +25,11 @@ class Backpropagation
private $prevSigmas = null; private $prevSigmas = null;
public function __construct(float $learningRate) public function __construct(float $learningRate)
{
$this->setLearningRate($learningRate);
}
public function setLearningRate(float $learningRate): void
{ {
$this->learningRate = $learningRate; $this->learningRate = $learningRate;
} }

View File

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace tests\Phpml\NeuralNetwork\Network;
use Phpml\NeuralNetwork\Network\MultilayerPerceptron;
use PHPUnit\Framework\TestCase;
class MultilayerPerceptronTest extends TestCase
{
public function testLearningRateSetter(): void
{
$mlp = $this->getMockForAbstractClass(
MultilayerPerceptron::class,
[5, [3], [0, 1], 1000, null, 0.42]
);
$this->assertEquals(0.42, $this->readAttribute($mlp, 'learningRate'));
$backprop = $this->readAttribute($mlp, 'backpropagation');
$this->assertEquals(0.42, $this->readAttribute($backprop, 'learningRate'));
$mlp->setLearningRate(0.24);
$this->assertEquals(0.24, $this->readAttribute($mlp, 'learningRate'));
$backprop = $this->readAttribute($mlp, 'backpropagation');
$this->assertEquals(0.24, $this->readAttribute($backprop, 'learningRate'));
}
}