[Bug] Callbacks can't access local variables
See original GitHub issueDescription
From the discussion here #756, callbacks in DQN should have access to local variables, e.g. done
through BaseCallback.locals
/ self.locals
. However, this isn’t the case.
Code example
from stable_baselines.common.callbacks import BaseCallback
import gym
import stable_baselines as sb
env = gym.make("MountainCar-v0")
class TestCallback(BaseCallback):
def __init__(self, verbose=0):
super().__init__(verbose)
def _on_step(self):
assert ('done' in self.locals)
agent = sb.DQN('MlpPolicy', env, verbose=1)
agent.learn(100, callback=TestCallback())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/stelios/anaconda3/envs/thesis37/lib/python3.7/site-packages/stable_baselines/deepq/dqn.py", line 224, in learn
if callback.on_step() is False:
File "/Users/stelios/anaconda3/envs/thesis37/lib/python3.7/site-packages/stable_baselines/common/callbacks.py", line 89, in on_step
return self._on_step()
File "<stdin>", line 5, in _on_step
AssertionError
Workaround
In DQN.learn
, line 219, the following results in assertion error.
new_obs, rew, done, info = self.env.step(env_action)
self.num_timesteps += 1
assert 'done' in callback.locals # fails
# Stop training if return value is False
if callback.on_step() is False:
break
If we call locals()
, then the assertion succeeds.
new_obs, rew, done, info = self.env.step(env_action)
self.num_timesteps += 1
locals()
assert 'done' in callback.locals # succeeds
# Stop training if return value is False
if callback.on_step() is False:
break
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:8
Top Results From Across the Web
JavaScript Callbacks Variable Scope Problem - Pluralsight
In this free JavaScript guide, you'll learn how to fix the common JavaScript callbacks variable scope problem. This step-by-step JavaScript ...
Read more >Why can't I use local variable in a callback? - Stack Overflow
We know that Stream.listen does not call its callback until after a value is returned, but the Dart compiler does not.
Read more >How to access the correct this inside a callback
3 methods for accessing the correct this inside a callback · 1. Use an arrow function · 2. Create another variable to store...
Read more >Can't get variable out of Callback function. Already defined as ...
I try to svae this global variable in a local variable in my Mainscript. Theme.
Read more >Unable to access variable outside of function when invoked as ...
But, When it is passed into setInterval as a callback, it can't access variable v1 outside. In nodejs, both of calling work same....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was just about to test this, but you got ahead of me 👍.
I think it would be nice to have same treatment for other algorithms as well, where
locals
has access to things likedone
andreward
(can be quite relevant foron_step
callback). This could include allenv.step
related variables (obs, reward, done, info and chosen action). I am not 100% sure if this can be done with all algorithms, but I can see use-cases for this kind of information.@araffin Quick thoughts on this, with you being the main author of this code?
I have solved this by initializing the variables before the
callback.on_training_start
call.Should I make a PR with this change? Which other variables should I initialise?