Add MNIST dataset example (#14)
* Update php-ml to 0.7.0 * Add MNIST example
This commit is contained in:
1 parent
6ecc876c8c
commit
191f03de4b
6 files changed
+74
-12
No files matched your search
@@ -2,3 +2,8 @@
|
||||
humbuglog.*
|
||||
/bin/phpunit
|
||||
.php_cs.cache
|
||||
/data/mnist-svm-model.phpml
|
||||
/data/train-images-idx3-ubyte
|
||||
/data/train-labels-idx1-ubyte
|
||||
/data/t10k-images-idx3-ubyte
|
||||
/data/t10k-labels-idx1-ubyte
|
||||
@@ -19,6 +19,7 @@ php classification/languageDetection.php
|
||||
Classification:
|
||||
|
||||
* `languageDetection.php` - classifier build for language detection
|
||||
* `minst.php` - recognize handwritten digits from MNIST dataset (to download dataset use `bin/download-mnist.sh`)
|
||||
|
||||
Regression:
|
||||
|
||||
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
echo "Start download MNIST dataset"
|
||||
wget --directory-prefix=data http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz
|
||||
gzip -d data/train-images-idx3-ubyte.gz
|
||||
|
||||
wget --directory-prefix=data http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz
|
||||
gzip -d data/train-labels-idx1-ubyte.gz
|
||||
|
||||
wget --directory-prefix=data http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz
|
||||
gzip -d data/t10k-images-idx3-ubyte.gz
|
||||
|
||||
wget --directory-prefix=data http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz
|
||||
gzip -d data/t10k-labels-idx1-ubyte.gz
|
||||
echo "Downloaded MNIST dataset to data/ dir"
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace PhpmlExamples;
|
||||
|
||||
use Phpml\Classification\SVC;
|
||||
use Phpml\Dataset\MnistDataset;
|
||||
use Phpml\Metric\Accuracy;
|
||||
use Phpml\ModelManager;
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
function convert($size)
|
||||
{
|
||||
$unit=array('b','kb','mb','gb','tb','pb');
|
||||
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
|
||||
}
|
||||
|
||||
$start = $loadStart = microtime(true);
|
||||
$trainDataset = new MnistDataset(__DIR__.'/../data/train-images-idx3-ubyte', __DIR__.'/../data/train-labels-idx1-ubyte');
|
||||
$testDataset = new MnistDataset(__DIR__.'/../data/t10k-images-idx3-ubyte', __DIR__.'/../data/t10k-labels-idx1-ubyte');
|
||||
|
||||
echo sprintf('Load time: %s'.PHP_EOL, microtime(true) - $loadStart);
|
||||
|
||||
$trainStart = microtime(true);
|
||||
$classifier = new SVC();
|
||||
$classifier->train($trainDataset->getSamples(), $trainDataset->getTargets());
|
||||
|
||||
echo sprintf('Train time: %s'.PHP_EOL, microtime(true) - $trainStart);
|
||||
|
||||
$modelStart = microtime(true);
|
||||
$modelManager = new ModelManager();
|
||||
$modelManager->saveToFile($classifier, __DIR__.'/../data/mnist-svm-model.phpml');
|
||||
echo sprintf('Persist time: %s'.PHP_EOL, microtime(true) - $modelStart);
|
||||
|
||||
$predictStart = microtime(true);
|
||||
$predicted = $classifier->predict($testDataset->getSamples());
|
||||
echo sprintf('Predict time: %s'.PHP_EOL, microtime(true) - $predictStart);
|
||||
|
||||
echo sprintf('Accuracy: %s', Accuracy::score($testDataset->getTargets(), $predicted));
|
||||
echo sprintf('Total time: %s'.PHP_EOL, microtime(true) - $start);
|
||||
echo sprintf('Memory: %s', convert(memory_get_peak_usage(true)));
|
||||
+1
-1
@@ -17,6 +17,6 @@
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php-ai/php-ml": "0.6.2"
|
||||
"php-ai/php-ml": "0.7.0"
|
||||
}
|
||||
}
|
||||
Generated
+12
-11
@@ -4,32 +4,33 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "5c0b77c2b5992a99ba68384b2caaa5d6",
|
||||
"content-hash": "51744d449f8402679eea8de2bde1b87b",
|
||||
"packages": [
|
||||
{
|
||||
"name": "php-ai/php-ml",
|
||||
"version": "0.6.2",
|
||||
"version": "0.7.0",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/php-ai/php-ml.git",
|
||||
"reference": "83f3e8de7021d58297f5357cfeb293a347a2fa39"
|
||||
"reference": "db95db3e57475df53d602b32c4451438c21e46c3"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/php-ai/php-ml/zipball/83f3e8de7021d58297f5357cfeb293a347a2fa39",
|
||||
"reference": "83f3e8de7021d58297f5357cfeb293a347a2fa39",
|
||||
"url": "https://api.github.com/repos/php-ai/php-ml/zipball/db95db3e57475df53d602b32c4451438c21e46c3",
|
||||
"reference": "db95db3e57475df53d602b32c4451438c21e46c3",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": "^7.1"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpstan/phpstan-phpunit": "^0.9.4",
|
||||
"phpstan/phpstan-shim": "^0.9",
|
||||
"phpstan/phpstan-strict-rules": "^0.9.0",
|
||||
"phpbench/phpbench": "^0.14.0",
|
||||
"phpstan/phpstan-phpunit": "^0.10",
|
||||
"phpstan/phpstan-shim": "^0.10",
|
||||
"phpstan/phpstan-strict-rules": "^0.10",
|
||||
"phpunit/phpunit": "^7.0.0",
|
||||
"symplify/coding-standard": "^3.1",
|
||||
"symplify/easy-coding-standard": "^3.1"
|
||||
"symplify/coding-standard": "^5.1",
|
||||
"symplify/easy-coding-standard": "^5.1"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
@@ -58,7 +59,7 @@
|
||||
"machine learning",
|
||||
"pattern recognition"
|
||||
],
|
||||
"time": "2018-02-22T16:06:31+00:00"
|
||||
"time": "2018-11-07T18:40:31+00:00"
|
||||
}
|
||||
],
|
||||
"packages-dev": [],
|
||||
|
||||
Reference in new issue
Block a user