AttributeError: module 'numpy' has no attribute 'bool8'

calico·2025년 4월 7일

Error

목록 보기
1/10

Error 원문



AttributeError Traceback (most recent call last)
Cell In[2], line 35
32 action = np.argmax(Q_table[state, :]) # Q-테이블 기반 행동
34 # 행동 실행 & 다음 상태, 보상, 종료 여부 받기
---> 35 new_state, reward, terminated, truncated, _ = env.step(action)
36 done = terminated or truncated
38 # Q-값 업데이트 (Bellman Equation)

File ~\ml\env\Lib\site-packages\gym\wrappers\time_limit.py:50, in TimeLimit.step(self, action)
39 def step(self, action):
40 """Steps through the environment and if the number of steps elapsed exceeds ``max_episode_steps`` then truncate.
41
42 Args:
(...) 48
49 """
---> 50 observation, reward, terminated, truncated, info = self.env.step(action)
51 self._elapsed_steps += 1
53 if self._elapsed_steps >= self._max_episode_steps:

File ~\ml\env\Lib\site-packages\gym\wrappers\order_enforcing.py:37, in OrderEnforcing.step(self, action)
35 if not self._has_reset:
36 raise ResetNeeded("Cannot call env.step() before calling env.reset()")
---> 37 return self.env.step(action)

File ~\ml\env\Lib\site-packages\gym\wrappers\env_checker.py:37, in PassiveEnvChecker.step(self, action)
35 if self.checked_step is False:
36 self.checked_step = True
---> 37 return env_step_passive_checker(self.env, action)
38 else:
39 return self.env.step(action)

File ~\ml\env\Lib\site-packages\gym\utils\passive_env_checker.py:233, in env_step_passive_checker(env, action)
230 obs, reward, terminated, truncated, info = result
232 # np.bool is actual python bool not np boolean type, therefore bool_ or bool8
--> 233 if not isinstance(terminated, (bool, np.bool8)):
234 logger.warn(
235 f"Expects `terminated` signal to be a boolean, actual type: {type(terminated)}"
236 )
237 if not isinstance(truncated, (bool, np.bool8)):

File ~\ml\env\Lib\site-packages\numpy\__init__.py:427, in __getattr__(attr)
424 import numpy.char as char
425 return char.chararray
--> 427 raise AttributeError("module {!r} has no attribute "
428 "{!r}".format(__name__, attr))

AttributeError: module 'numpy' has no attribute 'bool8'



Frozen Lake 환경 문제 해결 (AttributeError: numpy has no attribute 'bool8')


문제 개요


  • Frozen Lake 환경 실행 시 다음과 같은 에러가 발생
AttributeError: module 'numpy' has no attribute 'bool8'



문제 원인


  • numpy 최신 버전(1.24 이상)에서는 np.bool8 타입이 제거되었습니다.

  • gym 라이브러리는 내부적으로 np.bool8 타입을 사용하고 있어 호환성 문제가 발생합니다.

  • gymnumpy 버전 충돌로 인해 발생하는 문제입니다.



해결 방법


1. [권장] gymnumpy를 호환되는 버전으로 설치


  • gym: 0.26.2

  • numpy: 1.23.5

명령어

pip install gym==0.26.2
pip install gym[toy_text]==0.26.2
pip install numpy==1.23.5



2. 가상 환경 설정 (옵션)


  • 새로운 가상환경에서 패키지를 설치하여 버전 충돌 문제를 방지합니다.
# 새로운 가상환경 생성
python -m venv rl_env
source rl_env/bin/activate  # Windows: rl_env\Scripts\activate

# 패키지 설치
pip install gym==0.26.2
pip install gym[toy_text]==0.26.2
pip install numpy==1.23.5
pip install pygame  # 렌더링용



3. 설치 확인 및 테스트


  • 아래 코드를 실행하여 환경이 정상 작동하는지 확인하세요:
import numpy as np
import gym

# FrozenLake 환경 생성
env = gym.make("FrozenLake-v1", is_slippery=False)
state, _ = env.reset()
print("FrozenLake 환경 정상 작동")



4. 임시 수정 방법 (추천하지 않음)


  • 패키지 버전 변경이 어려울 경우, gym 소스코드에서 np.bool8np.bool_로 수동 수정할 수 있습니다.

경로 확인


gym 패키지 경로는 다음 명령어로 확인 가능합니다:

pip show gym



소스코드 수정


  • 설치된 경로로 이동해 gym 관련 파일을 열고, np.bool8np.bool_로 변경합니다.

  • 이 방법은 이후 패키지 업데이트 시 다시 문제가 발생할 수 있으므로 권장하지 않습니다.



profile
https://velog.io/@corone_hi/posts

0개의 댓글