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.

[question] Monitoring a custom environment

See original GitHub issue

I am training an A2C algorithm on a custom environment using multiprocessing and SubprocVecEnv as follows:

`env = SubprocVecEnv([lambda: CustomEnv(args, i) for i in range(args.cpus)])

model = A2C(MlpLnLstmPolicy, env, verbose=1, tensorboard_log=None, learning_rate=7e-3, lr_schedule="linear")

model.learn(total_timesteps=args.training_steps, log_interval=10)

I want to monitor the learning and save model checkpoints using a Monitor and callbacks, however I can’t seem to figure out how to combine everything. I’ve tried doing

`env = SubprocVecEnv([lambda: CustomEnv(args, i) for i in range(args.cpus)]) env = Monitor(env, log_dir, allow_early_resets=True)

model = A2C(MlpLnLstmPolicy, env, verbose=1, tensorboard_log=None, learning_rate=7e-3, lr_schedule="linear")

model.learn(total_timesteps=args.training_steps, log_interval=10, callback=callback)`

but I get the following exception:

Traceback (most recent call last): File “/main_file.py”, line 96, in <module> env = train(args) File “/main_file.py”, line 76, in train env = Monitor(env, log_dir, allow_early_resets=True) File “/miniconda3/envs/RL/lib/python3.7/site-packages/stable_baselines/bench/monitor.py”, line 27, in init Wrapper.init(self, env=env) File “/miniconda3/envs/RL/lib/python3.7/site-packages/gym/core.py”, line 210, in init self.reward_range = self.env.reward_range AttributeError: ‘SubprocVecEnv’ object has no attribute ‘reward_range’

So what is the correct way of using a monitor in this setting?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:9

github_iconTop GitHub Comments

13reactions
ajtanskanencommented, Nov 26, 2019

I also ran into similar problems with monitoring vectorized environments. It was straight forward to tweak VecMonitor from OpenAI Baselines to work with Stable Baselines, as suggested above. This is what I ended up with.

from stable_baselines.common.vec_env import VecEnvWrapper
import numpy as np
import time
from collections import deque
import os.path as osp
import json
import csv

class VecMonitor(VecEnvWrapper):
    EXT = "monitor.csv"
    
    def __init__(self, venv, filename=None, keep_buf=0, info_keywords=()):
        VecEnvWrapper.__init__(self, venv)
        print('init vecmonitor: ',filename)
        self.eprets = None
        self.eplens = None
        self.epcount = 0
        self.tstart = time.time()
        if filename:
            self.results_writer = ResultsWriter(filename, header={'t_start': self.tstart},
                extra_keys=info_keywords)
        else:
            self.results_writer = None
        self.info_keywords = info_keywords
        self.keep_buf = keep_buf
        if self.keep_buf:
            self.epret_buf = deque([], maxlen=keep_buf)
            self.eplen_buf = deque([], maxlen=keep_buf)

    def reset(self):
        obs = self.venv.reset()
        self.eprets = np.zeros(self.num_envs, 'f')
        self.eplens = np.zeros(self.num_envs, 'i')
        return obs

    def step_wait(self):
        obs, rews, dones, infos = self.venv.step_wait()
        self.eprets += rews
        self.eplens += 1

        newinfos = list(infos[:])
        for i in range(len(dones)):
            if dones[i]:
                info = infos[i].copy()
                ret = self.eprets[i]
                eplen = self.eplens[i]
                epinfo = {'r': ret, 'l': eplen, 't': round(time.time() - self.tstart, 6)}
                for k in self.info_keywords:
                    epinfo[k] = info[k]
                info['episode'] = epinfo
                if self.keep_buf:
                    self.epret_buf.append(ret)
                    self.eplen_buf.append(eplen)
                self.epcount += 1
                self.eprets[i] = 0
                self.eplens[i] = 0
                if self.results_writer:
                    self.results_writer.write_row(epinfo)
                newinfos[i] = info
        return obs, rews, dones, newinfos
        
class ResultsWriter(object):
    def __init__(self, filename, header='', extra_keys=()):
        print('init resultswriter')
        self.extra_keys = extra_keys
        assert filename is not None
        if not filename.endswith(VecMonitor.EXT):
            if osp.isdir(filename):
                filename = osp.join(filename, VecMonitor.EXT)
            else:
                filename = filename #   + "." + VecMonitor.EXT
        self.f = open(filename, "wt")
        if isinstance(header, dict):
            header = '# {} \n'.format(json.dumps(header))
        self.f.write(header)
        self.logger = csv.DictWriter(self.f, fieldnames=('r', 'l', 't')+tuple(extra_keys))
        self.logger.writeheader()
        self.f.flush()

    def write_row(self, epinfo):
        if self.logger:
            self.logger.writerow(epinfo)
            self.f.flush()        
7reactions
Demetrio92commented, Nov 29, 2021

So in SB3 simply:

from stable_baselines3.common.vec_env import VecMonitor
env = VecMonitor(env, log_dir)

instead of

from stable_baselines3.common.monitor import Monitor
env = Monitor(env, log_dir)   # won't work with vectorized enviroments, will throw cryptic errors

sorry for off-topic, googled the error and got here

Read more comments on GitHub >

github_iconTop Results From Across the Web

A Comprehensive Guide on How to Monitor Your Models in ...
The most effective way to gauge the functional performance of your model in production is to monitor the prediction of the model and...
Read more >
issues applying q-learning with custom environment (python ...
The environment works but I am not able to apply q-learning to it. Below the environment is a script that is able to...
Read more >
14 Questions to Ask to Assess IT Infrastructure Monitoring Tools
It's challenging to know which IT Infrastructure monitoring tool is right for you. Start by asking these questions.
Read more >
Application Monitoring tools Build vs Buy | Inspector tutorials
Determining if build a custom monitoring system worth the investment, or it's better to buy a prepackaged solution from a commercial vendor.
Read more >
Monitoring an environment - AWS Elastic Beanstalk
Monitoring an environment ... When you are running a production website, it is important to know that your application is available and responding...
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