mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2024-11-24 22:07:33 +00:00
Ability to update learningRate in MLP (#160)
* Allow people to update the learning rate * Test for learning rate setter
This commit is contained in:
parent
c4f58f7f6f
commit
c4ad117d28
@ -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:
|
||||||
|
@ -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
|
||||||
*/
|
*/
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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'));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user