46 lines
1.4 KiB
PHP
46 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Events\PerceptronTrainingEnded;
|
|
use App\Services\DataSetReader;
|
|
use App\Services\PerceptronIterationEventBuffer;
|
|
|
|
abstract class NetworkTraining
|
|
{
|
|
protected int $iteration = 0;
|
|
|
|
/**
|
|
* @abstract
|
|
* @var ActivationsFunctions
|
|
*/
|
|
public ActivationsFunctions $activationFunction;
|
|
|
|
public function __construct(
|
|
protected DataSetReader $datasetReader,
|
|
protected int $maxIterations,
|
|
protected PerceptronIterationEventBuffer $iterationEventBuffer,
|
|
protected string $sessionId,
|
|
protected string $trainingId,
|
|
) {
|
|
}
|
|
|
|
abstract public function start() : void;
|
|
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 ($finalError) {
|
|
$message .= " avec une erreur finale de $finalError";
|
|
}
|
|
|
|
event(new PerceptronTrainingEnded($message, $this->sessionId, $this->trainingId));
|
|
}
|
|
}
|
|
|
|
protected function addIterationToBuffer(float $error, array $synapticWeights) {
|
|
$this->iterationEventBuffer->addIteration($this->iteration, $this->datasetReader->getLastReadLineIndex(), $error, $synapticWeights);
|
|
}
|
|
}
|