MonoLayer Perceptron
All checks were successful
linter / quality (push) Successful in 6m16s
tests / ci (8.4) (push) Successful in 4m10s
tests / ci (8.5) (push) Successful in 4m29s

This commit is contained in:
2026-04-04 16:45:04 +02:00
parent f6620c2eca
commit 2f4db07918
21 changed files with 641 additions and 226 deletions

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Models\Perceptrons;
class InputNeuron extends Perceptron
{
public function __construct(
) {
parent::__construct([]);
}
public function setInput(float $input): void
{
$this->input = $input;
}
public function test(array $inputs): array
{
return [$this->input];
}
public function activationFunction(float $input): float
{
return $input; // Identity function for input neurons
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace App\Models\Perceptrons;
class NetworkPerceptron extends Perceptron
{
public array $network = [];
public function __construct(
private array $synaptic_weights,
private int $inputLayerNeuronsCount,
private string $hiddenLayerNeuronClass,
private string $outputLayerNeuronClass,
) {
parent::__construct($synaptic_weights);
$this->initializeNetwork($synaptic_weights);
}
private function initializeNetwork(array $synaptic_weights): void
{
// Input Layer
$this->network[0] = [];
foreach (range(0, $this->inputLayerNeuronsCount - 1) as $i) {
$this->network[0][] = new InputNeuron();
}
// Hidden Layer
for ($layerIndex = 0; $layerIndex < count($synaptic_weights) - 2; $layerIndex++) {
$this->network[$layerIndex + 1] = [];
foreach ($synaptic_weights[$layerIndex] as $neuronWeights) {
$this->network[$layerIndex + 1][] = new $this->hiddenLayerNeuronClass($neuronWeights);
}
}
// Output Layer
$outputLayer = $synaptic_weights[count($synaptic_weights) - 1];
$this->network[count($synaptic_weights)] = [];
foreach ($outputLayer as $neuronWeights) {
$this->network[count($synaptic_weights)][] = new $this->outputLayerNeuronClass($neuronWeights);
}
}
public function test(array $inputs): array
{
// Set the inputs for the input layer
foreach ($this->network[0] as $index => $inputNeuron) {
$inputNeuron->setInput($inputs[$index]);
}
// Pass through the hidden and output layers
$output = [];
for ($layerIndex = 0; $layerIndex < count($this->network); $layerIndex++) {
$lastLayerOutput = $output;
$output = [];
foreach ($this->network[$layerIndex] as $neuron) {
$output[] = $neuron->test($lastLayerOutput)[0];
}
}
return $output;
}
public function activationFunction(float $weighted_sum): float
{
return $weighted_sum;
}
public function setSynapticWeights(array $synaptic_weights): void
{
parent::setSynapticWeights($synaptic_weights);
$this->network = [];
$this->initializeNetwork($synaptic_weights);
}
}

View File

@@ -2,9 +2,9 @@
namespace App\Models\Perceptrons;
use Illuminate\Database\Eloquent\Model;
// use Illuminate\Database\Eloquent\Model;
abstract class Perceptron extends Model
abstract class Perceptron
{
public function __construct(
private array $synaptic_weights,
@@ -12,7 +12,7 @@ abstract class Perceptron extends Model
$this->synaptic_weights = $synaptic_weights;
}
public function test(array $inputs): float
public function test(array $inputs): array
{
$inputs = array_merge([1], $inputs); // Add bias input
@@ -22,7 +22,7 @@ abstract class Perceptron extends Model
$weighted_sum = array_sum(array_map(fn ($input, $weight) => $input * $weight, $inputs, $this->synaptic_weights));
return $this->activationFunction($weighted_sum);
return [$this->activationFunction($weighted_sum)];
}
abstract public function activationFunction(float $weighted_sum): float;

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Models\Perceptrons;
class SimpleBinaryPerceptron2 extends Perceptron
{
public function __construct(
array $synaptic_weights,
) {
parent::__construct($synaptic_weights);
}
public function activationFunction(float $weighted_sum): float
{
// return $weighted_sum >= 0.0 ? 1.0 : -1.0;
return $weighted_sum;
}
}