Skip to main content

load

load(f, model, /, *, likelihood=None, map_location=None)

Read a file written by save, attaching model.

model.load_state_dict(torch.load("model.pt"))
post = lk.load("post.pt", model)

Pass the same kind of model as the fit: a module for lk.laplace(module, ...), or the ModelFn the fit took (such as lk.transformers.causal_lm(model)). A posterior over a Selection gets its scattering model back from the saved indices. The model must be the saved one: the same parameters and buffers, with the same shapes, dtypes and values, as load_state_dict(strict=True) checks a module.

Parameters

Name Type Description Default
f str | os.PathLike[str] | IO[bytes] A path or a binary file object. required
model nn.Module | ModelFn The model, a module or a ModelFn. required
likelihood Any The likelihood to use instead of the saved one, of the same class and with the same tensors (a tuned sigma, say). Needed when the saved likelihood held a function that save could not write (a lambda as mask_fn, say). None
map_location torch.device | str | None The device of the loaded tensors. None means the device of the model’s first parameter (the CPU when it has none). None

Returns

Name Type Description
Any The saved object, on model. A posterior records the model’s current version counters, so later in-place changes are caught.

Raises

Name Type Description
ValueError The file is not a save file.
StructureError The file is malformed, or names a class or function outside laplacekit and its registered plugins; the model differs from the saved one (a parameter or buffer missing, added, or of another shape, dtype or value), or is of another kind than the fit’s (the plain module for a fit that took lk.transformers.sequence_classification(model), say); the saved likelihood lacks a function and no likelihood is given; or likelihood is of another class or has other tensors than the saved one.