diff --git a/src/Phpml/NeuralNetwork/ActivationFunction.php b/src/Phpml/NeuralNetwork/ActivationFunction.php new file mode 100644 index 0000000..9b7d984 --- /dev/null +++ b/src/Phpml/NeuralNetwork/ActivationFunction.php @@ -0,0 +1,15 @@ += 0 ? 1.0 : 0.0; + } +} diff --git a/src/Phpml/NeuralNetwork/ActivationFunction/Gaussian.php b/src/Phpml/NeuralNetwork/ActivationFunction/Gaussian.php new file mode 100644 index 0000000..cdbe4ae --- /dev/null +++ b/src/Phpml/NeuralNetwork/ActivationFunction/Gaussian.php @@ -0,0 +1,20 @@ +beta = $beta; + } + + /** + * @param float|int $value + * + * @return float + */ + public function compute($value): float + { + return tanh($this->beta * $value); + } +} diff --git a/src/Phpml/NeuralNetwork/ActivationFunction/Sigmoid.php b/src/Phpml/NeuralNetwork/ActivationFunction/Sigmoid.php new file mode 100644 index 0000000..ee7b7be --- /dev/null +++ b/src/Phpml/NeuralNetwork/ActivationFunction/Sigmoid.php @@ -0,0 +1,33 @@ +beta = $beta; + } + + /** + * @param float|int $value + * + * @return float + */ + public function compute($value): float + { + return 1 / (1 + exp(-$this->beta * $value)); + } +} diff --git a/src/Phpml/NeuralNetwork/Node/Neuron.php b/src/Phpml/NeuralNetwork/Node/Neuron.php new file mode 100644 index 0000000..52b38e7 --- /dev/null +++ b/src/Phpml/NeuralNetwork/Node/Neuron.php @@ -0,0 +1,9 @@ +assertEquals(10, Product::scalar([2, 3], [-1, 4])); $this->assertEquals(-0.1, Product::scalar([1, 4, 1], [-2, 0.5, -0.1])); $this->assertEquals(8, Product::scalar([2], [4])); - + //test for non numeric values $this->assertEquals(0, Product::scalar(['', null, [], new \stdClass()], [null])); } diff --git a/tests/Phpml/NeuralNetwork/ActivationFunction/BinaryStepTest.php b/tests/Phpml/NeuralNetwork/ActivationFunction/BinaryStepTest.php new file mode 100644 index 0000000..c074955 --- /dev/null +++ b/tests/Phpml/NeuralNetwork/ActivationFunction/BinaryStepTest.php @@ -0,0 +1,35 @@ +assertEquals($expected, $binaryStep->compute($value)); + } + + /** + * @return array + */ + public function binaryStepProvider() + { + return [ + [1, 1], + [1, 0], + [0, -0.1], + ]; + } +} diff --git a/tests/Phpml/NeuralNetwork/ActivationFunction/GaussianTest.php b/tests/Phpml/NeuralNetwork/ActivationFunction/GaussianTest.php new file mode 100644 index 0000000..4780a53 --- /dev/null +++ b/tests/Phpml/NeuralNetwork/ActivationFunction/GaussianTest.php @@ -0,0 +1,37 @@ +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], + ]; + } +} diff --git a/tests/Phpml/NeuralNetwork/ActivationFunction/HyperboliTangentTest.php b/tests/Phpml/NeuralNetwork/ActivationFunction/HyperboliTangentTest.php new file mode 100644 index 0000000..92f4b97 --- /dev/null +++ b/tests/Phpml/NeuralNetwork/ActivationFunction/HyperboliTangentTest.php @@ -0,0 +1,39 @@ +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], + ]; + } +} diff --git a/tests/Phpml/NeuralNetwork/ActivationFunction/SigmoidTest.php b/tests/Phpml/NeuralNetwork/ActivationFunction/SigmoidTest.php new file mode 100644 index 0000000..c84a20b --- /dev/null +++ b/tests/Phpml/NeuralNetwork/ActivationFunction/SigmoidTest.php @@ -0,0 +1,39 @@ +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], + ]; + } +}