Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix model loading with OptimizerWrapper #430

Merged
merged 1 commit into from
Nov 6, 2024
Merged
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
11 changes: 9 additions & 2 deletions d3rlpy/optimizers/optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from torch.optim import SGD, Adam, AdamW, Optimizer, RMSprop
from torch.optim.lr_scheduler import LRScheduler

from ..logging import LOG
from ..serializable_config import DynamicConfig, generate_config_registration
from .lr_schedulers import LRSchedulerFactory, make_lr_scheduler_field

Expand Down Expand Up @@ -102,9 +103,15 @@ def state_dict(self) -> Mapping[str, Any]:
}

def load_state_dict(self, state_dict: Mapping[str, Any]) -> None:
self._optim.load_state_dict(state_dict["optim"])
if "optim" in state_dict:
self._optim.load_state_dict(state_dict["optim"])
else:
LOG.warning("Skip loading optimizer state.")
if self._lr_scheduler:
self._lr_scheduler.load_state_dict(state_dict["lr_scheduler"])
if "lr_scheduler" in state_dict:
self._lr_scheduler.load_state_dict(state_dict["lr_scheduler"])
else:
LOG.warning("Skip loading lr scheduler state.")


@dataclasses.dataclass()
Expand Down
Loading