-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Added Reward Providers for Torch #4280
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
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f5c0b2a
Added Reward Providers for Torch
vincentpierre 2ee1609
Merge branch 'develop-add-fire' into develop-add-fire-rewards
vincentpierre 2df477d
Use NetworkBody to encode state in the reward providers
vincentpierre bf6f587
Integrating the reward prodiders with ppo and torch
vincentpierre 29f5d4a
work in progress, integration with PPO. Not training properly Pyramid…
vincentpierre 0a5e4c9
Integration in PPO
vincentpierre a011362
Removing duplicate file
vincentpierre 8f9f348
Gail and Curiosity working
vincentpierre 5c73799
addressing comments
vincentpierre 99a976f
Merge branch 'develop-add-fire' into develop-add-fire-rewards
vincentpierre f9b3eab
Merge branch 'develop-add-fire' into develop-add-fire-rewards
vincentpierre 7f59c0b
merging add-fire and resolving conflicts
vincentpierre 5d0bb32
Enfore float32 for tests
vincentpierre fff745c
enfore np.float32 in buffer
vincentpierre 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
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
111 changes: 111 additions & 0 deletions
111
ml-agents/mlagents/trainers/tests/torch/test_reward_providers/test_curiosity.py
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,111 @@ | ||
import numpy as np | ||
import pytest | ||
import torch | ||
from mlagents.trainers.torch.components.reward_providers import ( | ||
CuriosityRewardProvider, | ||
create_reward_provider, | ||
) | ||
from mlagents_envs.base_env import BehaviorSpec, ActionType | ||
from mlagents.trainers.settings import CuriositySettings, RewardSignalType | ||
from mlagents.trainers.tests.torch.test_reward_providers.utils import ( | ||
create_agent_buffer, | ||
) | ||
|
||
SEED = [42] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"behavior_spec", | ||
[ | ||
BehaviorSpec([(10,)], ActionType.CONTINUOUS, 5), | ||
BehaviorSpec([(10,)], ActionType.DISCRETE, (2, 3)), | ||
], | ||
) | ||
def test_construction(behavior_spec: BehaviorSpec) -> None: | ||
curiosity_settings = CuriositySettings(32, 0.01) | ||
curiosity_settings.strength = 0.1 | ||
curiosity_rp = CuriosityRewardProvider(behavior_spec, curiosity_settings) | ||
assert curiosity_rp.strength == 0.1 | ||
assert curiosity_rp.name == "Curiosity" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"behavior_spec", | ||
[ | ||
BehaviorSpec([(10,)], ActionType.CONTINUOUS, 5), | ||
BehaviorSpec([(10,), (64, 66, 3), (84, 86, 1)], ActionType.CONTINUOUS, 5), | ||
BehaviorSpec([(10,), (64, 66, 1)], ActionType.DISCRETE, (2, 3)), | ||
BehaviorSpec([(10,)], ActionType.DISCRETE, (2,)), | ||
], | ||
) | ||
def test_factory(behavior_spec: BehaviorSpec) -> None: | ||
curiosity_settings = CuriositySettings(32, 0.01) | ||
curiosity_rp = create_reward_provider( | ||
RewardSignalType.CURIOSITY, behavior_spec, curiosity_settings | ||
) | ||
assert curiosity_rp.name == "Curiosity" | ||
|
||
|
||
@pytest.mark.parametrize("seed", SEED) | ||
@pytest.mark.parametrize( | ||
"behavior_spec", | ||
[ | ||
BehaviorSpec([(10,), (64, 66, 3), (24, 26, 1)], ActionType.CONTINUOUS, 5), | ||
BehaviorSpec([(10,)], ActionType.DISCRETE, (2, 3)), | ||
BehaviorSpec([(10,)], ActionType.DISCRETE, (2,)), | ||
], | ||
) | ||
def test_reward_decreases(behavior_spec: BehaviorSpec, seed: int) -> None: | ||
np.random.seed(seed) | ||
torch.manual_seed(seed) | ||
curiosity_settings = CuriositySettings(32, 0.01) | ||
curiosity_rp = CuriosityRewardProvider(behavior_spec, curiosity_settings) | ||
buffer = create_agent_buffer(behavior_spec, 5) | ||
curiosity_rp.update(buffer) | ||
reward_old = curiosity_rp.evaluate(buffer)[0] | ||
for _ in range(10): | ||
curiosity_rp.update(buffer) | ||
reward_new = curiosity_rp.evaluate(buffer)[0] | ||
assert reward_new < reward_old | ||
reward_old = reward_new | ||
|
||
|
||
@pytest.mark.parametrize("seed", SEED) | ||
@pytest.mark.parametrize( | ||
"behavior_spec", [BehaviorSpec([(10,)], ActionType.CONTINUOUS, 5)] | ||
) | ||
def test_continuous_action_prediction(behavior_spec: BehaviorSpec, seed: int) -> None: | ||
np.random.seed(seed) | ||
torch.manual_seed(seed) | ||
curiosity_settings = CuriositySettings(32, 0.1) | ||
curiosity_rp = CuriosityRewardProvider(behavior_spec, curiosity_settings) | ||
buffer = create_agent_buffer(behavior_spec, 5) | ||
for _ in range(200): | ||
curiosity_rp.update(buffer) | ||
prediction = curiosity_rp._network.predict_action(buffer)[0].detach() | ||
target = buffer["actions"][0] | ||
error = float(torch.mean((prediction - target) ** 2)) | ||
assert error < 0.001 | ||
|
||
|
||
@pytest.mark.parametrize("seed", SEED) | ||
@pytest.mark.parametrize( | ||
"behavior_spec", | ||
[ | ||
BehaviorSpec([(10,), (64, 66, 3)], ActionType.CONTINUOUS, 5), | ||
BehaviorSpec([(10,)], ActionType.DISCRETE, (2, 3)), | ||
BehaviorSpec([(10,)], ActionType.DISCRETE, (2,)), | ||
], | ||
) | ||
def test_next_state_prediction(behavior_spec: BehaviorSpec, seed: int) -> None: | ||
np.random.seed(seed) | ||
torch.manual_seed(seed) | ||
curiosity_settings = CuriositySettings(32, 0.1) | ||
curiosity_rp = CuriosityRewardProvider(behavior_spec, curiosity_settings) | ||
buffer = create_agent_buffer(behavior_spec, 5) | ||
for _ in range(100): | ||
curiosity_rp.update(buffer) | ||
prediction = curiosity_rp._network.predict_next_state(buffer)[0] | ||
target = curiosity_rp._network.get_next_state(buffer)[0] | ||
error = float(torch.mean((prediction - target) ** 2).detach()) | ||
assert error < 0.001 |
56 changes: 56 additions & 0 deletions
56
ml-agents/mlagents/trainers/tests/torch/test_reward_providers/test_extrinsic.py
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,56 @@ | ||
import pytest | ||
from mlagents.trainers.torch.components.reward_providers import ( | ||
ExtrinsicRewardProvider, | ||
create_reward_provider, | ||
) | ||
from mlagents_envs.base_env import BehaviorSpec, ActionType | ||
from mlagents.trainers.settings import RewardSignalSettings, RewardSignalType | ||
from mlagents.trainers.tests.torch.test_reward_providers.utils import ( | ||
create_agent_buffer, | ||
) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"behavior_spec", | ||
[ | ||
BehaviorSpec([(10,)], ActionType.CONTINUOUS, 5), | ||
BehaviorSpec([(10,)], ActionType.DISCRETE, (2, 3)), | ||
], | ||
) | ||
def test_construction(behavior_spec: BehaviorSpec) -> None: | ||
settings = RewardSignalSettings() | ||
settings.gamma = 0.2 | ||
extrinsic_rp = ExtrinsicRewardProvider(behavior_spec, settings) | ||
assert extrinsic_rp.gamma == 0.2 | ||
assert extrinsic_rp.name == "Extrinsic" | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"behavior_spec", | ||
[ | ||
BehaviorSpec([(10,)], ActionType.CONTINUOUS, 5), | ||
BehaviorSpec([(10,)], ActionType.DISCRETE, (2, 3)), | ||
], | ||
) | ||
def test_factory(behavior_spec: BehaviorSpec) -> None: | ||
settings = RewardSignalSettings() | ||
extrinsic_rp = create_reward_provider( | ||
RewardSignalType.EXTRINSIC, behavior_spec, settings | ||
) | ||
assert extrinsic_rp.name == "Extrinsic" | ||
|
||
|
||
@pytest.mark.parametrize("reward", [2.0, 3.0, 4.0]) | ||
@pytest.mark.parametrize( | ||
"behavior_spec", | ||
[ | ||
BehaviorSpec([(10,)], ActionType.CONTINUOUS, 5), | ||
BehaviorSpec([(10,)], ActionType.DISCRETE, (2, 3)), | ||
], | ||
) | ||
def test_reward(behavior_spec: BehaviorSpec, reward: float) -> None: | ||
buffer = create_agent_buffer(behavior_spec, 1000, reward) | ||
settings = RewardSignalSettings() | ||
extrinsic_rp = ExtrinsicRewardProvider(behavior_spec, settings) | ||
generated_rewards = extrinsic_rp.evaluate(buffer) | ||
assert (generated_rewards == reward).all() |
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.