3

I'm trying to train RL-agent to play Car Racing environment with OpenAI Gym and been using following code:

import gym
from stable_baselines3 import PPO
from stable_baselines3.common.vec_env import DummyVecEnv
from stable_baselines3.common.evaluation import evaluate_policy
import os

environment_name = 'CarRacing-v2'
env = gym.make(environment_name, render_mode="human")

env.action_space
env.observation_space

episodes = 1
for episode in range(1, episodes+1):
    state = env.reset()
    done = False
    score = 0 
    
    while not done:
        action = env.action_space.sample()
        n_state, reward, done, info, _ = env.step(action)
        score+=reward
    print(f'Episode:{episode} Score:{score}')
env.close()

TRAIN MODEL:

env = gym.make(environment_name)
env = DummyVecEnv([lambda: env])

model = PPO("CnnPolicy", env, verbose=1)

model.learn(total_timesteps=1) # Just to show how a model is trained
# later we will train model over millions of timesteps

Everything works fine (except the upmost code block takes forever to run because the episode only ends after the car has driven off the map and the speed is very slow..) but the last row "model.learn(total_timesteps=1)" procudes the following error:

Box([-1.  0.  0.], 1.0, (3,), float32)
Box(0, 255, (96, 96, 3), uint8)
/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm
2022-11-18 15:15:08.327 Python[94428:771948] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to /var/folders/6l/gfqkwfbd7rs176sshdhfz5f80000gn/T/org.python.python.savedState
Episode:1 Score:-866.6780141845032

Using cpu device
Wrapping the env in a VecTransposeImage.
Output exceeds the size limit. Open the full output data in a text editor
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In [9], line 1
----> 1 model.learn(total_timesteps=1)

File /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/stable_baselines3/ppo/ppo.py:317, in PPO.learn(self, total_timesteps, callback, log_interval, eval_env, eval_freq, n_eval_episodes, tb_log_name, eval_log_path, reset_num_timesteps, progress_bar)
    303 def learn(
    304     self: PPOSelf,
    305     total_timesteps: int,
   (...)
    314     progress_bar: bool = False,
    315 ) -> PPOSelf:
--> 317     return super().learn(
    318         total_timesteps=total_timesteps,
    319         callback=callback,
    320         log_interval=log_interval,
    321         eval_env=eval_env,
    322         eval_freq=eval_freq,
    323         n_eval_episodes=n_eval_episodes,
    324         tb_log_name=tb_log_name,
    325         eval_log_path=eval_log_path,
    326         reset_num_timesteps=reset_num_timesteps,
    327         progress_bar=progress_bar,
    328     )
...
---> 94         self.buf_obs[key][env_idx] = obs
     95     else:
     96         self.buf_obs[key][env_idx] = obs[key]

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.

Why does this error happen and how can I get around it?

rjuri
  • 133
  • 7

1 Answers1

0

I also encountered a similar problem. Try installing a later version of stable_baselines3 with pip

pip install stable-baselines3==2.0.0a5

Full post here

Ethan C
  • 1
  • 3