51 lines
1.6 KiB
PHP
51 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class PerceptronIterationEventBuffer {
|
|
private $data;
|
|
private int $nextSizeIncreaseThreshold;
|
|
private int $underSizeIncreaseCount = 0;
|
|
|
|
private int $MAX_SIZE = 50;
|
|
|
|
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 $iteration, int $exampleIndex, float $error, array $synaptic_weights): void {
|
|
$this->data[] = [
|
|
"iteration" => $iteration,
|
|
"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 > $this->MAX_SIZE) {
|
|
$this->nextSizeIncreaseThreshold = $this->MAX_SIZE; // Cap the threshold to the maximum size
|
|
}
|
|
}
|
|
}
|
|
}
|