Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Neuron
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 addInput
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 output
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Ai\NeuralNetwork
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Ai\NeuralNetwork;
16
17/**
18 * Neuron
19 *
20 * @package phpOMS\Ai\NeuralNetwork
21 * @license OMS License 2.0
22 * @link    https://jingga.app
23 * @since   1.0.0
24 */
25class Neuron
26{
27    /**
28     * Neuron inputs
29     *
30     * @var array
31     * @since 1.0.0
32     */
33    private array $inputs = [];
34
35    /**
36     * Input weights
37     *
38     * @var array
39     * @since 1.0.0
40     */
41    private array $weights = [];
42
43    /**
44     * Bias
45     *
46     * @var float
47     * @since 1.0.0
48     */
49    public float $bias = 0;
50
51    /**
52     * Constructor.
53     *
54     * @param array $inputs  Neuron inputs/connections
55     * @param array $weights Input weights
56     * @param float $bias    Input bias
57     *
58     * @since 1.0.0
59     */
60    public function __construct(array $inputs = [], array $weights = [], float $bias = 0.0)
61    {
62        $this->inputs  = $inputs;
63        $this->weights = $weights;
64        $this->bias    = $bias;
65    }
66
67    /**
68     * Add neuron input
69     *
70     * @param mixed $input  Input
71     * @param float $weight Weight of input
72     *
73     * @return void
74     *
75     * @since 1.0.0
76     */
77    public function addInput(mixed $input, float $weight) : void
78    {
79        $this->inputs[]  = $input;
80        $this->weights[] = $weight;
81    }
82
83    /**
84     * Create node output
85     *
86     * @return float
87     *
88     * @since 1.0.0
89     */
90    public function output() : float
91    {
92        $length = \count($this->inputs);
93        $output = 0.0;
94
95        for ($i = 0; $i < $length; ++$i) {
96            $output += $this->inputs[$i]->output() * $this->weights[$i];
97        }
98
99        return $output + $this->bias;
100        // return $this->activationFunction($output + $this->bias);
101    }
102}