question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Bug in StackedObservations for terminal_observation when using scalar observations

See original GitHub issue

🐛 Bug

When using scalar observation spaces (Box with just one value), the update function of StackedObservations and StackedDictObservations determines an incorrect stack_ax_size, because the extra dimension is not present. The function assumes (num_envs, dim), but receives (num_envs,) because dim is 1.

In StackedObservations: https://github.com/DLR-RM/stable-baselines3/blob/master/stable_baselines3/common/vec_env/stacked_observations.py#L120

In StackedDictObservations: https://github.com/DLR-RM/stable-baselines3/blob/master/stable_baselines3/common/vec_env/stacked_observations.py#L232

To Reproduce

Code:

import numpy as np
import gym.spaces
from stable_baselines3.common.vec_env.stacked_observations import StackedObservations


num_envs = 4
n_stack = 3
obs_space = gym.spaces.Box(low=-1, high=1, shape=(1,), dtype=np.float32)

infos = [{"terminal_observation": 0}] * num_envs
dones = np.array([True] * num_envs)


stacked_observations = StackedObservations(
    num_envs=num_envs,
    n_stack=n_stack,
    observation_space=obs_space,
)

obs = np.zeros((num_envs,) + obs_space.shape)

stacked_observations.update(obs, dones=dones, infos=infos)

Error:

Traceback (most recent call last):
  File "******", line 24, in <module>
    stacked_observations.update(obs, dones=dones, infos=infos)
  File "******stable-baselines3/stable
_baselines3/common/vec_env/stacked_observations.py", line 132, in update
    new_terminal = np.concatenate(
  File "<__array_function__ internals>", line 180, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 0 dimension(s)

Expected behavior

When calling update, stack_ax_size should be set to 1, but is set to num_envs due to the missing 1 in observations.shape.

### System Info

Describe the characteristic of your environment:

  • Stable Baselines 3: 1.4.1a3 (cdaa9ab418aec18f41c7e8e12e0ad28f238553eb)
  • Python version: 3.9.7
  • Gym version: 0.21.0

Checklist

  • I have checked that there is no similar issue in the repo (required)
  • I have read the documentation (required)
  • I have provided a minimal working example to reproduce the bug (required)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ScheiklPcommented, Mar 8, 2022

Oh damn, I thought it did, but there were too many moving parts. check_env correctly catches the incorrect shape. Sorry for the trouble…

1reaction
araffincommented, Mar 7, 2022

Hello, thanks for spotting the bug, we would welcome a PR that fixes it 😉

Read more comments on GitHub >

github_iconTop Results From Across the Web

master PDF - Stable Baselines3 Documentation
You can use environments with dictionary observation spaces. ... Fixed a bug where the terminal observation stored in the replay buffer was ...
Read more >
Triatomine Bug FAQs - Chagas Disease - CDC
What is a triatomine bug and what does it look like? triatomine. Various triatomine bugs in all life stages, from eggs to nymphs...
Read more >
Kissing Bugs and Chagas Disease in the U.S. | Texas A&M
Please explore to learn more about kissing bugs, the parasite they transmit, and how you can submit your insects and observations to our...
Read more >
Brown Marmorated Stink Bug
The name 'brown marmorated stink bug' describes their appearance: they are brown with a marmorated (marble-patterned) exterior. Nymphs (young) are more ...
Read more >
Bug Fest - The Academy of Natural Sciences of Drexel ...
Meet Academy researchers and talk with fellow bug enthusiasts to find out how their passion for all things invertebrate impacts our communities. As...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found