-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Fix for critic normalization bug #5595
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 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f79afcb
Added normalization to critic during training for poca, ppo, and sac.
miguelalonsojr 2960028
Fixed critic normalization bug. Added new tests to cover.
miguelalonsojr 75b218e
Merge branch 'develop-critic-normalization-bug' into fix-critic-norma…
miguelalonsojr e3e2ce4
Updated CHANGELOG.
miguelalonsojr 225e971
Merge branch 'main' into fix-critic-normalization-bug
miguelalonsojr 5f0f517
Fixed typo in test_trainers.py
miguelalonsojr 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 |
---|---|---|
@@ -0,0 +1,164 @@ | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from mlagents.trainers.agent_processor import AgentManagerQueue | ||
from mlagents.trainers.behavior_id_utils import BehaviorIdentifiers | ||
from mlagents.trainers.environment_parameter_manager import EnvironmentParameterManager | ||
from mlagents.trainers.settings import RunOptions | ||
from mlagents.trainers.tests import mock_brain as mb | ||
from mlagents.trainers.tests.dummy_config import ( | ||
create_observation_specs_with_shapes, | ||
ppo_dummy_config, | ||
poca_dummy_config, | ||
sac_dummy_config, | ||
) | ||
from mlagents.trainers.tests.mock_brain import make_fake_trajectory | ||
from mlagents.trainers.trainer import TrainerFactory | ||
|
||
|
||
@pytest.fixture | ||
def ppo_config(): | ||
return RunOptions(behaviors={"test_brain": ppo_dummy_config()}) | ||
|
||
|
||
@pytest.fixture | ||
def sac_config(): | ||
return RunOptions(behaviors={"test_brain": sac_dummy_config()}) | ||
|
||
|
||
@pytest.fixture | ||
def poca_config(): | ||
return RunOptions(behaviors={"test_brain": poca_dummy_config()}) | ||
|
||
|
||
def test_ppo_trainer_update_normalization(ppo_config): | ||
behavior_id_team0 = "test_brain?team=0" | ||
brain_name = BehaviorIdentifiers.from_name_behavior_id(behavior_id_team0).brain_name | ||
mock_specs = mb.setup_test_behavior_specs( | ||
True, False, vector_action_space=[2], vector_obs_space=1 | ||
) | ||
base_config = ppo_config.behaviors | ||
output_path = "results_dir" | ||
train_model = True | ||
load_model = False | ||
seed = 42 | ||
trainer_factory = TrainerFactory( | ||
trainer_config=base_config, | ||
output_path=output_path, | ||
train_model=train_model, | ||
load_model=load_model, | ||
seed=seed, | ||
param_manager=EnvironmentParameterManager(), | ||
) | ||
ppo_trainer = trainer_factory.generate(brain_name) | ||
parsed_behavior_id0 = BehaviorIdentifiers.from_name_behavior_id(behavior_id_team0) | ||
policy = ppo_trainer.create_policy(parsed_behavior_id0, mock_specs) | ||
ppo_trainer.add_policy(parsed_behavior_id0, policy) | ||
trajectory_queue0 = AgentManagerQueue(behavior_id_team0) | ||
ppo_trainer.subscribe_trajectory_queue(trajectory_queue0) | ||
time_horizon = 15 | ||
trajectory = make_fake_trajectory( | ||
length=time_horizon, | ||
max_step_complete=True, | ||
observation_specs=create_observation_specs_with_shapes([(1,)]), | ||
action_spec=mock_specs.action_spec, | ||
) | ||
trajectory_queue0.put(trajectory) | ||
# mocking out update_normalization in both the policy and critic | ||
with patch( | ||
"mlagents.trainers.torch.networks.ValueNetwork.update_normalization" | ||
) as optimizer_update_normalization_mock, patch( | ||
"mlagents.trainers.policy.torch_policy.TorchPolicy.update_normalization" | ||
) as policy_update_normalization_mock: | ||
ppo_trainer.advance() | ||
optimizer_update_normalization_mock.assert_called_once() | ||
policy_update_normalization_mock.assert_called_once() | ||
|
||
|
||
def test_sac_trainer_update_normalization(sac_config): | ||
behavior_id_team0 = "test_brain?team=0" | ||
brain_name = BehaviorIdentifiers.from_name_behavior_id(behavior_id_team0).brain_name | ||
mock_specs = mb.setup_test_behavior_specs( | ||
True, False, vector_action_space=[2], vector_obs_space=1 | ||
) | ||
base_config = sac_config.behaviors | ||
output_path = "results_dir" | ||
train_model = True | ||
load_model = False | ||
seed = 42 | ||
trainer_factory = TrainerFactory( | ||
trainer_config=base_config, | ||
output_path=output_path, | ||
train_model=train_model, | ||
load_model=load_model, | ||
seed=seed, | ||
param_manager=EnvironmentParameterManager(), | ||
) | ||
ppo_trainer = trainer_factory.generate(brain_name) | ||
parsed_behavior_id0 = BehaviorIdentifiers.from_name_behavior_id(behavior_id_team0) | ||
policy = ppo_trainer.create_policy(parsed_behavior_id0, mock_specs) | ||
ppo_trainer.add_policy(parsed_behavior_id0, policy) | ||
trajectory_queue0 = AgentManagerQueue(behavior_id_team0) | ||
ppo_trainer.subscribe_trajectory_queue(trajectory_queue0) | ||
time_horizon = 15 | ||
trajectory = make_fake_trajectory( | ||
length=time_horizon, | ||
max_step_complete=True, | ||
observation_specs=create_observation_specs_with_shapes([(1,)]), | ||
action_spec=mock_specs.action_spec, | ||
) | ||
trajectory_queue0.put(trajectory) | ||
# mocking out update_normalization in both the policy and critic | ||
with patch( | ||
"mlagents.trainers.torch.networks.ValueNetwork.update_normalization" | ||
) as optimizer_update_normalization_mock, patch( | ||
"mlagents.trainers.policy.torch_policy.TorchPolicy.update_normalization" | ||
) as policy_update_normalization_mock: | ||
ppo_trainer.advance() | ||
optimizer_update_normalization_mock.assert_called_once() | ||
policy_update_normalization_mock.assert_called_once() | ||
|
||
|
||
def test_poca_trainer_update_normalization(poca_config): | ||
behavior_id_team0 = "test_brain?team=0" | ||
brain_name = BehaviorIdentifiers.from_name_behavior_id(behavior_id_team0).brain_name | ||
mock_specs = mb.setup_test_behavior_specs( | ||
True, False, vector_action_space=[2], vector_obs_space=1 | ||
) | ||
base_config = poca_config.behaviors | ||
output_path = "results_dir" | ||
train_model = True | ||
load_model = False | ||
seed = 42 | ||
trainer_factory = TrainerFactory( | ||
trainer_config=base_config, | ||
output_path=output_path, | ||
train_model=train_model, | ||
load_model=load_model, | ||
seed=seed, | ||
param_manager=EnvironmentParameterManager(), | ||
) | ||
poca_trainer = trainer_factory.generate(brain_name) | ||
parsed_behavior_id0 = BehaviorIdentifiers.from_name_behavior_id(behavior_id_team0) | ||
policy = poca_trainer.create_policy(parsed_behavior_id0, mock_specs) | ||
poca_trainer.add_policy(parsed_behavior_id0, policy) | ||
trajectory_queue0 = AgentManagerQueue(behavior_id_team0) | ||
poca_trainer.subscribe_trajectory_queue(trajectory_queue0) | ||
time_horizon = 15 | ||
trajectory = make_fake_trajectory( | ||
length=time_horizon, | ||
max_step_complete=True, | ||
observation_specs=create_observation_specs_with_shapes([(1,)]), | ||
action_spec=mock_specs.action_spec, | ||
) | ||
trajectory_queue0.put(trajectory) | ||
# mocking out update_normalization in both the policy and critic | ||
with patch( | ||
"mlagents.trainers.poca.optimizer_torch.TorchPOCAOptimizer.POCAValueNetwork.update_normalization" | ||
) as optimizer_update_normalization_mock, patch( | ||
"mlagents.trainers.policy.torch_policy.TorchPolicy.update_normalization" | ||
) as policy_update_normalization_mock: | ||
poca_trainer.advance() | ||
optimizer_update_normalization_mock.assert_called_once() | ||
policy_update_normalization_mock.assert_called_once() |
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.
Nit: these should be
sac_trainers
in this method