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] AttributeError: 'numpy.random._generator.Generator' object has no attribute 'randint'

See original GitHub issue

So I was working on my atari breakout project and whenever I do evaluate policy I get this error:

AttributeError                            Traceback (most recent call last)
Input In [21], in <cell line: 1>()
----> 1 evaluate_policy(model, env, n_eval_episodes=10)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\stable_baselines3\common\evaluation.py:82, in evaluate_policy(model, env, n_eval_episodes, deterministic, render, callback, reward_threshold, return_episode_rewards, warn)
     80 current_rewards = np.zeros(n_envs)
     81 current_lengths = np.zeros(n_envs, dtype="int")
---> 82 observations = env.reset()
     83 states = None
     84 episode_starts = np.ones((env.num_envs,), dtype=bool)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\stable_baselines3\common\vec_env\vec_frame_stack.py:58, in VecFrameStack.reset(self)
     54 def reset(self) -> Union[np.ndarray, Dict[str, np.ndarray]]:
     55     """
     56     Reset all environments
     57     """
---> 58     observation = self.venv.reset()  # pytype:disable=annotation-type-mismatch
     60     observation = self.stackedobs.reset(observation)
     61     return observation

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\stable_baselines3\common\vec_env\dummy_vec_env.py:61, in DummyVecEnv.reset(self)
     59 def reset(self) -> VecEnvObs:
     60     for env_idx in range(self.num_envs):
---> 61         obs = self.envs[env_idx].reset()
     62         self._save_obs(env_idx, obs)
     63     return self._obs_from_buf()

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\gym\core.py:292, in Wrapper.reset(self, **kwargs)
    291 def reset(self, **kwargs):
--> 292     return self.env.reset(**kwargs)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\gym\core.py:333, in RewardWrapper.reset(self, **kwargs)
    332 def reset(self, **kwargs):
--> 333     return self.env.reset(**kwargs)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\gym\core.py:319, in ObservationWrapper.reset(self, **kwargs)
    318 def reset(self, **kwargs):
--> 319     observation = self.env.reset(**kwargs)
    320     return self.observation(observation)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\stable_baselines3\common\atari_wrappers.py:59, in FireResetEnv.reset(self, **kwargs)
     58 def reset(self, **kwargs) -> np.ndarray:
---> 59     self.env.reset(**kwargs)
     60     obs, _, done, _ = self.env.step(1)
     61     if done:

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\stable_baselines3\common\atari_wrappers.py:106, in EpisodicLifeEnv.reset(self, **kwargs)
     97 """
     98 Calls the Gym environment reset, only when lives are exhausted.
     99 This way all states are still reachable even though lives are episodic,
   (...)
    103 :return: the first observation of the environment
    104 """
    105 if self.was_real_done:
--> 106     obs = self.env.reset(**kwargs)
    107 else:
    108     # no-op step to advance from terminal/lost life state
    109     obs, _, _, _ = self.env.step(0)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\stable_baselines3\common\atari_wrappers.py:154, in MaxAndSkipEnv.reset(self, **kwargs)
    153 def reset(self, **kwargs) -> GymObs:
--> 154     return self.env.reset(**kwargs)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\stable_baselines3\common\atari_wrappers.py:36, in NoopResetEnv.reset(self, **kwargs)
     34     noops = self.override_num_noops
     35 else:
---> 36     noops = self.unwrapped.np_random.randint(1, self.noop_max + 1)
     37 assert noops > 0
     38 obs = np.zeros(0)

AttributeError: 'numpy.random._generator.Generator' object has no attribute 'randint'

Here is my code:

import gym
from stable_baselines3 import A2C
from stable_baselines3.common.vec_env import VecFrameStack
from stable_baselines3.common.evaluation import evaluate_policy
from stable_baselines3.common.env_util import make_atari_env
from stable_baselines3.common.env_util import make_vec_env

import os
from gym.utils import play
from stable_baselines3.ddpg.policies import CnnPolicy

from ale_py import ALEInterface
from ale_py.roms import Breakout

ale = ALEInterface()


ale.loadROM(Breakout)
environment_name = env = gym.make('ALE/Breakout-v5', render_mode='human')
env = make_atari_env('Breakout-v0', n_envs=4, seed=0)
env = VecFrameStack(env, n_stack=4)
log_path = os.path.join('Training', 'Logs')
model = A2C('CnnPolicy', environment_name, verbose=1, tensorboard_log=log_path)
a2c_path = os.path.join('Training','Logs', 'A2C_300k_model')
model.save(a2c_path)
del model
model = A2C.load(a2c_path, env)
evaluate_policy(model, env, n_eval_episodes=10)

Please help. Thanks.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
qgallouedeccommented, Jul 22, 2022

Next time, please fill in the issue template. This way you would have realized that this question has already been asked and duplicated: #875

0reactions
qgallouedeccommented, Jul 23, 2022

Please open a new issue, as it is not related to your original topic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues - GitHub
[RLlib] AttributeError: 'numpy.random._generator.Generator' object has no attribute 'randint' in ".../ray/rllib/env/wrappers/atari_wrappers.py" #24133.
Read more >
'numpy.random._generator.Generator' object has no ... - Reddit
'numpy.random._generator.Generator' object has no attribute 'randint'. So I heard that this error was a bug in the stable_baselines3 module.
Read more >
random.randint error - python - Stack Overflow
For some reason, when running it, I get the following error: AttributeError: 'module' object has no attribute 'randint' . However, I have no...
Read more >
AttributeError module 'random' has no attribute 'randint'
The Python AttributeError module 'random' has no attribute 'randint' occurs when we have a local file named `random.py` and try to import ...
Read more >
'numpy.random._generator.Generator' object has no attribute ...
Generator ' object has no attribute 'randint' in “… ... AttributeError: module numpy.random has no attribute bit_generator 解决办法.
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