|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Rubix\ML\Benchmarks\Persisters\Serializers; |
| 4 | + |
| 5 | +use Rubix\ML\Datasets\Generators\Blob; |
| 6 | +use Rubix\ML\Classifiers\KNearestNeighbors; |
| 7 | +use Rubix\ML\Datasets\Generators\Agglomerate; |
| 8 | +use Rubix\ML\Persisters\Serializers\RBXE; |
| 9 | + |
| 10 | +/** |
| 11 | + * @Groups({"Serializers"}) |
| 12 | + * @BeforeMethods({"setUp"}) |
| 13 | + */ |
| 14 | +class RBXEBench |
| 15 | +{ |
| 16 | + protected const TRAINING_SIZE = 2500; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var \Rubix\ML\Persisters\Serializers\RBXE |
| 20 | + */ |
| 21 | + protected $serializer; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var \Rubix\ML\Persistable |
| 25 | + */ |
| 26 | + protected $persistable; |
| 27 | + |
| 28 | + public function setUp() : void |
| 29 | + { |
| 30 | + $generator = new Agglomerate([ |
| 31 | + 'Iris-setosa' => new Blob([5.0, 3.42, 1.46, 0.24], [0.35, 0.38, 0.17, 0.1]), |
| 32 | + 'Iris-versicolor' => new Blob([5.94, 2.77, 4.26, 1.33], [0.51, 0.31, 0.47, 0.2]), |
| 33 | + 'Iris-virginica' => new Blob([6.59, 2.97, 5.55, 2.03], [0.63, 0.32, 0.55, 0.27]), |
| 34 | + ]); |
| 35 | + |
| 36 | + $training = $generator->generate(self::TRAINING_SIZE); |
| 37 | + |
| 38 | + $estimator = new KNearestNeighbors(5, true); |
| 39 | + |
| 40 | + $estimator->train($training); |
| 41 | + |
| 42 | + $this->persistable = $estimator; |
| 43 | + |
| 44 | + $this->serializer = new RBXE('secret'); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @Subject |
| 49 | + * @revs(10) |
| 50 | + * @Iterations(5) |
| 51 | + * @OutputTimeUnit("milliseconds", precision=3) |
| 52 | + */ |
| 53 | + public function serializeUnserialize() : void |
| 54 | + { |
| 55 | + $encoding = $this->serializer->serialize($this->persistable); |
| 56 | + |
| 57 | + $persistable = $this->serializer->unserialize($encoding); |
| 58 | + } |
| 59 | +} |
0 commit comments