Skip to content

Commit

Permalink
[release] Fix rl trainer warning (#5144)
Browse files Browse the repository at this point in the history
* Fix rl trainer warning

* Fix typo
  • Loading branch information
Ervin T committed Mar 17, 2021
1 parent 7868faa commit ab18283
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
32 changes: 32 additions & 0 deletions ml-agents/mlagents/trainers/tests/test_rl_trainer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import unittest
from unittest import mock
import pytest
import mlagents.trainers.tests.mock_brain as mb
Expand Down Expand Up @@ -178,3 +179,34 @@ def test_summary_checkpoint(mock_add_checkpoint, mock_write_summary):
for step in checkpoint_range
]
mock_add_checkpoint.assert_has_calls(add_checkpoint_calls)


class RLTrainerWarningTest(unittest.TestCase):
def test_warning_group_reward(self):
with self.assertLogs("mlagents.trainers", level="WARN") as cm:
rl_trainer = create_rl_trainer()
# This one should warn
trajectory = mb.make_fake_trajectory(
length=10,
observation_specs=create_observation_specs_with_shapes([(1,)]),
max_step_complete=True,
action_spec=ActionSpec.create_discrete((2,)),
group_reward=1.0,
)
buff = trajectory.to_agentbuffer()
rl_trainer._warn_if_group_reward(buff)
assert len(cm.output) > 0
len_of_first_warning = len(cm.output)

rl_trainer = create_rl_trainer()
# This one shouldn't
trajectory = mb.make_fake_trajectory(
length=10,
observation_specs=create_observation_specs_with_shapes([(1,)]),
max_step_complete=True,
action_spec=ActionSpec.create_discrete((2,)),
)
buff = trajectory.to_agentbuffer()
rl_trainer._warn_if_group_reward(buff)
# Make sure warnings don't get bigger
assert len(cm.output) == len_of_first_warning
2 changes: 1 addition & 1 deletion ml-agents/mlagents/trainers/trainer/rl_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ def _warn_if_group_reward(self, buffer: AgentBuffer) -> None:
Warn if the trainer receives a Group Reward but isn't a multiagent trainer (e.g. POCA).
"""
if not self._has_warned_group_rewards:
if not np.any(buffer[BufferKey.GROUP_REWARD]):
if np.any(buffer[BufferKey.GROUP_REWARD]):
logger.warning(
"An agent recieved a Group Reward, but you are not using a multi-agent trainer. "
"Please use the POCA trainer for best results."
Expand Down

0 comments on commit ab18283

Please sign in to comment.