-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out subjects and add more checks for VideoSubject
PiperOrigin-RevId: 692892203 Change-Id: I01dd3726dee34a37cd5a67139cccc932f65a2bf7
- Loading branch information
1 parent
5ff2a7f
commit 663653f
Showing
6 changed files
with
286 additions
and
199 deletions.
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright 2024 DeepMind Technologies Limited. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""Subject that emits the player returns at the end of each episode.""" | ||
|
||
import dm_env | ||
import numpy as np | ||
from reactivex import subject | ||
|
||
|
||
class ReturnSubject(subject.Subject): | ||
"""Subject that emits the player returns at the end of each episode.""" | ||
|
||
_return: np.ndarray | None = None | ||
|
||
def on_next(self, timestep: dm_env.TimeStep) -> None: | ||
"""Called on each timestep. | ||
Args: | ||
timestep: the most recent timestep. | ||
""" | ||
if timestep.step_type.first(): | ||
self._return = np.zeros_like(timestep.reward) | ||
elif self._return is None: | ||
raise ValueError('First timestep must be StepType.FIRST.') | ||
self._return += timestep.reward | ||
if timestep.step_type.last(): | ||
super().on_next(self._return) | ||
self._return = None |
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Copyright 2024 DeepMind Technologies Limited. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
|
||
from absl.testing import absltest | ||
import dm_env | ||
from meltingpot.utils.evaluation import return_subject | ||
import numpy as np | ||
|
||
|
||
def _send_timesteps_to_subject(subject, timesteps): | ||
results = [] | ||
subject.subscribe(on_next=results.append) | ||
|
||
for n, timestep in enumerate(timesteps): | ||
subject.on_next(timestep) | ||
if results: | ||
return n, results.pop() | ||
return None, None | ||
|
||
|
||
class ReturnSubjectTest(absltest.TestCase): | ||
|
||
def test(self): | ||
timesteps = [ | ||
dm_env.restart(observation=[{}])._replace(reward=[0, 0]), | ||
dm_env.transition(observation=[{}], reward=[2, 4]), | ||
dm_env.termination(observation=[{}], reward=[1, 3]), | ||
] | ||
subject = return_subject.ReturnSubject() | ||
step_written, episode_returns = _send_timesteps_to_subject( | ||
subject, timesteps | ||
) | ||
|
||
with self.subTest('written_on_final_step'): | ||
self.assertEqual(step_written, 2) | ||
|
||
with self.subTest('returns'): | ||
np.testing.assert_equal(episode_returns, [3, 7]) | ||
|
||
if __name__ == '__main__': | ||
absltest.main() |
Oops, something went wrong.