Added ADALINE training
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\PerceptronInitialization;
|
||||
use App\Models\NetworksTraining\ADALINEPerceptronTraining;
|
||||
use App\Models\NetworksTraining\GradientDescentPerceptronTraining;
|
||||
use App\Models\NetworksTraining\SimpleBinaryPerceptronTraining;
|
||||
use App\Services\DatasetReader\IDataSetReader;
|
||||
@@ -32,8 +33,9 @@ class PerceptronController extends Controller
|
||||
$learningRate = 0.015;
|
||||
$maxIterations = 150;
|
||||
break;
|
||||
case 'gradientdescent':
|
||||
case 'gradientdescent' || 'adaline':
|
||||
$learningRate = 0.00003;
|
||||
break;
|
||||
}
|
||||
|
||||
return inertia('PerceptronViewer', [
|
||||
@@ -88,6 +90,10 @@ class PerceptronController extends Controller
|
||||
$dataset['defaultLearningRate'] = 0.3;
|
||||
$dataset['defaultMinError'] = 0.125;
|
||||
break;
|
||||
case 'adaline':
|
||||
$dataset['defaultLearningRate'] = 0.05;
|
||||
$dataset['defaultMinError'] = 0.125;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case 'table_2_9':
|
||||
@@ -95,7 +101,7 @@ class PerceptronController extends Controller
|
||||
case 'simple':
|
||||
$dataset['defaultLearningRate'] = 0.015;
|
||||
break;
|
||||
case 'gradientdescent':
|
||||
case 'gradientdescent' || 'adaline':
|
||||
$dataset['defaultLearningRate'] = 0.001;
|
||||
break;
|
||||
}
|
||||
@@ -123,7 +129,7 @@ class PerceptronController extends Controller
|
||||
$weightInitMethod = $request->input('weight_init_method', 'random');
|
||||
$dataSet = $request->input('dataset');
|
||||
$learningRate = $request->input('learning_rate', 0.015);
|
||||
$maxIterations = $request->input('max_iterations', 100);
|
||||
$maxEpochs = $request->input('max_iterations', 100);
|
||||
$sessionId = $request->input('session_id', session()->getId());
|
||||
$trainingId = $request->input('training_id');
|
||||
|
||||
@@ -132,21 +138,22 @@ class PerceptronController extends Controller
|
||||
}
|
||||
|
||||
$iterationEventBuffer = new PerceptronIterationEventBuffer($sessionId, $trainingId);
|
||||
if ($maxIterations > config('perceptron.limited_broadcast_iterations')) {
|
||||
$iterationsInterval = (int)($maxIterations / config('perceptron.limited_broadcast_iterations'));
|
||||
if ($maxEpochs > config('perceptron.limited_broadcast_iterations')) {
|
||||
$iterationsInterval = (int)($maxEpochs / config('perceptron.limited_broadcast_iterations'));
|
||||
$iterationEventBuffer = new PerceptronLimitedEpochEventBuffer($sessionId, $trainingId, $iterationsInterval);
|
||||
}
|
||||
|
||||
$dataSetReader = $this->getDataSetReader($dataSet);
|
||||
$datasetReader = $this->getDataSetReader($dataSet);
|
||||
|
||||
|
||||
$networkTraining = match ($perceptronType) {
|
||||
'simple' => new SimpleBinaryPerceptronTraining($dataSetReader, $learningRate, $maxIterations, $synapticWeightsProvider, $iterationEventBuffer, $sessionId, $trainingId),
|
||||
'gradientdescent' => new GradientDescentPerceptronTraining($dataSetReader, $learningRate, $maxIterations, $synapticWeightsProvider, $iterationEventBuffer, $sessionId, $trainingId, $minError),
|
||||
'simple' => new SimpleBinaryPerceptronTraining($datasetReader, $learningRate, $maxEpochs, $synapticWeightsProvider, $iterationEventBuffer, $sessionId, $trainingId),
|
||||
'gradientdescent' => new GradientDescentPerceptronTraining($datasetReader, $learningRate, $maxEpochs, $synapticWeightsProvider, $iterationEventBuffer, $sessionId, $trainingId, $minError),
|
||||
'adaline' => new ADALINEPerceptronTraining($datasetReader, $learningRate, $maxEpochs, $synapticWeightsProvider, $iterationEventBuffer, $sessionId, $trainingId, $minError),
|
||||
default => null,
|
||||
};
|
||||
|
||||
event(new PerceptronInitialization($dataSetReader->lines, $networkTraining->activationFunction, $sessionId, $trainingId));
|
||||
event(new PerceptronInitialization($datasetReader->lines, $networkTraining->activationFunction, $sessionId, $trainingId));
|
||||
|
||||
$networkTraining->start();
|
||||
|
||||
|
||||
105
app/Models/NetworksTraining/ADALINEPerceptronTraining.php
Normal file
105
app/Models/NetworksTraining/ADALINEPerceptronTraining.php
Normal file
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\NetworksTraining;
|
||||
|
||||
use App\Events\PerceptronTrainingEnded;
|
||||
use App\Models\ActivationsFunctions;
|
||||
use App\Models\Perceptrons\GradientDescentPerceptron;
|
||||
use App\Models\Perceptrons\Perceptron;
|
||||
use App\Services\DatasetReader\IDataSetReader;
|
||||
use App\Services\IterationEventBuffer\IPerceptronIterationEventBuffer;
|
||||
use App\Services\SynapticWeightsProvider\ISynapticWeightsProvider;
|
||||
|
||||
class ADALINEPerceptronTraining extends NetworkTraining
|
||||
{
|
||||
private Perceptron $perceptron;
|
||||
|
||||
public ActivationsFunctions $activationFunction = ActivationsFunctions::LINEAR;
|
||||
|
||||
private float $epochError;
|
||||
|
||||
public function __construct(
|
||||
IDataSetReader $datasetReader,
|
||||
protected float $learningRate,
|
||||
int $maxEpochs,
|
||||
protected ISynapticWeightsProvider $synapticWeightsProvider,
|
||||
IPerceptronIterationEventBuffer $iterationEventBuffer,
|
||||
string $sessionId,
|
||||
string $trainingId,
|
||||
private float $minError,
|
||||
) {
|
||||
parent::__construct($datasetReader, $maxEpochs, $iterationEventBuffer, $sessionId, $trainingId);
|
||||
$this->perceptron = new GradientDescentPerceptron($synapticWeightsProvider->generate($datasetReader->getInputSize()));
|
||||
}
|
||||
|
||||
public function start(): void
|
||||
{
|
||||
$this->epoch = 0;
|
||||
do {
|
||||
$this->epochError = 0;
|
||||
$this->epoch++;
|
||||
|
||||
$inputsForCurrentEpoch = [];
|
||||
|
||||
while ($nextRow = $this->datasetReader->getNextLine()) {
|
||||
$inputsForCurrentEpoch[] = $nextRow;
|
||||
$inputs = array_slice($nextRow, 0, -1);
|
||||
$correctOutput = (float) end($nextRow);
|
||||
|
||||
$iterationError = $this->iterationFunction($inputs, $correctOutput);
|
||||
|
||||
// Synaptic weights correction after each example
|
||||
$synaptic_weights = $this->perceptron->getSynapticWeights();
|
||||
$inputs_with_bias = array_merge([1], $inputs); // Add bias input
|
||||
$new_weights = array_map(
|
||||
fn($weight, $weightIndex) => $weight + ($this->learningRate * $iterationError * $inputs_with_bias[$weightIndex]),
|
||||
$synaptic_weights,
|
||||
array_keys($synaptic_weights)
|
||||
);
|
||||
$this->perceptron->setSynapticWeights($new_weights);
|
||||
|
||||
// Broadcast the training iteration event
|
||||
$this->addIterationToBuffer($iterationError, [[$this->perceptron->getSynapticWeights()]]);
|
||||
}
|
||||
|
||||
// Calculte the average error for the epoch with the last synaptic weights
|
||||
foreach ($inputsForCurrentEpoch as $inputsWithLabel) {
|
||||
$inputs = array_slice($inputsWithLabel, 0, -1);
|
||||
$correctOutput = (float) end($inputsWithLabel);
|
||||
$output = $this->perceptron->test($inputs);
|
||||
$iterationError = $correctOutput - $output;
|
||||
$this->epochError += ($iterationError ** 2) / 2; // Squared error for the example
|
||||
}
|
||||
$this->epochError /= $this->datasetReader->getEpochExamplesCount(); // Average error for the epoch
|
||||
|
||||
$this->datasetReader->reset(); // Reset the dataset for the next iteration
|
||||
} while ($this->epoch < $this->maxEpochs && !$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;
|
||||
}
|
||||
|
||||
public function getSynapticWeights(): array
|
||||
{
|
||||
return [[$this->perceptron->getSynapticWeights()]];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user