Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions deep_river/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,13 +512,7 @@ def save(self, filepath: Union[str, Path]) -> None:
filepath = Path(filepath)
filepath.parent.mkdir(parents=True, exist_ok=True)

state = {
"estimator_class": f"{type(self).__module__}.{type(self).__name__}",
"init_params": self._get_all_init_params(),
"model_state_dict": getattr(self.module, "state_dict", lambda: {})(),
"optimizer_state_dict": getattr(self.optimizer, "state_dict", lambda: {})(),
"runtime_state": self._get_runtime_state(),
}
state = {"estimator": self}

with open(filepath, "wb") as f:
pickle.dump(state, f)
Expand All @@ -533,6 +527,14 @@ def load(cls, filepath: Union[str, Path]):
with open(filepath, "rb") as f:
state = pickle.load(f)

if "estimator" in state:
estimator = state["estimator"]
if not isinstance(estimator, cls):
raise TypeError(
f"Saved estimator is {type(estimator).__name__}, not {cls.__name__}"
)
return estimator

estimator_cls = cls._import_from_path(state["estimator_class"])
init_params = state["init_params"]

Expand Down
Loading