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, ]; $lastEpoch = $this->data[0]['epoch'] ?? null; if ($this->data && $lastEpoch !== $epoch) { // Current Epoch has changed from the last one if ($lastEpoch == 1 || $lastEpoch % $this->epochInterval === 0) { // The last saved epoch need to be sent $this->flush(); // Flush all data from the previous epoch } else { $this->data = []; // We clear the data without sending it as we are saving the next epoch data } $lastEpoch = $epoch; } $this->data[] = $newData; } }