-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Make advantages compatible with Terminated, Truncated, Done (…
…#1581) Co-authored-by: Skander Moalla <37197319+skandermoalla@users.noreply.github.com> Co-authored-by: Matteo Bettini <55539777+matteobettini@users.noreply.github.com>
- Loading branch information
1 parent
3785609
commit 106368f
Showing
22 changed files
with
1,203 additions
and
337 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 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 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 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 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,41 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
from tensordict import unravel_key | ||
from torchrl.envs import Transform | ||
|
||
|
||
def swap_last(source, dest): | ||
source = unravel_key(source) | ||
dest = unravel_key(dest) | ||
if isinstance(source, str): | ||
if isinstance(dest, str): | ||
return dest | ||
return dest[-1] | ||
if isinstance(dest, str): | ||
return source[:-1] + (dest,) | ||
return source[:-1] + (dest[-1],) | ||
|
||
|
||
class DoneTransform(Transform): | ||
"""Expands the 'done' entries (incl. terminated) to match the reward shape. | ||
Can be appended to a replay buffer or a collector. | ||
""" | ||
|
||
def __init__(self, reward_key, done_keys): | ||
super().__init__() | ||
self.reward_key = reward_key | ||
self.done_keys = done_keys | ||
|
||
def forward(self, tensordict): | ||
for done_key in self.done_keys: | ||
new_name = swap_last(self.reward_key, done_key) | ||
tensordict.set( | ||
("next", new_name), | ||
tensordict.get(("next", done_key)) | ||
.unsqueeze(-1) | ||
.expand(tensordict.get(("next", self.reward_key)).shape), | ||
) | ||
return tensordict |
Oops, something went wrong.