这是indexloc提供的服务,不要输入任何密码
Skip to content

Hotfix : keep statisics of buffer when reset buffer in on policy trainer #328

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 1 commit into from
Mar 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions tianshou/data/buffer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ def load_hdf5(cls, path: str, device: Optional[str] = None) -> "ReplayBuffer":
buf.__setstate__(from_hdf5(f, device=device))
return buf

def reset(self) -> None:
def reset(self, keep_statistics: bool = False) -> None:
"""Clear all the data in replay buffer and episode statistics."""
self.last_index = np.array([0])
self._index = self._size = 0
self._ep_rew, self._ep_len, self._ep_idx = 0.0, 0, 0
if not keep_statistics:
self._ep_rew, self._ep_len, self._ep_idx = 0.0, 0, 0

def set_batch(self, batch: Batch) -> None:
"""Manually choose the batch you want the ReplayBuffer to manage."""
Expand Down
4 changes: 2 additions & 2 deletions tianshou/data/buffer/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ def _compile(self) -> None:
def __len__(self) -> int:
return self._lengths.sum()

def reset(self) -> None:
def reset(self, keep_statistics: bool = False) -> None:
self.last_index = self._offset.copy()
self._lengths = np.zeros_like(self._offset)
for buf in self.buffers:
buf.reset()
buf.reset(keep_statistics=keep_statistics)

def _set_batch_for_children(self) -> None:
for offset, buf in zip(self._offset, self.buffers):
Expand Down
4 changes: 2 additions & 2 deletions tianshou/data/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ def reset_stat(self) -> None:
"""Reset the statistic variables."""
self.collect_step, self.collect_episode, self.collect_time = 0, 0, 0.0

def reset_buffer(self) -> None:
def reset_buffer(self, keep_statistics: bool = False) -> None:
"""Reset the data buffer."""
self.buffer.reset()
self.buffer.reset(keep_statistics=keep_statistics)

def reset_env(self) -> None:
"""Reset all of the environments."""
Expand Down
2 changes: 1 addition & 1 deletion tianshou/trainer/onpolicy.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def onpolicy_trainer(
losses = policy.update(
0, train_collector.buffer,
batch_size=batch_size, repeat=repeat_per_collect)
train_collector.reset_buffer()
train_collector.reset_buffer(keep_statistics=True)
step = max([1] + [
len(v) for v in losses.values() if isinstance(v, list)])
gradient_step += step
Expand Down