rheon.polars.ModelBank.load¶
- classmethod ModelBank.load(source: str | PathLike[str] | bytes | IO[bytes], specs: Iterable[dict[str, Any]] | None = None, *, threads: int | None = None) ModelBank[source]¶
Load a bank from a state file
save()wrote, on this OS or any other.Use it to resume a saved bank, or to serve predictions from it with
predict(). The file carries the bank’s specs, so you need not give them. For bytes from a store that is not a file, this call takes them as well, asload_bytes()does.Parameters
sourceA path, the bytes
save_bytes()gave, or a file object opened for binary reading, such asopen(..., "rb")or anio.BytesIO, as Polars’read_*functions take any of the three.specsThe specs you expect the file to hold. Given, the load is refused unless they are the file’s, which is how a job that resumes checks that the state it found belongs to the bank it is about to run.
None, the default, takes the file’s specs unchecked.threadsThe loaded bank’s
threads, asModelBanktakes it. The file carries no count, so a bank saved on eight threads and loaded with none runs on the process’s shared threads, with the same numbers.
Returns
A
ModelBankholding the saved state, which goes on from the row after the last one it learned before the save.Raises
OSErrorThe file cannot be read, such as
FileNotFoundErrorfor a path that does not exist.TypeErrorsourceis not a path, bytes or a file object, or the file object gives text rather than bytes.threadsis not an integer.ValueErrorThe file can be read but is not a bank this build loads:
it is not a bank’s state file, or it is damaged in a way the checks find (see Notes);
a newer build wrote it, under a format or state version above this build’s;
a build of 0.13.0 or earlier wrote it, below the oldest state version this build loads, which is 1.0’s (
rheon.schema_version()gives the current one): fit the bank again from its input;a state contradicts its own spec, such as an
sgdstate without the scaler itsstandardizeneeds;specsare given and differ from the file’s;threadsis below 1, or above 1024 (or the machine’s core count, where that is larger).
UnstableWarningUnder
RHEON_WARN_UNSTABLE=1, for a file holding a formula target, assave()says.
Examples
This code reads
dfandtodayfrom Example data:spec = rp.spec.ewridge("ridge", targets=["y"], features=["x0", "x1"], half_life=100.0) saved = rp.ModelBank([spec]) saved.fit_predict(df) saved.save("bank.state") bank = rp.ModelBank.load("bank.state", specs=[spec]) # refused if the specs differ bank.fit_predict(today) # and the stream goes on print(bank.rows_fed)
800
See also
save(), which writes the state file.skip_learned(), for input that overlaps what the state has learned.
Notes
The file carries no checksum. The load checks the file’s envelope (its magic number, its format and state versions, its number of states) and each state’s shape against its spec, every vector at the length its spec gives it. A damaged file that still decodes and passes those checks loads: a flipped bit in a stored number is a slightly different state, consistent with itself, and the model goes on from it. Keep a state file where it cannot be damaged, or keep a checksum beside it.