I've been using stable_baselines3
for recently and successfully applied the Monitor
wrapper for the classic control problems, like so:
from stable_baselines3.common.monitor import Monitor
env = gym.make('CartPole-v1')
env = Monitor(env, "path_to_log_dir")
It saves logs to the log directory, and I can use it to plot the graph to see performance.
However, I ran into issues when I tried applying the same method to Atari environments, specifically while using vectorized environments. Here's a snippet of what I tried:
from stable_baselines3.common.envs import make_atari_env
from stable_baselines3.common.vec_env import VecFrameStack
env = make_atari_env("ALE/BeamRider-v5", n_envs=4)
env = Monitor(env, "path_to_log_dir")
env = VecFrameStack(env, n_stack=4)
Upon trying the above, I received an following error.
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[20], line 5
3 env = make_atari_env("ALE/BeamRider-v5", n_envs=4)
4 # Stack 4 consecutive frames together to provide temporal information
----> 5 env = Monitor(env, logs_dir)
6 env = VecFrameStack(env, n_stack=4)
File /opt/conda/lib/python3.10/site-packages/stable_baselines3/common/monitor.py:44, in Monitor.__init__(self, env, filename, allow_early_resets, reset_keywords, info_keywords, override_existing)
42 self.results_writer = None
43 if filename is not None:
---> 44 env_id = env.spec.id if env.spec is not None else None
45 self.results_writer = ResultsWriter(
46 filename,
47 header={"t_start": self.t_start, "env_id": str(env_id)},
48 extra_keys=reset_keywords + info_keywords,
49 override_existing=override_existing,
50 )
52 self.reset_keywords = reset_keywords
AttributeError: 'DummyVecEnv' object has no attribute 'spec'
I understand that Atari environments are more complex due to being vectorized, and this might be causing the problem.
Could anyone guide me on properly using the Monitor
wrapper with vectorized Atari environments in stable_baselines3
? I'd appreciate a solution that explains the steps in simple terms.
Thank you in advance!
I have also raised this question on official GitHub of Stable baseline: https://github.com/DLR-RM/stable-baselines3/issues/1721