mirror of
https://github.com/Llewellynvdm/php-ml.git
synced 2025-01-09 16:36:34 +00:00
implement activation function for neural network
This commit is contained in:
parent
2f5b090188
commit
637fd613b8
15
src/Phpml/NeuralNetwork/ActivationFunction.php
Normal file
15
src/Phpml/NeuralNetwork/ActivationFunction.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\NeuralNetwork;
|
||||
|
||||
interface ActivationFunction
|
||||
{
|
||||
/**
|
||||
* @param float|int $value
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function compute($value): float;
|
||||
}
|
20
src/Phpml/NeuralNetwork/ActivationFunction/BinaryStep.php
Normal file
20
src/Phpml/NeuralNetwork/ActivationFunction/BinaryStep.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\NeuralNetwork\ActivationFunction;
|
||||
|
||||
use Phpml\NeuralNetwork\ActivationFunction;
|
||||
|
||||
class BinaryStep implements ActivationFunction
|
||||
{
|
||||
/**
|
||||
* @param float|int $value
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function compute($value): float
|
||||
{
|
||||
return $value >= 0 ? 1.0 : 0.0;
|
||||
}
|
||||
}
|
20
src/Phpml/NeuralNetwork/ActivationFunction/Gaussian.php
Normal file
20
src/Phpml/NeuralNetwork/ActivationFunction/Gaussian.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\NeuralNetwork\ActivationFunction;
|
||||
|
||||
use Phpml\NeuralNetwork\ActivationFunction;
|
||||
|
||||
class Gaussian implements ActivationFunction
|
||||
{
|
||||
/**
|
||||
* @param float|int $value
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function compute($value): float
|
||||
{
|
||||
return exp(-pow($value, 2));
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\NeuralNetwork\ActivationFunction;
|
||||
|
||||
use Phpml\NeuralNetwork\ActivationFunction;
|
||||
|
||||
class HyperbolicTangent implements ActivationFunction
|
||||
{
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $beta;
|
||||
|
||||
/**
|
||||
* @param float $beta
|
||||
*/
|
||||
public function __construct($beta = 1.0)
|
||||
{
|
||||
$this->beta = $beta;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float|int $value
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function compute($value): float
|
||||
{
|
||||
return tanh($this->beta * $value);
|
||||
}
|
||||
}
|
33
src/Phpml/NeuralNetwork/ActivationFunction/Sigmoid.php
Normal file
33
src/Phpml/NeuralNetwork/ActivationFunction/Sigmoid.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\NeuralNetwork\ActivationFunction;
|
||||
|
||||
use Phpml\NeuralNetwork\ActivationFunction;
|
||||
|
||||
class Sigmoid implements ActivationFunction
|
||||
{
|
||||
/**
|
||||
* @var float
|
||||
*/
|
||||
private $beta;
|
||||
|
||||
/**
|
||||
* @param float $beta
|
||||
*/
|
||||
public function __construct($beta = 1.0)
|
||||
{
|
||||
$this->beta = $beta;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param float|int $value
|
||||
*
|
||||
* @return float
|
||||
*/
|
||||
public function compute($value): float
|
||||
{
|
||||
return 1 / (1 + exp(-$this->beta * $value));
|
||||
}
|
||||
}
|
9
src/Phpml/NeuralNetwork/Node/Neuron.php
Normal file
9
src/Phpml/NeuralNetwork/Node/Neuron.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace Phpml\NeuralNetwork\Node;
|
||||
|
||||
class Neuron
|
||||
{
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace tests\Phpml\NeuralNetwork\ActivationFunction;
|
||||
|
||||
use Phpml\NeuralNetwork\ActivationFunction\BinaryStep;
|
||||
|
||||
class BinaryStepTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @param $expected
|
||||
* @param $value
|
||||
*
|
||||
* @dataProvider binaryStepProvider
|
||||
*/
|
||||
public function testBinaryStepActivationFunction($expected, $value)
|
||||
{
|
||||
$binaryStep = new BinaryStep();
|
||||
|
||||
$this->assertEquals($expected, $binaryStep->compute($value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function binaryStepProvider()
|
||||
{
|
||||
return [
|
||||
[1, 1],
|
||||
[1, 0],
|
||||
[0, -0.1],
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace tests\Phpml\NeuralNetwork\ActivationFunction;
|
||||
|
||||
use Phpml\NeuralNetwork\ActivationFunction\Gaussian;
|
||||
|
||||
class GaussianTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @param $expected
|
||||
* @param $value
|
||||
*
|
||||
* @dataProvider gaussianProvider
|
||||
*/
|
||||
public function testGaussianActivationFunction($expected, $value)
|
||||
{
|
||||
$gaussian = new Gaussian();
|
||||
|
||||
$this->assertEquals($expected, $gaussian->compute($value), '', 0.001);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function gaussianProvider()
|
||||
{
|
||||
return [
|
||||
[0.367, 1],
|
||||
[1, 0],
|
||||
[0.367, -1],
|
||||
[0, 3],
|
||||
[0, -3],
|
||||
];
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace tests\Phpml\NeuralNetwork\ActivationFunction;
|
||||
|
||||
use Phpml\NeuralNetwork\ActivationFunction\HyperbolicTangent;
|
||||
|
||||
class HyperboliTangentTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @param $beta
|
||||
* @param $expected
|
||||
* @param $value
|
||||
*
|
||||
* @dataProvider tanhProvider
|
||||
*/
|
||||
public function testHyperbolicTangentActivationFunction($beta, $expected, $value)
|
||||
{
|
||||
$tanh = new HyperbolicTangent($beta);
|
||||
|
||||
$this->assertEquals($expected, $tanh->compute($value), '', 0.001);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function tanhProvider()
|
||||
{
|
||||
return [
|
||||
[1.0, 0.761, 1],
|
||||
[1.0, 0, 0],
|
||||
[1.0, 1, 4],
|
||||
[1.0, -1, -4],
|
||||
[0.5, 0.462, 1],
|
||||
[0.3, 0, 0],
|
||||
];
|
||||
}
|
||||
}
|
39
tests/Phpml/NeuralNetwork/ActivationFunction/SigmoidTest.php
Normal file
39
tests/Phpml/NeuralNetwork/ActivationFunction/SigmoidTest.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace tests\Phpml\NeuralNetwork\ActivationFunction;
|
||||
|
||||
use Phpml\NeuralNetwork\ActivationFunction\Sigmoid;
|
||||
|
||||
class SigmoidTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @param $beta
|
||||
* @param $expected
|
||||
* @param $value
|
||||
*
|
||||
* @dataProvider sigmoidProvider
|
||||
*/
|
||||
public function testSigmoidActivationFunction($beta, $expected, $value)
|
||||
{
|
||||
$sigmoid = new Sigmoid($beta);
|
||||
|
||||
$this->assertEquals($expected, $sigmoid->compute($value), '', 0.001);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function sigmoidProvider()
|
||||
{
|
||||
return [
|
||||
[1.0, 1, 7.25],
|
||||
[2.0, 1, 3.75],
|
||||
[1.0, 0.5, 0],
|
||||
[0.5, 0.5, 0],
|
||||
[1.0, 0, -7.25],
|
||||
[2.0, 0, -3.75],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user