forked from jorgecasas/php-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Persistence class to save and restore models (#37)
* Models manager with save/restore capabilities * Refactoring dataset exceptions * Persistency layer docs * New tests for serializable estimators * ModelManager static methods to instance methods
- Loading branch information
Showing
17 changed files
with
361 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# Persistency | ||
|
||
You can save trained models for future use. Persistency across requests achieved by saving and restoring serialized estimators into files. | ||
|
||
### Example | ||
|
||
``` | ||
use Phpml\Classification\KNearestNeighbors; | ||
use Phpml\ModelManager; | ||
$samples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]]; | ||
$labels = ['a', 'a', 'a', 'b', 'b', 'b']; | ||
$classifier = new KNearestNeighbors(); | ||
$classifier->train($samples, $labels); | ||
$filepath = '/path/to/store/the/model'; | ||
$modelManager = new ModelManager(); | ||
$modelManager->saveToFile($classifier, $filepath); | ||
$restoredClassifier = $modelManager->restoreFromFile($filepath); | ||
$restoredClassifier->predict([3, 2]); | ||
// return 'b' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phpml\Exception; | ||
|
||
class FileException extends \Exception | ||
{ | ||
|
||
/** | ||
* @param string $filepath | ||
* | ||
* @return FileException | ||
*/ | ||
public static function missingFile(string $filepath) | ||
{ | ||
return new self(sprintf('File "%s" missing.', $filepath)); | ||
} | ||
|
||
/** | ||
* @param string $filepath | ||
* | ||
* @return FileException | ||
*/ | ||
public static function cantOpenFile(string $filepath) | ||
{ | ||
return new self(sprintf('File "%s" can\'t be open.', $filepath)); | ||
} | ||
|
||
/** | ||
* @param string $filepath | ||
* | ||
* @return FileException | ||
*/ | ||
public static function cantSaveFile(string $filepath) | ||
{ | ||
return new self(sprintf('File "%s" can\'t be saved.', $filepath)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phpml\Exception; | ||
|
||
class SerializeException extends \Exception | ||
{ | ||
|
||
/** | ||
* @param string $filepath | ||
* | ||
* @return SerializeException | ||
*/ | ||
public static function cantUnserialize(string $filepath) | ||
{ | ||
return new self(sprintf('"%s" can not be unserialized.', $filepath)); | ||
} | ||
|
||
/** | ||
* @param string $classname | ||
* | ||
* @return SerializeException | ||
*/ | ||
public static function cantSerialize(string $classname) | ||
{ | ||
return new self(sprintf('Class "%s" can not be serialized.', $classname)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Phpml; | ||
|
||
use Phpml\Estimator; | ||
use Phpml\Exception\SerializeException; | ||
use Phpml\Exception\FileException; | ||
|
||
class ModelManager | ||
{ | ||
/** | ||
* @param Estimator $object | ||
* @param string $filepath | ||
*/ | ||
public function saveToFile(Estimator $object, string $filepath) | ||
{ | ||
if (!file_exists($filepath) || !is_writable(dirname($filepath))) { | ||
throw FileException::cantSaveFile(basename($filepath)); | ||
} | ||
|
||
$serialized = serialize($object); | ||
if (empty($serialized)) { | ||
throw SerializeException::cantSerialize(get_type($object)); | ||
} | ||
|
||
$result = file_put_contents($filepath, $serialized, LOCK_EX); | ||
if ($result === false) { | ||
throw FileException::cantSaveFile(basename($filepath)); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $filepath | ||
* | ||
* @return Estimator | ||
*/ | ||
public function restoreFromFile(string $filepath) | ||
{ | ||
if (!file_exists($filepath) || !is_readable($filepath)) { | ||
throw FileException::cantOpenFile(basename($filepath)); | ||
} | ||
|
||
$object = unserialize(file_get_contents($filepath)); | ||
if ($object === false) { | ||
throw SerializeException::cantUnserialize(basename($filepath)); | ||
} | ||
|
||
return $object; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.