Refactored into folders

This commit is contained in:
2026-03-22 10:42:08 +01:00
parent 977c259cb9
commit 4717254da4
18 changed files with 258 additions and 44 deletions

View File

@@ -0,0 +1,46 @@
<?php
namespace App\Services\IterationEventBuffer;
class PerceptronIterationEventBuffer implements IPerceptronIterationEventBuffer {
private $data;
private int $nextSizeIncreaseThreshold;
private int $underSizeIncreaseCount = 0;
public function __construct(
private string $sessionId,
private string $trainingId,
private int $sizeIncreaseStart = 10,
private int $sizeIncreaseFactor = 2,
) {
$this->data = [];
$this->nextSizeIncreaseThreshold = $sizeIncreaseStart;
}
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 {
$this->data[] = [
"epoch" => $epoch,
"exampleIndex" => $exampleIndex,
"error" => $error,
"weights" => $synaptic_weights,
];
if ($this->underSizeIncreaseCount <= $this->sizeIncreaseStart) { // We can still send a single date because we are under the increase start threshold
$this->underSizeIncreaseCount++;
$this->flush();
}
else if (count($this->data) >= $this->nextSizeIncreaseThreshold) {
$this->flush();
$this->nextSizeIncreaseThreshold *= $this->sizeIncreaseFactor;
if ($this->nextSizeIncreaseThreshold > config('perceptron.broadcast_iteration_size')) {
$this->nextSizeIncreaseThreshold = config('perceptron.broadcast_iteration_size'); // Cap the threshold to the maximum size
}
}
}
}