Gradient descent training + Added all dataset + graphs improvements
This commit is contained in:
@@ -2,14 +2,14 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\SimplePerceptron;
|
||||
use App\Models\SimplePerceptronTraining;
|
||||
use App\Events\PerceptronInitialization;
|
||||
use App\Models\GradientDescentPerceptronTraining;
|
||||
use App\Models\SimpleBinaryPerceptronTraining;
|
||||
use App\Services\DataSetReader;
|
||||
use App\Services\ISynapticWeightsProvider;
|
||||
use App\Services\PerceptronIterationEventBuffer;
|
||||
use App\Services\ZeroSynapticWeights;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class PerceptronController extends Controller
|
||||
{
|
||||
@@ -18,29 +18,32 @@ class PerceptronController extends Controller
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$perceptronType = $request->query('type', 'simple');
|
||||
$perceptronType = $request->query('type');
|
||||
|
||||
$learningRate = 0.1;
|
||||
$maxIterations = 100;
|
||||
$learningRate = 0.01;
|
||||
$maxIterations = 200;
|
||||
$minError = 0.6;
|
||||
|
||||
switch ($perceptronType) {
|
||||
case 'simple':
|
||||
$learningRate = 0.015;
|
||||
$maxIterations = 100;
|
||||
$maxIterations = 150;
|
||||
break;
|
||||
case 'gradientdescent':
|
||||
$learningRate = 0.00003;
|
||||
}
|
||||
|
||||
return inertia('PerceptronViewer', [
|
||||
'type' => $perceptronType,
|
||||
'sessionId' => session()->getId(),
|
||||
'datasets' => $this->getDatasets(),
|
||||
'minError' => 0.01,
|
||||
'datasets' => $this->getDatasets($perceptronType),
|
||||
'minError' => $minError,
|
||||
'learningRate' => $learningRate,
|
||||
'maxIterations' => $maxIterations,
|
||||
]);
|
||||
}
|
||||
|
||||
private function getDatasets(): array
|
||||
private function getDatasets(string $perceptronType): array
|
||||
{
|
||||
$dataSetsDirectory = public_path('data_sets');
|
||||
$files = scandir($dataSetsDirectory);
|
||||
@@ -51,17 +54,44 @@ class PerceptronController extends Controller
|
||||
$dataset['label'] = str_replace('.csv', '', $file);
|
||||
$dataSetReader = new DataSetReader($dataSetsDirectory . '/' . $file);
|
||||
$dataset['data'] = [];
|
||||
foreach ($dataSetReader->lines as $line) {
|
||||
$dataset['data'][] = [
|
||||
'x' => $line[0],
|
||||
'y' => $line[1],
|
||||
'label' => $line[2],
|
||||
];
|
||||
switch (count($dataSetReader->lines[0])) {
|
||||
case 3:
|
||||
foreach ($dataSetReader->lines as $line) {
|
||||
$dataset['data'][] = [
|
||||
'x' => $line[0],
|
||||
'y' => $line[1],
|
||||
'label' => $line[2],
|
||||
];
|
||||
}
|
||||
break;
|
||||
case 2:
|
||||
foreach ($dataSetReader->lines as $line) {
|
||||
$dataset['data'][] = [
|
||||
'x' => $line[0],
|
||||
'y' => $line[1],
|
||||
'label' => 1,
|
||||
];
|
||||
}
|
||||
break;
|
||||
default:
|
||||
$dataset['data'] = null; // Not supported for viewing
|
||||
break;
|
||||
}
|
||||
|
||||
switch ($dataset['label']) {
|
||||
case '2.9':
|
||||
$dataset['defaultLearningRate'] = 0.015;
|
||||
case 'logic_and_gradient':
|
||||
switch ($perceptronType) {
|
||||
case 'gradientdescent':
|
||||
$dataset['defaultLearningRate'] = 0.3;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'table_2_9':
|
||||
switch ($perceptronType) {
|
||||
case 'simple':
|
||||
$dataset['defaultLearningRate'] = 0.015;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
$datasets[] = $dataset;
|
||||
@@ -78,7 +108,7 @@ class PerceptronController extends Controller
|
||||
|
||||
public function run(Request $request, ISynapticWeightsProvider $synapticWeightsProvider)
|
||||
{
|
||||
$perceptronType = $request->input('type', 'simple');
|
||||
$perceptronType = $request->input('type');
|
||||
$minError = $request->input('min_error', 0.01);
|
||||
$weightInitMethod = $request->input('weight_init_method', 'random');
|
||||
$dataSet = $request->input('dataset');
|
||||
@@ -96,11 +126,12 @@ class PerceptronController extends Controller
|
||||
$iterationEventBuffer = new PerceptronIterationEventBuffer($sessionId, $trainingId);
|
||||
|
||||
$networkTraining = match ($perceptronType) {
|
||||
'simple' => new SimplePerceptronTraining($dataSetReader, $learningRate, $maxIterations, $synapticWeightsProvider, $iterationEventBuffer, $sessionId, $trainingId),
|
||||
'simple' => new SimpleBinaryPerceptronTraining($dataSetReader, $learningRate, $maxIterations, $synapticWeightsProvider, $iterationEventBuffer, $sessionId, $trainingId),
|
||||
'gradientdescent' => new GradientDescentPerceptronTraining($dataSetReader, $learningRate, $maxIterations, $synapticWeightsProvider, $iterationEventBuffer, $sessionId, $trainingId, $minError),
|
||||
default => null,
|
||||
};
|
||||
|
||||
event(new \App\Events\PerceptronInitialization($dataSetReader->lines, $networkTraining->activationFunction, $sessionId, $trainingId));
|
||||
event(new PerceptronInitialization($dataSetReader->lines, $networkTraining->activationFunction, $sessionId, $trainingId));
|
||||
|
||||
$networkTraining->start();
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
||||
enum ActivationsFunctions: string
|
||||
{
|
||||
case STEP = 'step';
|
||||
case LINEAR = 'linear';
|
||||
case SIGMOID = 'sigmoid';
|
||||
case RELU = 'relu';
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
class SimplePerceptron extends Perceptron {
|
||||
class GradientDescentPerceptron extends Perceptron {
|
||||
|
||||
public function __construct(
|
||||
array $synaptic_weights,
|
||||
@@ -10,9 +10,9 @@ class SimplePerceptron extends Perceptron {
|
||||
parent::__construct($synaptic_weights);
|
||||
}
|
||||
|
||||
public function activationFunction(float $weighted_sum): int
|
||||
public function activationFunction(float $weighted_sum): float
|
||||
{
|
||||
return $weighted_sum >= 0 ? 1 : 0;
|
||||
return $weighted_sum;
|
||||
}
|
||||
|
||||
}
|
||||
91
app/Models/GradientDescentPerceptronTraining.php
Normal file
91
app/Models/GradientDescentPerceptronTraining.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Events\PerceptronTrainingEnded;
|
||||
use App\Services\DataSetReader;
|
||||
use App\Services\ISynapticWeightsProvider;
|
||||
use App\Services\PerceptronIterationEventBuffer;
|
||||
|
||||
class GradientDescentPerceptronTraining extends NetworkTraining
|
||||
{
|
||||
private Perceptron $perceptron;
|
||||
|
||||
public ActivationsFunctions $activationFunction = ActivationsFunctions::LINEAR;
|
||||
|
||||
private float $epochError;
|
||||
|
||||
public function __construct(
|
||||
DataSetReader $datasetReader,
|
||||
protected float $learningRate,
|
||||
int $maxIterations,
|
||||
protected ISynapticWeightsProvider $synapticWeightsProvider,
|
||||
PerceptronIterationEventBuffer $iterationEventBuffer,
|
||||
string $sessionId,
|
||||
string $trainingId,
|
||||
private float $minError,
|
||||
) {
|
||||
parent::__construct($datasetReader, $maxIterations, $iterationEventBuffer, $sessionId, $trainingId);
|
||||
$this->perceptron = new GradientDescentPerceptron($synapticWeightsProvider->generate($datasetReader->getInputSize()));
|
||||
}
|
||||
|
||||
public function start(): void
|
||||
{
|
||||
$this->iteration = 0;
|
||||
do {
|
||||
$this->epochError = 0;
|
||||
$iterationErrorPerWeight = [];
|
||||
$this->iteration++;
|
||||
|
||||
while ($nextRow = $this->datasetReader->getRandomLine()) {
|
||||
$inputs = array_slice($nextRow, 0, -1);
|
||||
$correctOutput = (float) end($nextRow);
|
||||
|
||||
$iterationError = $this->iterationFunction($inputs, $correctOutput);
|
||||
$this->epochError += (1 / 2) * (abs($iterationError) ** 2); // TDDO REMOVEME abs()
|
||||
|
||||
// Store the iteration error for each weight
|
||||
$inputs_with_bias = array_merge([1], $inputs); // Add bias input
|
||||
foreach ($inputs_with_bias as $index => $input) {
|
||||
$iterationErrorPerWeight[$index][] = $iterationError * $input;
|
||||
}
|
||||
|
||||
// Broadcast the training iteration event
|
||||
$this->addIterationToBuffer($iterationError, [[$this->perceptron->getSynapticWeights()]]);
|
||||
}
|
||||
|
||||
// Synaptic weights correction after each epoch
|
||||
$synaptic_weights = $this->perceptron->getSynapticWeights();
|
||||
$new_weights = array_map(
|
||||
fn($weight, $weightIndex) => $weight + $this->learningRate * array_sum($iterationErrorPerWeight[$weightIndex]),
|
||||
$synaptic_weights,
|
||||
array_keys($synaptic_weights)
|
||||
);
|
||||
$this->perceptron->setSynapticWeights($new_weights);
|
||||
|
||||
$this->datasetReader->reset(); // Reset the dataset for the next iteration
|
||||
} while ($this->iteration < $this->maxIterations && !$this->stopCondition());
|
||||
|
||||
$this->iterationEventBuffer->flush(); // Ensure all iterations are sent to the frontend
|
||||
|
||||
$this->checkPassedMaxIterations($this->epochError);
|
||||
}
|
||||
|
||||
protected function stopCondition(): bool
|
||||
{
|
||||
$condition = $this->epochError <= $this->minError && $this->perceptron->getSynapticWeights() !== [[0.0, 0.0, 0.0]];
|
||||
if ($condition === true) {
|
||||
event(new PerceptronTrainingEnded('Le perceptron à atteint l\'erreur minimale', $this->sessionId, $this->trainingId));
|
||||
}
|
||||
return $condition;
|
||||
}
|
||||
|
||||
private function iterationFunction(array $inputs, int $correctOutput)
|
||||
{
|
||||
$output = $this->perceptron->test($inputs);
|
||||
|
||||
$error = $correctOutput - $output;
|
||||
|
||||
return $error;
|
||||
}
|
||||
}
|
||||
8
app/Models/Network.php
Normal file
8
app/Models/Network.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
abstract class Network
|
||||
{
|
||||
|
||||
}
|
||||
@@ -28,9 +28,14 @@ abstract class NetworkTraining
|
||||
abstract public function start() : void;
|
||||
abstract protected function stopCondition(): bool;
|
||||
|
||||
protected function checkPassedMaxIterations() {
|
||||
protected function checkPassedMaxIterations(?float $finalError) {
|
||||
if ($this->iteration >= $this->maxIterations) {
|
||||
event(new PerceptronTrainingEnded('Le nombre maximal d\'itérations a été atteint', $this->sessionId, $this->trainingId));
|
||||
$message = 'Le nombre maximal d\'itérations a été atteint';
|
||||
if ($finalError) {
|
||||
$message .= " avec une erreur finale de $finalError";
|
||||
}
|
||||
|
||||
event(new PerceptronTrainingEnded($message, $this->sessionId, $this->trainingId));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ abstract class Perceptron extends Model
|
||||
$this->synaptic_weights = $synaptic_weights;
|
||||
}
|
||||
|
||||
public function test(array $inputs): int
|
||||
public function test(array $inputs): float
|
||||
{
|
||||
$inputs = array_merge([1], $inputs); // Add bias input
|
||||
|
||||
@@ -24,7 +24,7 @@ abstract class Perceptron extends Model
|
||||
return $this->activationFunction($weighted_sum);
|
||||
}
|
||||
|
||||
abstract public function activationFunction(float $weighted_sum): int;
|
||||
abstract public function activationFunction(float $weighted_sum): float;
|
||||
|
||||
public function getSynapticWeights(): array
|
||||
{
|
||||
|
||||
18
app/Models/SimpleBinaryPerceptron.php
Normal file
18
app/Models/SimpleBinaryPerceptron.php
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
class SimpleBinaryPerceptron 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 : 0.0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,9 +6,8 @@ use App\Events\PerceptronTrainingEnded;
|
||||
use App\Services\DataSetReader;
|
||||
use App\Services\ISynapticWeightsProvider;
|
||||
use App\Services\PerceptronIterationEventBuffer;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class SimplePerceptronTraining extends NetworkTraining
|
||||
class SimpleBinaryPerceptronTraining extends NetworkTraining
|
||||
{
|
||||
private Perceptron $perceptron;
|
||||
private int $iterationErrorCounter = 0;
|
||||
@@ -27,7 +26,7 @@ class SimplePerceptronTraining extends NetworkTraining
|
||||
string $trainingId,
|
||||
) {
|
||||
parent::__construct($datasetReader, $maxIterations, $iterationEventBuffer, $sessionId, $trainingId);
|
||||
$this->perceptron = new SimplePerceptron($synapticWeightsProvider->generate(2));
|
||||
$this->perceptron = new SimpleBinaryPerceptron($synapticWeightsProvider->generate($datasetReader->getInputSize()));
|
||||
}
|
||||
|
||||
public function start(): void
|
||||
@@ -40,13 +39,11 @@ class SimplePerceptronTraining extends NetworkTraining
|
||||
|
||||
while ($nextRow = $this->datasetReader->getRandomLine()) {
|
||||
$inputs = array_slice($nextRow, 0, -1);
|
||||
$correctOutput = end($nextRow);
|
||||
$correctOutput = (float) end($nextRow);
|
||||
$correctOutput = $correctOutput > 0 ? 1 : 0; // Modify labels for non binary datasets
|
||||
|
||||
$error = $this->iterationFunction($inputs, $correctOutput);
|
||||
|
||||
$error = abs($error); // Use absolute error
|
||||
|
||||
// Broadcast the training iteration event
|
||||
$this->addIterationToBuffer($error, [[$this->perceptron->getSynapticWeights()]]);
|
||||
}
|
||||
@@ -55,7 +52,7 @@ class SimplePerceptronTraining extends NetworkTraining
|
||||
|
||||
$this->iterationEventBuffer->flush(); // Ensure all iterations are sent to the frontend
|
||||
|
||||
$this->checkPassedMaxIterations();
|
||||
$this->checkPassedMaxIterations(null);
|
||||
}
|
||||
|
||||
protected function stopCondition(): bool
|
||||
@@ -4,7 +4,7 @@ namespace App\Services;
|
||||
|
||||
class CsvReader {
|
||||
private $file;
|
||||
private array $headers;
|
||||
// private array $headers;
|
||||
|
||||
public array $lines = [];
|
||||
|
||||
@@ -17,7 +17,7 @@ class CsvReader {
|
||||
throw new \RuntimeException("Failed to open file: " . $filename);
|
||||
}
|
||||
|
||||
$this->headers = $this->readNextLine();
|
||||
// $this->headers = $this->readNextLine();
|
||||
}
|
||||
|
||||
public function readNextLine(): ?array
|
||||
|
||||
@@ -20,7 +20,17 @@ class DataSetReader {
|
||||
private function readEntireFile(CsvReader $reader): void
|
||||
{
|
||||
while ($line = $reader->readNextLine()) {
|
||||
$this->lines[] = $line;
|
||||
$newLine = [];
|
||||
foreach ($line as $value) { // Transform to float
|
||||
$newLine[] = (float) $value;
|
||||
}
|
||||
|
||||
// if the dataset is for regression, we add a fake label of 0
|
||||
if (count($newLine) === 2) {
|
||||
$newLine[] = 0.0;
|
||||
}
|
||||
|
||||
$this->lines[] = $newLine;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,12 +41,21 @@ class DataSetReader {
|
||||
}
|
||||
$randomNumber = array_rand($this->currentLines);
|
||||
$randomLine = $this->currentLines[$randomNumber];
|
||||
|
||||
// Remove the line from the current lines to avoid repetition
|
||||
unset($this->currentLines[$randomNumber]);
|
||||
|
||||
// Remember the index of the last read line in the full list
|
||||
$this->lastReadLineIndex = array_search($randomLine, $this->lines, true);
|
||||
|
||||
return $randomLine;
|
||||
}
|
||||
|
||||
public function getInputSize(): int
|
||||
{
|
||||
return count($this->lines[0]) - 1; // Don't count the label
|
||||
}
|
||||
|
||||
public function reset(): void
|
||||
{
|
||||
$this->currentLines = $this->lines;
|
||||
|
||||
Reference in New Issue
Block a user