1# Model Persistence
2Model 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](persistable.md) interface are able to have their internal state persisted between processes by a [Persister](persisters/api.md). In addition, the library provides the [Persistent Model](persistent-model.md) meta-estimator that acts as a wrapper for persistable estimators.
3
4## Persisters
5Persisters are objects that interface with your storage backend such as a filesystem or Redis database. They provide the `save()` and `load()` methods which take and receive persistable objects. In order to function properly, persisters must have read and write access to your storage system. In the example below, we'll use the [Filesystem](persisters/filesystem.md) persister to load a [Persistable](persistable.md) estimator from the filesystem and then save it after performing some task.
6
7```php
8use Rubix\ML\Persisters\Filesystem;
9
10$persister = new Filesystem('example.model');
11
12$estimator = $persister->load();
13
14// Do something
15
16$persister->save($estimator);
17```
18
19## Serialization
20Serialization occurs in between saving and loading a model and can be thought of as packaging the model's parameters. The data can be in byte-stream format such as with PHP's [Native](persisters/serializers/native.md) serializer or in a compressed byte-stream with integrity checks such as with the library's own [RBX](persisters/serializers/rbx.md) serializer. In the next example, we demonstrate how to replace the default serializer of the [Filesystem](persisters/filesystem.md) persister with the RBX format.
21
22```php
23use Rubix\ML\Persisters\Filesystem;
24use Rubix\ML\Persisters\Serializers\RBX;
25
26$persister = new Filesystem('example.rbx', true, new RBX());
27```
28
29!!! note
30    Due to a limitation in PHP, anonymous classes and functions (*closures*) are not able to be deserialized. Avoid adding anonymous classes or functions to an object that you intend to persist.
31
32## Persistent Model Meta-estimator
33The [Persistent Model](persistent-model.md) meta-estimator is a wrapper that uses the persistence subsystem under the hood. It provides the `save()` and `load()` methods that give the estimator the ability to save and load itself.
34
35```php
36use Rubix\ML\PersistentModel;
37use Rubix\ML\Persisters\Filesystem;
38
39$estimator = PersistentModel::load(new Filesystem('example.model'));
40
41// Do something
42
43$estimator->save();
44```
45
46## Caveats
47Since 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.
48