Skip to content
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

recorder_log_keys arg: enable logging of arbitrary keys from test rollouts #212

Merged
merged 5 commits into from
Jun 21, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
amend
  • Loading branch information
vmoens committed Jun 21, 2022
commit 5bdf692af59ff2013709d47dd28895fc36420f90
24 changes: 20 additions & 4 deletions torchrl/trainers/trainers.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,8 @@ def __init__(
policy_exploration: TensorDictModule,
recorder: _EnvClass,
exploration_mode: str = "mode",
out_key: str = "r_evaluation",
log_keys: Optional[List[str]] = None,
out_keys: Optional[Dict[str, str]] = None,
suffix: Optional[str] = None,
log_pbar: bool = False,
) -> None:
Expand All @@ -837,7 +838,13 @@ def __init__(
self._count = 0
self.record_interval = record_interval
self.exploration_mode = exploration_mode
self.out_key = out_key
if log_keys is None:
log_keys = ["reward"]
if out_keys is None:
out_keys = KeyDependentDefaultDict()
out_keys["reward"] = "r_evaluation"
self.log_keys = log_keys
self.out_keys = out_keys
self.suffix = suffix
self.log_pbar = log_pbar

Expand All @@ -860,10 +867,19 @@ def __call__(self, batch: _TensorDict) -> Dict:
if isinstance(self.policy_exploration, torch.nn.Module):
self.policy_exploration.train()
self.recorder.train()
reward = td_record.get("reward").mean() / self.frame_skip
self.recorder.transform.dump(suffix=self.suffix)
out = {self.out_key: reward, "log_pbar": self.log_pbar}

out = dict()
for key in self.log_keys:
value = td_record.get(key).float().mean()
if key == "reward":
value = value / self.frame_skip
if key == "solved":
value = value.any().float()
out[self.out_keys[key]] = value
out["log_pbar"] = self.log_pbar
self._count += 1
self.recorder.close()
return out


Expand Down