Rendering example for deep learning

71 views Asked by At

I am trying to get the window to render showing me the game running but it never pops up and just finishes running printing out the 5 scores. I am not sure if there is a problem in vs code (I m running on my machine I am not using a virtual environment)

here is the code

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

env_name = 'CartPole-v1'
env = gym.make(env_name, render_mode="rgb_array")
episodes = 5
for episode in range(episodes+1):
  state = env.reset()
  done = False
  score = 0
  while not done:
    env.render()
    action = env.action_space.sample()
    n_state, reward, done, info, k = env.step(action)
    score += reward
  print('Episode: {} Score: {}'.format(episode, score))

env.close()

I have tried different environments such as a virtual environment and google collab but nothing gets this window to render for a visual look at what's happening

1

There are 1 answers

0
Lexpj On

You could either do one of two things:

  1. Change the render_mode to human. Depending on the gym version, you either have to call env.render() to update the frame or it is done automatically for you. So: env = gym.make(env_name, render_mode="human")
  2. You could look at this post for rendering in notebooks, which works slightly different.