-
-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathPersistentModel.php
239 lines (212 loc) · 5.6 KB
/
PersistentModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
namespace Rubix\ML;
use Rubix\ML\Helpers\Params;
use Rubix\ML\Serializers\RBX;
use Rubix\ML\Datasets\Dataset;
use Rubix\ML\Persisters\Persister;
use Rubix\ML\Serializers\Serializer;
use Rubix\ML\AnomalyDetectors\Scoring;
use Rubix\ML\Exceptions\InvalidArgumentException;
use Rubix\ML\Exceptions\RuntimeException;
/**
* Persistent Model
*
* The Persistent Model wrapper gives the estimator two additional methods (`save()`
* and `load()`) that allow the estimator to be saved and retrieved from storage.
*
* @category Machine Learning
* @package Rubix/ML
* @author Andrew DalPino
*/
class PersistentModel implements EstimatorWrapper, Learner, Probabilistic, Scoring
{
/**
* The persistable base learner.
*
* @var Learner
*/
protected Learner $base;
/**
* The persister used to interface with the storage layer.
*
* @var Persister
*/
protected Persister $persister;
/**
* The object serializer.
*
* @var Serializer
*/
protected Serializer $serializer;
/**
* Factory method to restore the model from persistence.
*
* @param Persister $persister
* @param Serializer|null $serializer
* @throws InvalidArgumentException
* @return self
*/
public static function load(Persister $persister, ?Serializer $serializer = null) : self
{
$serializer = $serializer ?? new RBX();
$base = $serializer->deserialize($persister->load());
if (!$base instanceof Learner) {
throw new InvalidArgumentException('Persistable must'
. ' implement the Learner interface.');
}
return new self($base, $persister, $serializer);
}
/**
* @param Learner $base
* @param Persister $persister
* @param Serializer|null $serializer
* @throws InvalidArgumentException
*/
public function __construct(Learner $base, Persister $persister, ?Serializer $serializer = null)
{
if (!$base instanceof Persistable) {
throw new InvalidArgumentException('Base Learner must'
. ' implement the Persistable interface.');
}
$this->base = $base;
$this->persister = $persister;
$this->serializer = $serializer ?? new RBX();
}
/**
* Return the estimator type.
*
* @internal
*
* @return EstimatorType
*/
public function type() : EstimatorType
{
return $this->base->type();
}
/**
* Return the data types that the estimator is compatible with.
*
* @internal
*
* @return list<DataType>
*/
public function compatibility() : array
{
return $this->base->compatibility();
}
/**
* Return the settings of the hyper-parameters in an associative array.
*
* @internal
*
* @return mixed[]
*/
public function params() : array
{
return [
'base' => $this->base,
'persister' => $this->persister,
'serializer' => $this->serializer,
];
}
/**
* Has the learner been trained?
*
* @return bool
*/
public function trained() : bool
{
return $this->base->trained();
}
/**
* Return the base estimator instance.
*
* @return Estimator
*/
public function base() : Estimator
{
return $this->base;
}
/**
* Save the model to storage.
*/
public function save() : void
{
if (!$this->base instanceof Persistable) {
throw new RuntimeException('Base estimator is not persistable.');
}
$encoding = $this->serializer->serialize($this->base);
$this->persister->save($encoding);
}
/**
* Train the learner with a dataset.
*
* @param Dataset $dataset
*/
public function train(Dataset $dataset) : void
{
$this->base->train($dataset);
}
/**
* Make a prediction on a given sample dataset.
*
* @param Dataset $dataset
* @return mixed[]
*/
public function predict(Dataset $dataset) : array
{
return $this->base->predict($dataset);
}
/**
* Estimate the joint probabilities for each possible outcome.
*
* @param Dataset $dataset
* @throws RuntimeException
* @return list<float[]>
*/
public function proba(Dataset $dataset) : array
{
if (!$this->base instanceof Probabilistic) {
throw new RuntimeException('Base Estimator must'
. ' implement the Probabilistic interface.');
}
return $this->base->proba($dataset);
}
/**
* Return the anomaly scores assigned to the samples in a dataset.
*
* @param Dataset $dataset
* @throws RuntimeException
* @return float[]
*/
public function score(Dataset $dataset) : array
{
if (!$this->base instanceof Scoring) {
throw new RuntimeException('Base Estimator must'
. ' implement the Scoring interface.');
}
return $this->base->score($dataset);
}
/**
* Allow methods to be called on the model from the wrapper.
*
* @param string $name
* @param mixed[] $arguments
* @return mixed
*/
public function __call(string $name, array $arguments)
{
return $this->base->$name(...$arguments);
}
/**
* Return the string representation of the object.
*
* @internal
*
* @return string
*/
public function __toString() : string
{
return 'Persistent Model (' . Params::stringify($this->params()) . ')';
}
}