Files
Ninluc 25b03fc39a
linter / quality (push) Failing after 1m11s
tests / ci (8.3) (push) Successful in 3m55s
Cancel Training
2026-09-18 23:08:19 +02:00

75 lines
2.2 KiB
PHP

<?php
namespace App\Models\NetworksTraining;
use App\Events\PerceptronTrainingEnded;
use App\Exceptions\TrainingCancelledException;
use App\Models\ActivationsFunctions;
use App\Services\DatasetReader\IDataSetReader;
use App\Services\IterationEventBuffer\IPerceptronIterationEventBuffer;
use Closure;
abstract class NetworkTraining
{
protected int $epoch = 0;
/**
* @abstract
*/
public ActivationsFunctions $activationFunction;
public ?ActivationsFunctions $presentationLayerActivationFunction = null;
public function __construct(
protected IDataSetReader $datasetReader,
protected int $maxEpochs,
protected IPerceptronIterationEventBuffer $iterationEventBuffer,
protected string $sessionId,
protected string $trainingId,
protected ?Closure $isCancelled = null,
) {}
abstract public function start(): void;
abstract protected function stopCondition(): bool;
protected function checkPassedMaxIterations(?float $finalError)
{
if ($this->epoch >= $this->maxEpochs) {
$message = 'Le nombre maximal d\'époques a été atteint';
if ($finalError) {
$message .= " avec une erreur finale de $finalError";
}
event(new PerceptronTrainingEnded($message, $this->sessionId, $this->trainingId));
}
}
protected function broadcastTrainingEnded(string $reason): void
{
$this->iterationEventBuffer->flush();
event(new PerceptronTrainingEnded($reason, $this->sessionId, $this->trainingId));
}
protected function addIterationToBuffer(float $error, array $synapticWeights)
{
if ($this->isCancelled !== null && ($this->isCancelled)()) {
throw new TrainingCancelledException;
}
$this->iterationEventBuffer->addIteration($this->epoch, $this->datasetReader->getLastReadLineIndex(), $error, $synapticWeights);
}
public function cancel(): void
{
$this->broadcastTrainingEnded('Entraînement annulé');
}
public function getEpoch(): int
{
return $this->epoch;
}
abstract public function getSynapticWeights(): array;
}