60 lines
1.9 KiB
PHP
60 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\Services;
|
|
|
|
use App\Events\PerceptronTrainingIteration;
|
|
use App\Services\IterationEventBuffer\PerceptronIterationEventBuffer;
|
|
use App\Services\IterationEventBuffer\PerceptronLimitedEpochEventBuffer;
|
|
use Illuminate\Support\Facades\Event;
|
|
use Tests\TestCase;
|
|
|
|
class IterationEventBufferTest extends TestCase
|
|
{
|
|
public function test_iterations_are_sent_as_a_single_batch(): void
|
|
{
|
|
Event::fake();
|
|
$buffer = new PerceptronIterationEventBuffer('session', 'training');
|
|
|
|
$buffer->addIteration(1, 0, 0.5, []);
|
|
$buffer->addIteration(1, 1, 0.25, []);
|
|
$buffer->flush();
|
|
|
|
Event::assertDispatched(PerceptronTrainingIteration::class, function (PerceptronTrainingIteration $event): bool {
|
|
return count($event->iterations) === 2;
|
|
});
|
|
}
|
|
|
|
public function test_limited_buffer_discards_non_selected_epochs(): void
|
|
{
|
|
Event::fake();
|
|
$buffer = new PerceptronLimitedEpochEventBuffer('session', 'training', 2);
|
|
|
|
$buffer->addIteration(1, 0, 0.5, []);
|
|
$buffer->addIteration(2, 0, 0.25, []);
|
|
$buffer->flush();
|
|
|
|
Event::assertDispatched(PerceptronTrainingIteration::class, function (PerceptronTrainingIteration $event): bool {
|
|
return count($event->iterations) === 1
|
|
&& $event->iterations[0]['epoch'] === 1;
|
|
});
|
|
}
|
|
|
|
public function test_non_finite_values_are_normalized_before_broadcasting(): void
|
|
{
|
|
$event = new PerceptronTrainingIteration([
|
|
[
|
|
'epoch' => 1,
|
|
'exampleIndex' => 0,
|
|
'error' => NAN,
|
|
'weights' => [[[INF]]],
|
|
],
|
|
], 'session', 'training');
|
|
|
|
$payload = $event->broadcastWith();
|
|
|
|
$this->assertNull($payload['iterations'][0]['error']);
|
|
$this->assertNull($payload['iterations'][0]['weights'][0][0][0]);
|
|
$this->assertJson(json_encode($payload, JSON_THROW_ON_ERROR));
|
|
}
|
|
}
|