-
Notifications
You must be signed in to change notification settings - Fork 1
Sac fix #96
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
Merged
Sac fix #96
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
488b439
updated sac to test if it works
amsks 707b280
update
amsks 979742e
update
amsks 484d1f2
SAC updates
amsks c667d0a
updated code + tests
amsks ade9d40
removed FIX comments
amsks c4a6d81
updates for Merge
amsks a47ace9
removed instance comparisons in stochastic and exploration policies
amsks 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -27,6 +27,9 @@ def __init__( | |
| :param entropy_coefficient: weight on entropy term | ||
| :param discrete: whether the action space is discrete | ||
| """ | ||
|
|
||
| self.model = model | ||
|
|
||
| super().__init__(algo, model, discrete) | ||
| self.entropy_coefficient = entropy_coefficient | ||
| self.discrete = discrete | ||
|
|
@@ -84,33 +87,24 @@ def explore(self, s, return_logp, metrics=None) -> Tuple[np.ndarray, torch.Tenso | |
| # 4-tuple case (Tanh squashing): (action, z, mean, log_std) | ||
| elif isinstance(model_output, tuple) and len(model_output) == 4: | ||
| action, z, mean, log_std = model_output | ||
| log_prob = sample_nondeterministic_logprobs( | ||
|
Contributor
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. I don't understand the reason for changing this, it's the same code but longer and locking into a specific model class? |
||
| z=z, | ||
| mean=mean, | ||
| log_std=log_std, | ||
| sac=self.algo == "sac", | ||
| ) | ||
|
|
||
| if not self.algo == "sac": | ||
|
|
||
| log_prob = sample_nondeterministic_logprobs( | ||
| z=z, | ||
| mean=mean, | ||
| log_std=log_std, | ||
| sac=False, | ||
| ) | ||
| else: | ||
| log_prob = self.model.policy_log_prob(z, mean, log_std) | ||
|
|
||
| if return_logp: | ||
| return action.detach().cpu().numpy(), log_prob | ||
| else: | ||
| weighted_log_prob = log_prob * self.entropy_coefficient | ||
| return action.detach().cpu().numpy(), weighted_log_prob | ||
|
|
||
| # Legacy 2-tuple case: (mean, std) | ||
| elif isinstance(model_output, tuple) and len(model_output) == 2: | ||
| mean, std = model_output | ||
| dist = Normal(mean, std) | ||
| z = dist.rsample() # [batch, action_dim] | ||
| action = torch.tanh(z) # [batch, action_dim] | ||
|
|
||
| log_prob = sample_nondeterministic_logprobs( | ||
| z=z, mean=mean, log_std=torch.log(std), sac=self.algo == "sac" | ||
| ) | ||
| entropy = dist.entropy().sum(dim=-1, keepdim=True) # [batch, 1] | ||
| weighted_log_prob = log_prob * entropy | ||
| return action.detach().cpu().numpy(), weighted_log_prob | ||
|
|
||
| # Check for model attribute-based approaches | ||
| elif hasattr(self.model, "continuous_action") and getattr( | ||
| self.model, "continuous_action" | ||
|
|
@@ -126,9 +120,16 @@ def explore(self, s, return_logp, metrics=None) -> Tuple[np.ndarray, torch.Tenso | |
| elif len(model_output) == 4: | ||
| # Tanh squashing mode: (action, z, mean, log_std) | ||
| action, z, mean, log_std = model_output | ||
| log_prob = sample_nondeterministic_logprobs( | ||
| z=z, mean=mean, log_std=log_std, sac=self.algo == "sac" | ||
| ) | ||
| if not self.algo == "sac": | ||
|
|
||
| log_prob = sample_nondeterministic_logprobs( | ||
| z=z, | ||
| mean=mean, | ||
| log_std=log_std, | ||
| sac=False, | ||
| ) | ||
| else: | ||
| log_prob = self.model.policy_log_prob(z, mean, log_std) | ||
| else: | ||
| raise ValueError( | ||
| f"Unexpected model output length: {len(model_output)}" | ||
|
|
@@ -145,9 +146,15 @@ def explore(self, s, return_logp, metrics=None) -> Tuple[np.ndarray, torch.Tenso | |
| if self.model.output_style == "squashed_gaussian": | ||
| # Should be 4-tuple: (action, z, mean, log_std) | ||
| action, z, mean, log_std = model_output | ||
| log_prob = sample_nondeterministic_logprobs( | ||
| z=z, mean=mean, log_std=log_std, sac=self.algo == "sac" | ||
| ) | ||
| if not self.algo == "sac": | ||
| log_prob = sample_nondeterministic_logprobs( | ||
| z=z, | ||
| mean=mean, | ||
| log_std=log_std, | ||
| sac=False, | ||
| ) | ||
| else: | ||
| log_prob = self.model.policy_log_prob(z, mean, log_std) | ||
|
|
||
| if return_logp: | ||
| return action.detach().cpu().numpy(), log_prob | ||
|
|
@@ -162,9 +169,16 @@ def explore(self, s, return_logp, metrics=None) -> Tuple[np.ndarray, torch.Tenso | |
| z = dist.rsample() | ||
| action = torch.tanh(z) | ||
|
|
||
| log_prob = sample_nondeterministic_logprobs( | ||
| z=z, mean=mean, log_std=torch.log(std), sac=self.algo == "sac" | ||
| ) | ||
| if not self.algo == "sac": | ||
| log_prob = sample_nondeterministic_logprobs( | ||
| z=z, | ||
| mean=mean, | ||
| log_std=log_std, | ||
| sac=False, | ||
| ) | ||
| else: | ||
| log_prob = self.model.policy_log_prob(z, mean, log_std) | ||
|
|
||
| entropy = dist.entropy().sum(dim=-1, keepdim=True) | ||
| weighted_log_prob = log_prob * entropy | ||
| return action.detach().cpu().numpy(), weighted_log_prob | ||
|
|
@@ -175,14 +189,11 @@ def explore(self, s, return_logp, metrics=None) -> Tuple[np.ndarray, torch.Tenso | |
| ) | ||
|
|
||
| # Special handling for SACModel | ||
| elif isinstance(self.model, SACModel): | ||
| elif self.algo == "sac" and isinstance(self.model, SACModel): | ||
| action, z, mean, log_std = self.model(state, deterministic=False) | ||
| std = torch.exp(log_std) | ||
| dist = Normal(mean, std) | ||
|
|
||
| log_pz = dist.log_prob(z).sum(dim=-1, keepdim=True) | ||
| weighted_log_prob = log_pz * self.entropy_coefficient | ||
| return action.detach().cpu().numpy(), weighted_log_prob | ||
| # CRITICAL: Use the model's policy_log_prob which includes tanh correction | ||
| log_prob = self.model.policy_log_prob(z, mean, log_std) | ||
| return action.detach().cpu().numpy(), log_prob | ||
|
|
||
| else: | ||
| raise RuntimeError( | ||
|
|
||
Oops, something went wrong.
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.
Comment here is env-specific. Also inconsistent: dones are always overwritten to real termination regardless of what the flag says.
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.
Make this default