Compare commits

...

2 Commits

Author SHA1 Message Date
a70b7670e7 Use correct naming for iteration and epoch
Some checks failed
linter / quality (push) Failing after 7s
tests / ci (8.4) (push) Failing after 4s
tests / ci (8.5) (push) Failing after 4s
2026-03-21 09:46:35 +01:00
6abb417430 Added Limited Epoch Event Buffer
for better frontend performance when using big  max epoch number
2026-03-21 09:42:05 +01:00
17 changed files with 185 additions and 57 deletions

View File

@@ -7,7 +7,6 @@ use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
class PerceptronTrainingIteration implements ShouldBroadcast
{
@@ -17,7 +16,7 @@ class PerceptronTrainingIteration implements ShouldBroadcast
* Create a new event instance.
*/
public function __construct(
public array $iterations, // ["iteration" => int, "exampleIndex" => int, "error" => float, "synaptic_weights" => array]
public array $iterations, // ["epoch" => int, "exampleIndex" => int, "error" => float, "synaptic_weights" => array]
public string $sessionId,
public string $trainingId,
)

View File

@@ -8,6 +8,7 @@ use App\Models\SimpleBinaryPerceptronTraining;
use App\Services\DataSetReader;
use App\Services\ISynapticWeightsProvider;
use App\Services\PerceptronIterationEventBuffer;
use App\Services\PerceptronLimitedEpochEventBuffer;
use App\Services\ZeroSynapticWeights;
use Illuminate\Http\Request;
@@ -91,8 +92,15 @@ class PerceptronController extends Controller
case 'simple':
$dataset['defaultLearningRate'] = 0.015;
break;
case 'gradientdescent':
$dataset['defaultLearningRate'] = 0.001;
$dataset['defaultMinError'] = 2.0;
break;
}
break;
case 'table_2_11':
$dataset['defaultMinError'] = 1.0;
break;
}
$datasets[] = $dataset;
}
@@ -121,9 +129,14 @@ class PerceptronController extends Controller
$synapticWeightsProvider = new ZeroSynapticWeights();
}
$iterationEventBuffer = new PerceptronIterationEventBuffer($sessionId, $trainingId);
if ($maxIterations > config('perceptron.limited_broadcast_iterations')) {
$iterationsInterval = (int)($maxIterations / config('perceptron.limited_broadcast_iterations'));
$iterationEventBuffer = new PerceptronLimitedEpochEventBuffer($sessionId, $trainingId, $iterationsInterval);
}
$dataSetReader = $this->getDataSetReader($dataSet);
$iterationEventBuffer = new PerceptronIterationEventBuffer($sessionId, $trainingId);
$networkTraining = match ($perceptronType) {
'simple' => new SimpleBinaryPerceptronTraining($dataSetReader, $learningRate, $maxIterations, $synapticWeightsProvider, $iterationEventBuffer, $sessionId, $trainingId),

View File

@@ -4,6 +4,7 @@ namespace App\Models;
use App\Events\PerceptronTrainingEnded;
use App\Services\DataSetReader;
use App\Services\IPerceptronIterationEventBuffer;
use App\Services\ISynapticWeightsProvider;
use App\Services\PerceptronIterationEventBuffer;
@@ -18,36 +19,36 @@ class GradientDescentPerceptronTraining extends NetworkTraining
public function __construct(
DataSetReader $datasetReader,
protected float $learningRate,
int $maxIterations,
int $maxEpochs,
protected ISynapticWeightsProvider $synapticWeightsProvider,
PerceptronIterationEventBuffer $iterationEventBuffer,
IPerceptronIterationEventBuffer $iterationEventBuffer,
string $sessionId,
string $trainingId,
private float $minError,
) {
parent::__construct($datasetReader, $maxIterations, $iterationEventBuffer, $sessionId, $trainingId);
parent::__construct($datasetReader, $maxEpochs, $iterationEventBuffer, $sessionId, $trainingId);
$this->perceptron = new GradientDescentPerceptron($synapticWeightsProvider->generate($datasetReader->getInputSize()));
}
public function start(): void
{
$this->iteration = 0;
$this->epoch = 0;
do {
$this->epochError = 0;
$iterationErrorPerWeight = [];
$this->iteration++;
$epochCorrectorPerWeight = [];
$this->epoch++;
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()
$this->epochError += ($iterationError ** 2) / 2;
// 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;
$epochCorrectorPerWeight[$index][] = $iterationError * $input;
}
// Broadcast the training iteration event
@@ -57,14 +58,14 @@ class GradientDescentPerceptronTraining extends NetworkTraining
// 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]),
fn($weight, $weightIndex) => $weight + $this->learningRate * array_sum($epochCorrectorPerWeight[$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());
} while ($this->epoch < $this->maxEpochs && !$this->stopCondition());
$this->iterationEventBuffer->flush(); // Ensure all iterations are sent to the frontend

View File

@@ -4,11 +4,11 @@ namespace App\Models;
use App\Events\PerceptronTrainingEnded;
use App\Services\DataSetReader;
use App\Services\PerceptronIterationEventBuffer;
use App\Services\IPerceptronIterationEventBuffer;
abstract class NetworkTraining
{
protected int $iteration = 0;
protected int $epoch = 0;
/**
* @abstract
@@ -18,8 +18,8 @@ abstract class NetworkTraining
public function __construct(
protected DataSetReader $datasetReader,
protected int $maxIterations,
protected PerceptronIterationEventBuffer $iterationEventBuffer,
protected int $maxEpochs,
protected IPerceptronIterationEventBuffer $iterationEventBuffer,
protected string $sessionId,
protected string $trainingId,
) {
@@ -29,8 +29,8 @@ abstract class NetworkTraining
abstract protected function stopCondition(): bool;
protected function checkPassedMaxIterations(?float $finalError) {
if ($this->iteration >= $this->maxIterations) {
$message = 'Le nombre maximal d\'itérations a été atteint';
if ($this->epoch >= $this->maxEpochs) {
$message = 'Le nombre maximal d\'epoch a été atteint';
if ($finalError) {
$message .= " avec une erreur finale de $finalError";
}
@@ -40,6 +40,6 @@ abstract class NetworkTraining
}
protected function addIterationToBuffer(float $error, array $synapticWeights) {
$this->iterationEventBuffer->addIteration($this->iteration, $this->datasetReader->getLastReadLineIndex(), $error, $synapticWeights);
$this->iterationEventBuffer->addIteration($this->epoch, $this->datasetReader->getLastReadLineIndex(), $error, $synapticWeights);
}
}

View File

@@ -4,8 +4,8 @@ namespace App\Models;
use App\Events\PerceptronTrainingEnded;
use App\Services\DataSetReader;
use App\Services\IPerceptronIterationEventBuffer;
use App\Services\ISynapticWeightsProvider;
use App\Services\PerceptronIterationEventBuffer;
class SimpleBinaryPerceptronTraining extends NetworkTraining
{
@@ -19,25 +19,25 @@ class SimpleBinaryPerceptronTraining extends NetworkTraining
public function __construct(
DataSetReader $datasetReader,
protected float $learningRate,
int $maxIterations,
int $maxEpochs,
protected ISynapticWeightsProvider $synapticWeightsProvider,
PerceptronIterationEventBuffer $iterationEventBuffer,
IPerceptronIterationEventBuffer $iterationEventBuffer,
string $sessionId,
string $trainingId,
) {
parent::__construct($datasetReader, $maxIterations, $iterationEventBuffer, $sessionId, $trainingId);
parent::__construct($datasetReader, $maxEpochs, $iterationEventBuffer, $sessionId, $trainingId);
$this->perceptron = new SimpleBinaryPerceptron($synapticWeightsProvider->generate($datasetReader->getInputSize()));
}
public function start(): void
{
$this->iteration = 0;
$this->epoch = 0;
$error = 0;
do {
$this->iterationErrorCounter = 0;
$this->iteration++;
$this->epoch++;
while ($nextRow = $this->datasetReader->getRandomLine()) {
while ($nextRow = $this->datasetReader->getNextLine()) {
$inputs = array_slice($nextRow, 0, -1);
$correctOutput = (float) end($nextRow);
$correctOutput = $correctOutput > 0 ? 1 : 0; // Modify labels for non binary datasets
@@ -48,7 +48,7 @@ class SimpleBinaryPerceptronTraining extends NetworkTraining
$this->addIterationToBuffer($error, [[$this->perceptron->getSynapticWeights()]]);
}
$this->datasetReader->reset(); // Reset the dataset for the next iteration
} while ($this->iteration < $this->maxIterations && !$this->stopCondition());
} while ($this->epoch < $this->maxEpochs && !$this->stopCondition());
$this->iterationEventBuffer->flush(); // Ensure all iterations are sent to the frontend

View File

@@ -51,6 +51,16 @@ class DataSetReader {
return $randomLine;
}
public function getNextLine(): array | null {
if (!isset($this->currentLines[0])) {
return null; // No more lines to read
}
$this->lastReadLineIndex = array_search($this->currentLines[0], $this->lines, true);
return array_shift($this->currentLines);
}
public function getInputSize(): int
{
return count($this->lines[0]) - 1; // Don't count the label

View File

@@ -0,0 +1,10 @@
<?php
namespace App\Services;
interface IPerceptronIterationEventBuffer {
public function flush(): void ;
public function addIteration(int $iteration, int $exampleIndex, float $error, array $synaptic_weights): void ;
}

View File

@@ -2,15 +2,11 @@
namespace App\Services;
use Illuminate\Support\Facades\Log;
class PerceptronIterationEventBuffer {
class PerceptronIterationEventBuffer implements IPerceptronIterationEventBuffer {
private $data;
private int $nextSizeIncreaseThreshold;
private int $underSizeIncreaseCount = 0;
private int $MAX_SIZE = 50;
public function __construct(
private string $sessionId,
private string $trainingId,
@@ -26,9 +22,9 @@ class PerceptronIterationEventBuffer {
$this->data = [];
}
public function addIteration(int $iteration, int $exampleIndex, float $error, array $synaptic_weights): void {
public function addIteration(int $epoch, int $exampleIndex, float $error, array $synaptic_weights): void {
$this->data[] = [
"iteration" => $iteration,
"epoch" => $epoch,
"exampleIndex" => $exampleIndex,
"error" => $error,
"weights" => $synaptic_weights,
@@ -42,8 +38,8 @@ class PerceptronIterationEventBuffer {
$this->flush();
$this->nextSizeIncreaseThreshold *= $this->sizeIncreaseFactor;
if ($this->nextSizeIncreaseThreshold > $this->MAX_SIZE) {
$this->nextSizeIncreaseThreshold = $this->MAX_SIZE; // Cap the threshold to the maximum size
if ($this->nextSizeIncreaseThreshold > config('perceptron.broadcast_iteration_size')) {
$this->nextSizeIncreaseThreshold = config('perceptron.broadcast_iteration_size'); // Cap the threshold to the maximum size
}
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Services;
class PerceptronLimitedEpochEventBuffer implements IPerceptronIterationEventBuffer {
private array $data;
private int $underSizeIncreaseCount = 0;
public function __construct(
private string $sessionId,
private string $trainingId,
private int $epochInterval,
private int $sizeIncreaseStart = 10,
) {
$this->data = [];
}
public function flush(): void {
event(new \App\Events\PerceptronTrainingIteration($this->data, $this->sessionId, $this->trainingId));
$this->data = [];
}
public function addIteration(int $epoch, int $exampleIndex, float $error, array $synaptic_weights): void {
$newData = [
"epoch" => $epoch,
"exampleIndex" => $exampleIndex,
"error" => $error,
"weights" => $synaptic_weights,
];
if ($this->underSizeIncreaseCount <= $this->sizeIncreaseStart) { // Special case where we need to send each iteration separately
$this->underSizeIncreaseCount++;
$this->data[] = $newData;
$this->flush();
return;
}
$lastEpoch = $this->data[0]['epoch'] ?? null;
if ($this->data && $lastEpoch !== $epoch) { // Current Epoch has changed from the last one
if ($lastEpoch % $this->epochInterval === 0) { // The last epoch need to be sent
$this->flush(); // Flush all data from the previous epoch
}
else {
$this->data = [];
}
$lastEpoch = $epoch;
}
$this->data[] = $newData;
}
}

19
config/perceptron.php Normal file
View File

@@ -0,0 +1,19 @@
<?php
return [
/**
* Minimum number of iterations for which the broadcast of the training progress is allowed in full.
* Beyond this number of iterations, the broadcast will be splitted every x iterations,
* x is limited_broadcast_number
*/
'limited_broadcast_iterations' => 200,
/**
* How much broadcasts is sent when in limmited broadcast mode
*/
'limited_broadcast_number' => 200,
'broadcast_iteration_size' => 75,
];

View File

@@ -0,0 +1,4 @@
0, 0, -1
0, 1, 1
1, 0, 1
1, 1, 1
1 0 0 -1
2 0 1 1
3 1 0 1
4 1 1 1

View File

@@ -15,14 +15,29 @@ const allWeightPerIteration: ComputedRef<number[][]> = computed(() => {
return iteration.weights.flat(2);
});
});
const rowBgDark = computed(() => {
let isEven = false;
return props.iterations.map((iteration, index, arr) => {
if (index > 0 && arr[index - 1].epoch !== iteration.epoch) {
isEven = !isEven;
}
return isEven;
});
});
</script>
<template>
<table class="table w-full border-collapse border border-gray-300">
<tr class="text-left" v-if="props.iterations.length > 0">
<th>Itération</th>
<th>Époch</th>
<th>Exemple</th>
<th v-for="(weight, index) in allWeightPerIteration[allWeightPerIteration.length - 1]" v-bind:key="index">
<th
v-for="(weight, index) in allWeightPerIteration[
allWeightPerIteration.length - 1
]"
v-bind:key="index"
>
X<sub>{{ index }}</sub>
</th>
<th>Erreur</th>
@@ -31,12 +46,15 @@ const allWeightPerIteration: ComputedRef<number[][]> = computed(() => {
v-for="(iteration, index) in props.iterations"
v-bind:key="index"
:class="{
'bg-gray-900': iteration.iteration % 2 === 0,
'bg-gray-900': rowBgDark[index],
}"
>
<td>{{ iteration.iteration }}</td>
<td>{{ iteration.epoch }}</td>
<td>{{ iteration.exampleIndex }}</td>
<td v-for="(weight, index) in allWeightPerIteration[index]" v-bind:key="index">
<td
v-for="(weight, index) in allWeightPerIteration[index]"
v-bind:key="index"
>
{{ weight.toFixed(2) }}
</td>
<td>{{ iteration.error.toFixed(2) }}</td>

View File

@@ -46,7 +46,7 @@ function getPerceptronDecisionBoundaryDataset(
networkWeights[0].length == 1 &&
networkWeights[0][0].length == 3
) { // Unique, 3 weights perceptron
const perceptronWeights = networkWeights[0][0]; // We take the unique
const perceptronWeights = networkWeights[0][0]; // We take the unique perceptron
function perceptronLine(x: number): number {
// w0 + w1*x + w2*y = 0 => y = -(w1/w2)*x - w0/w2

View File

@@ -39,8 +39,8 @@ function getPerceptronErrorsPerIteration(): ChartData<
dataset.data.push(iteration.error);
// Epoch error
epochAverageError[iteration.iteration - 1] =
(epochAverageError[iteration.iteration - 1] || 0) +
epochAverageError[iteration.epoch - 1] =
(epochAverageError[iteration.epoch - 1] || 0) +
iteration.error ** 2 / 2;
});
@@ -81,7 +81,7 @@ function getPerceptronErrorsPerIteration(): ChartData<
plugins: {
title: {
display: true,
text: 'Nombre d\'erreurs par itération',
text: 'Nombre d\'erreurs par epoch',
},
},
scales: {
@@ -106,8 +106,8 @@ function getPerceptronErrorsPerIteration(): ChartData<
}"
:data="{
labels: props.iterations.reduce((labels, iteration) => {
if (!labels.includes(`Itération ${iteration.iteration}`)) {
labels.push(`Itération ${iteration.iteration}`);
if (!labels.includes(`Époch ${iteration.epoch}`)) {
labels.push(`Époch ${iteration.epoch}`);
}
return labels;
}, [] as string[]),

View File

@@ -47,11 +47,17 @@ watch(selectedDatasetCopy, (newvalue) => {
(dataset) => dataset.label === newvalue
) || null;
let defaultLearningRate = props.defaultLearningRate;
// LearningRate
learningRate.value = props.defaultLearningRate;
if (selectedDatasetCopy && selectedDatasetCopy.defaultLearningRate !== undefined) {
defaultLearningRate = selectedDatasetCopy.defaultLearningRate;
learningRate.value = selectedDatasetCopy.defaultLearningRate;
}
learningRate.value = defaultLearningRate;
// MinError
minError.value = props.minError;
if (selectedDatasetCopy && selectedDatasetCopy.defaultMinError !== undefined) {
minError.value = selectedDatasetCopy.defaultMinError;
}
// MaxIterations
maxIterations.value = props.defaultMaxIterations;
})

View File

@@ -1,7 +1,7 @@
import type { Point } from "chart.js";
export type Iteration = {
iteration: number;
epoch: number;
exampleIndex: number;
weights: number[][][];
error: number;
@@ -10,7 +10,8 @@ export type Iteration = {
export type Dataset = {
label: string;
data: DatasetPoint[];
defaultLearningRate: number | undefined;
defaultLearningRate?: number;
defaultMinError?: number;
};
export type DatasetPoint = {