Model persistence is the ability to save and subsequently load a learner's state in another process. Trained estimators can be used for real-time inference by loading the model onto a server or they can be saved to make predictions offline at a later time. Estimators that implement the Persistable interface are able to have their internal state persisted between processes by a model Persister. In addition, the library provides the Persistent Model meta-estimator that acts as a wrapper for persistable estimators.
Persisters are objects that interface with your storage backend such as a filesystem or Redis database. They provide the
load() methods which take and return persistable objects respectively. In order to function properly, persisters must have both read and write access to your storage system.
In the example below, the Filesystem persister loads a persistable estimator from the filesystem, such as the system's local hard drive or network attached storage (NAS), and then saves it after performing some task.
use Rubix\ML\Persisters\Filesystem; $persister = new Filesystem('example.model'); $estimator = $persister->load(); // Do something $persister->save($estimator);
Model serialization occurs in between saving a loading and can be thought of as packaging a model's parameters into a single contiguous blob of data. The data can be in byte-stream format such as with PHP's Native serializer or in binary format as with the Igbinary serializer. Knowing the format that a model was serialized in will allow you to transport the model between systems.
In the next example, we demonstrate how to replace the default serializer of the Filesystem persister with Igbinary format.
use Rubix\ML\Persisters\Filesystem; use Rubix\ML\Persisters\Serializers\Igbinary; $persister = new Filesystem('example.model', true, new Igbinary());
Note: Due to a limitation in PHP, anonymous classes and functions (closures) are not able to be deserialized. If you add anonymous classes or functions to the model, they must be given formal definitions before they can be persisted.
Persistent Model Meta-estimator#
The Persistent Model meta-estimator is a wrapper that uses the persistence subsystem under the hood. It provides the
load() methods that give the estimator the ability to save and load itself.
use Rubix\ML\PersistentModel; use Rubix\ML\Persisters\Filesystem; $estimator = PersistentModel::load(new Filesystem('example.model')); // Do something $estimator->save();
Since model data are exported with the learner's current class definition in mind, problems may occur when loading a model using a different version of the library than the one it was trained and saved on. For example, when upgrading to a new version, there is a small chance that a previously saved learner may not be able to be deserialized if the model is not compatible with the learner's new class definition. For maximum interoperability, ensure that each system is running the same version of Rubix ML.