Skip to content

Commit

Permalink
Fix VectorizedActionNoise in OffPolicyAlgorithm (#1657)
Browse files Browse the repository at this point in the history
* moves VectorizedActionNoise into _setup_learn()

* update changelog

---------

Co-authored-by: Antonin RAFFIN <antonin.raffin@ensta.org>
Co-authored-by: Antonin Raffin <antonin.raffin@dlr.de>
  • Loading branch information
3 people committed Aug 30, 2023
1 parent 5c93e9f commit c99d65c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
3 changes: 2 additions & 1 deletion docs/misc/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ New Features:

Bug Fixes:
^^^^^^^^^^
- Prevents OOB error on Windows if no seed is passed (@PatrickHelm)
- Moves VectorizedActionNoise into ``_setup_learn()`` in OffPolicyAlgorithm (@PatrickHelm)
- Prevents out of bound error on Windows if no seed is passed (@PatrickHelm)

Deprecations:
^^^^^^^^^^^^^
Expand Down
12 changes: 8 additions & 4 deletions stable_baselines3/common/off_policy_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,14 @@ def _setup_learn(
pos = (replay_buffer.pos - 1) % replay_buffer.buffer_size
replay_buffer.dones[pos] = True

# Vectorize action noise if needed
if (
self.action_noise is not None
and self.env.num_envs > 1
and not isinstance(self.action_noise, VectorizedActionNoise)
):
self.action_noise = VectorizedActionNoise(self.action_noise, self.env.num_envs)

return super()._setup_learn(
total_timesteps,
callback,
Expand Down Expand Up @@ -523,10 +531,6 @@ def collect_rollouts(
if env.num_envs > 1:
assert train_freq.unit == TrainFrequencyUnit.STEP, "You must use only one env when doing episodic training."

# Vectorize action noise if needed
if action_noise is not None and env.num_envs > 1 and not isinstance(action_noise, VectorizedActionNoise):
action_noise = VectorizedActionNoise(action_noise, env.num_envs)

if self.use_sde:
self.actor.reset_noise(env.num_envs)

Expand Down

0 comments on commit c99d65c

Please sign in to comment.