Added Limited Epoch Event Buffer
for better frontend performance when using big max epoch number
This commit is contained in:
@@ -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
|
||||
|
||||
10
app/Services/IPerceptronIterationEventBuffer.php
Normal file
10
app/Services/IPerceptronIterationEventBuffer.php
Normal 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 ;
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
51
app/Services/PerceptronLimitedEpochEventBuffer.php
Normal file
51
app/Services/PerceptronLimitedEpochEventBuffer.php
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user