-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Return deterministic actions #5597
Merged
cmard
merged 16 commits into
deterministic-actions-python-training
from
develop-staging-determinstic-action
Nov 15, 2021
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7e7c3e2
Progress on propagating the setting to the action model.
cmard 824f54b
Added the _sample_action logic and tests.
cmard 3e1a60a
Add information to the changelog.
cmard 2918be6
Prioritize the CLI over the configuration file.
cmard f1d0965
Update documentation for config file.
cmard 646498e
CR refactor.
cmard 78fd1c8
Update docs/Training-Configuration-File.md
cmard 6e43451
Update com.unity.ml-agents/CHANGELOG.md
cmard 4b6808f
Update com.unity.ml-agents/CHANGELOG.md
cmard a507c7d
Update com.unity.ml-agents/CHANGELOG.md
cmard 5b059fd
Update ml-agents/mlagents/trainers/settings.py
cmard c8eb7a9
Update ml-agents/mlagents/trainers/cli_utils.py
cmard 283ed15
Fix CR requests
cmard 0267d3d
Merge branch 'develop-staging-determinstic-action' of github.com:Unit…
cmard 0431025
Add tests for discrete.
cmard 98da4b1
Update ml-agents/mlagents/trainers/torch/distributions.py
cmard 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
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
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
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 |
---|---|---|
|
@@ -32,6 +32,7 @@ def __init__( | |
action_spec: ActionSpec, | ||
conditional_sigma: bool = False, | ||
tanh_squash: bool = False, | ||
deterministic: bool = False, | ||
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. please update the docstring |
||
): | ||
""" | ||
A torch module that represents the action space of a policy. The ActionModel may contain | ||
|
@@ -43,6 +44,7 @@ def __init__( | |
:params action_spec: The ActionSpec defining the action space dimensions and distributions. | ||
:params conditional_sigma: Whether or not the std of a Gaussian is conditioned on state. | ||
:params tanh_squash: Whether to squash the output of a Gaussian with the tanh function. | ||
:params deterministic: Whether to select actions deterministically in policy. | ||
""" | ||
super().__init__() | ||
self.encoding_size = hidden_size | ||
|
@@ -66,22 +68,32 @@ def __init__( | |
# During training, clipping is done in TorchPolicy, but we need to clip before ONNX | ||
# export as well. | ||
self._clip_action_on_export = not tanh_squash | ||
self._deterministic = deterministic | ||
|
||
def _sample_action(self, dists: DistInstances) -> AgentAction: | ||
""" | ||
Samples actions from a DistInstances tuple | ||
:params dists: The DistInstances tuple | ||
:return: An AgentAction corresponding to the actions sampled from the DistInstances | ||
""" | ||
|
||
continuous_action: Optional[torch.Tensor] = None | ||
discrete_action: Optional[List[torch.Tensor]] = None | ||
# This checks None because mypy complains otherwise | ||
print(self._deterministic) | ||
if dists.continuous is not None: | ||
continuous_action = dists.continuous.sample() | ||
if self._deterministic: | ||
continuous_action = dists.continuous.deterministic_sample() | ||
else: | ||
continuous_action = dists.continuous.sample() | ||
if dists.discrete is not None: | ||
discrete_action = [] | ||
for discrete_dist in dists.discrete: | ||
discrete_action.append(discrete_dist.sample()) | ||
if self._deterministic: | ||
for discrete_dist in dists.discrete: | ||
discrete_action.append(discrete_dist.deterministic_sample()) | ||
else: | ||
for discrete_dist in dists.discrete: | ||
discrete_action.append(discrete_dist.sample()) | ||
return AgentAction(continuous_action, discrete_action) | ||
|
||
def _get_dists(self, inputs: torch.Tensor, masks: torch.Tensor) -> DistInstances: | ||
|
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
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.
some tests on discrete actions would be great!