-
Notifications
You must be signed in to change notification settings - Fork 244
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
[FEATURE] Discrete IQL #404
Open
Mamba413
wants to merge
3
commits into
takuseno:master
Choose a base branch
from
Mamba413:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -157,6 +157,116 @@ def q_function_optim(self) -> Optimizer: | |
return self._modules.critic_optim | ||
|
||
|
||
class DiscreteDDPGBaseImpl( | ||
ContinuousQFunctionMixin, QLearningAlgoImplBase, metaclass=ABCMeta | ||
Mamba413 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
): | ||
_modules: DDPGBaseModules | ||
_gamma: float | ||
_tau: float | ||
_q_func_forwarder: ContinuousEnsembleQFunctionForwarder | ||
_targ_q_func_forwarder: ContinuousEnsembleQFunctionForwarder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These need to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you means |
||
|
||
def __init__( | ||
self, | ||
observation_shape: Shape, | ||
action_size: int, | ||
modules: DDPGBaseModules, | ||
q_func_forwarder: ContinuousEnsembleQFunctionForwarder, | ||
targ_q_func_forwarder: ContinuousEnsembleQFunctionForwarder, | ||
gamma: float, | ||
tau: float, | ||
device: str, | ||
): | ||
super().__init__( | ||
observation_shape=observation_shape, | ||
action_size=action_size, | ||
modules=modules, | ||
device=device, | ||
) | ||
self._gamma = gamma | ||
self._tau = tau | ||
self._q_func_forwarder = q_func_forwarder | ||
self._targ_q_func_forwarder = targ_q_func_forwarder | ||
hard_sync(self._modules.targ_q_funcs, self._modules.q_funcs) | ||
|
||
def update_critic(self, batch: TorchMiniBatch) -> Dict[str, float]: | ||
self._modules.critic_optim.zero_grad() | ||
q_tpn = self.compute_target(batch) | ||
loss = self.compute_critic_loss(batch, q_tpn) | ||
loss.critic_loss.backward() | ||
self._modules.critic_optim.step() | ||
return asdict_as_float(loss) | ||
|
||
def compute_critic_loss( | ||
self, batch: TorchMiniBatch, q_tpn: torch.Tensor | ||
) -> DDPGBaseCriticLoss: | ||
loss = self._q_func_forwarder.compute_error( | ||
observations=batch.observations, | ||
actions=batch.actions, | ||
rewards=batch.rewards, | ||
target=q_tpn, | ||
terminals=batch.terminals, | ||
gamma=self._gamma**batch.intervals, | ||
) | ||
return DDPGBaseCriticLoss(loss) | ||
|
||
def update_actor( | ||
self, batch: TorchMiniBatch, action: ActionOutput | ||
) -> Dict[str, float]: | ||
# Q function should be inference mode for stability | ||
self._modules.q_funcs.eval() | ||
self._modules.actor_optim.zero_grad() | ||
loss = self.compute_actor_loss(batch, action) | ||
loss.actor_loss.backward() | ||
self._modules.actor_optim.step() | ||
return asdict_as_float(loss) | ||
|
||
def inner_update( | ||
self, batch: TorchMiniBatch, grad_step: int | ||
) -> Dict[str, float]: | ||
metrics = {} | ||
action = self._modules.policy(batch.observations) | ||
metrics.update(self.update_critic(batch)) | ||
metrics.update(self.update_actor(batch, action)) | ||
self.update_critic_target() | ||
return metrics | ||
|
||
@abstractmethod | ||
def compute_actor_loss( | ||
self, batch: TorchMiniBatch, action: ActionOutput | ||
) -> DDPGBaseActorLoss: | ||
pass | ||
|
||
@abstractmethod | ||
def compute_target(self, batch: TorchMiniBatch) -> torch.Tensor: | ||
pass | ||
|
||
def inner_predict_best_action(self, x: TorchObservation) -> torch.Tensor: | ||
return torch.argmax(self._modules.policy(x).probs).unsqueeze(0) | ||
|
||
@abstractmethod | ||
def inner_sample_action(self, x: TorchObservation) -> torch.Tensor: | ||
pass | ||
|
||
def update_critic_target(self) -> None: | ||
soft_sync(self._modules.targ_q_funcs, self._modules.q_funcs, self._tau) | ||
|
||
@property | ||
def policy(self) -> Policy: | ||
return self._modules.policy | ||
|
||
@property | ||
def policy_optim(self) -> Optimizer: | ||
return self._modules.actor_optim | ||
|
||
@property | ||
def q_function(self) -> nn.ModuleList: | ||
return self._modules.q_funcs | ||
|
||
@property | ||
def q_function_optim(self) -> Optimizer: | ||
return self._modules.critic_optim | ||
|
||
@dataclasses.dataclass(frozen=True) | ||
class DDPGModules(DDPGBaseModules): | ||
targ_policy: Policy | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove
q_func_factory
from config? Instead, please useMeanQFunctionFactory
just like the continuous IQL?