-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Method to compute actions from observations #991
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
Trinkle23897
merged 8 commits into
thu-ml:master
from
aai-institute:feature/get_action_from_obs
Nov 16, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
aa43688
Change policy forward interface: removed support for 'input' kwarg
MischaPanch 980a0c4
Added compute_action method and test, multiple typing improvements
MischaPanch ce85f17
Fixed missing info field in target-q computation
MischaPanch 0b5e96d
Meta: Adjusted PR and Issue templates
MischaPanch 61e1fa9
Fixed dealing with max_action_num
MischaPanch b931258
Minor improvements in types and fix in passing info
MischaPanch a216c14
try changing seed
Trinkle23897 2d9afeb
Merge branch 'master' into feature/get_action_from_obs
Trinkle23897 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| - [ ] I have marked all applicable categories: | ||
| + [ ] exception-raising fix | ||
| + [ ] algorithm implementation fix | ||
| + [ ] documentation modification | ||
| + [ ] new feature | ||
| - [ ] I have reformatted the code using `make format` (**required**) | ||
| - [ ] I have checked the code using `make commit-checks` (**required**) | ||
| - [ ] If applicable, I have mentioned the relevant/related issue(s) | ||
| - [ ] If applicable, I have listed every items in this Pull Request below | ||
| - [ ] I have added the correct label(s) to this Pull Request or linked the relevant issue(s) | ||
| - [ ] I have provided a description of the changes in this Pull Request | ||
| - [ ] I have added documentation for my changes | ||
| - [ ] If applicable, I have added tests to cover my changes. | ||
| - [ ] I have reformatted the code using `poe format` | ||
| - [ ] I have checked style and types with `poe lint` and `poe type-check` | ||
| - [ ] (Optional) I ran tests locally with `poe test` | ||
| (or a subset of them with `poe test-reduced`) ,and they pass | ||
| - [ ] (Optional) I have tested that documentation builds correctly with `poe doc-build` |
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 |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import gymnasium as gym | ||
| import numpy as np | ||
| import pytest | ||
| import torch | ||
| from torch.distributions import Categorical, Independent, Normal | ||
|
|
||
| from tianshou.policy import PPOPolicy | ||
| from tianshou.utils.net.common import ActorCritic, Net | ||
| from tianshou.utils.net.continuous import ActorProb, Critic | ||
| from tianshou.utils.net.discrete import Actor | ||
|
|
||
| obs_shape = (5,) | ||
|
|
||
|
|
||
| def _to_hashable(x: np.ndarray | int): | ||
| return x if isinstance(x, int) else tuple(x.tolist()) | ||
|
|
||
|
|
||
| @pytest.fixture(params=["continuous", "discrete"]) | ||
| def policy(request): | ||
| action_type = request.param | ||
| if action_type == "continuous": | ||
| action_space = gym.spaces.Box(low=-1, high=1, shape=(3,)) | ||
| actor = ActorProb( | ||
| Net(state_shape=obs_shape, hidden_sizes=[64, 64], action_shape=action_space.shape), | ||
| action_shape=action_space.shape, | ||
| ) | ||
| dist_fn = lambda *logits: Independent(Normal(*logits), 1) | ||
| elif action_type == "discrete": | ||
| action_space = gym.spaces.Discrete(3) | ||
| actor = Actor( | ||
| Net(state_shape=obs_shape, hidden_sizes=[64, 64], action_shape=action_space.n), | ||
| action_shape=action_space.n, | ||
| ) | ||
| dist_fn = lambda logits: Categorical(logits=logits) | ||
| else: | ||
| raise ValueError(f"Unknown action type: {action_type}") | ||
|
|
||
| critic = Critic( | ||
| Net(obs_shape, hidden_sizes=[64, 64]), | ||
| ) | ||
|
|
||
| actor_critic = ActorCritic(actor, critic) | ||
| optim = torch.optim.Adam(actor_critic.parameters(), lr=1e-3) | ||
|
|
||
| policy = PPOPolicy( | ||
| actor=actor, | ||
| critic=critic, | ||
| dist_fn=dist_fn, | ||
| optim=optim, | ||
| action_space=action_space, | ||
| action_scaling=False, | ||
| ) | ||
| policy.eval() | ||
| return policy | ||
|
|
||
|
|
||
| class TestPolicyBasics: | ||
| def test_get_action(self, policy): | ||
| sample_obs = torch.randn(obs_shape) | ||
| policy.deterministic_eval = False | ||
| actions = [policy.compute_action(sample_obs) for _ in range(10)] | ||
| assert all(policy.action_space.contains(a) for a in actions) | ||
|
|
||
| # check that the actions are different in non-deterministic mode | ||
| assert len(set(map(_to_hashable, actions))) > 1 | ||
|
|
||
| policy.deterministic_eval = True | ||
| actions = [policy.compute_action(sample_obs) for _ in range(10)] | ||
| # check that the actions are the same in deterministic mode | ||
| assert len(set(map(_to_hashable, actions))) == 1 |
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
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.
Uh oh!
There was an error while loading. Please reload this page.