implement activation function for neural network

This commit is contained in:
Arkadiusz Kondas 2016-08-02 13:07:47 +02:00
parent 2f5b090188
commit 637fd613b8
11 changed files with 281 additions and 1 deletions

View 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;
}

View 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;
}
}

View 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));
}
}

View File

@ -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);
}
}

View 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));
}
}

View File

@ -0,0 +1,9 @@
<?php
declare (strict_types = 1);
namespace Phpml\NeuralNetwork\Node;
class Neuron
{
}

View File

@ -13,7 +13,7 @@ class ProductTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(10, Product::scalar([2, 3], [-1, 4])); $this->assertEquals(10, Product::scalar([2, 3], [-1, 4]));
$this->assertEquals(-0.1, Product::scalar([1, 4, 1], [-2, 0.5, -0.1])); $this->assertEquals(-0.1, Product::scalar([1, 4, 1], [-2, 0.5, -0.1]));
$this->assertEquals(8, Product::scalar([2], [4])); $this->assertEquals(8, Product::scalar([2], [4]));
//test for non numeric values //test for non numeric values
$this->assertEquals(0, Product::scalar(['', null, [], new \stdClass()], [null])); $this->assertEquals(0, Product::scalar(['', null, [], new \stdClass()], [null]));
} }

View File

@ -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],
];
}
}

View File

@ -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],
];
}
}

View File

@ -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],
];
}
}

View 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],
];
}
}