티스토리 뷰

Study/AI

[RL] Windows에서 Dopamine 설치

생각많은 소심남 2021. 12. 22. 18:01

Dopamine

Dopamine은 Google Research에서 만든 강화학습 실험용 Framework이다. 사실 이 툴의 취지는 SW 개발적인 측면보다는 강화학습 알고리즘 개발에 치중할 수 있도록 환경과 에이전트 설정을 configuration화시킨 것이다. 그래서 소개란에도 "Fast prototyping of reinforcement learning algorithm"이라고 되어 있다. Framework에 대한 논문은 아래에 있다.

 

Dopamine: A Research Framework for Deep Reinforcement Learning

Deep reinforcement learning (deep RL) research has grown significantly in recent years. A number of software offerings now exist that provide stable, comprehensive implementations for benchmarking. At the same time, recent deep RL research has become more

arxiv.org

github에도 소개되어 있지만 Dopamine의 특징은 다음과 같이 정의되어 있다.

 

GitHub - google/dopamine: Dopamine is a research framework for fast prototyping of reinforcement learning algorithms.

Dopamine is a research framework for fast prototyping of reinforcement learning algorithms. - GitHub - google/dopamine: Dopamine is a research framework for fast prototyping of reinforcement learn...

github.com

  • 쉬운 실험(Easy experimentation): 새로운 사용자가 벤치마크 실험을 쉽게 돌릴 수 있도록 함.
  • 유연한 개발(Flexible development): 연구 아이디어를 쉽게 시도해볼 수 있도록 도와줌
  • 경량화되어 있고 안정적(Compact and Reliable): 몇몇 큰 실험이 필요한 알고리즘을 위한 구현을 미리 제공함
  • 재생성(Reproducible): 결과에 대해서 재생성을 부여한다. (개인적으로 생각해도 중요한 부분이긴 한데, 강화학습 자체가 워낙 환경의 초기 상태나 hyperparameter의 영향을 많이 받아, 결과가 들쭉날쭉한 부분이 있다. 이 framework는 Revisiting the Arcade Learning Environment 논문에서 언급한 환경 설정을 그대로 따르고 있다.

내부 코드를 살펴보면 알겠지만 몇몇 기본 에이전트(DQN, C51, Rainbow, IQN, SAC)들이 미리 구현되어 있고, 최신 트렌드에 맞춰서 jax로 구현되어 있다. 

이게 범용적으로 쓸수 있으면 참 좋은데, 리소스의 제한때문에 공식 지원은 Linux와 Mac에서만 지원한다. Windows에서는 아마 설치할경우, jax 와 jaxlib을 설치하면서 오류가 난다. 그래서 이 framework설치도 이전에 언급했던 mujoco_py와 동일하게 몇가지 설정을 좀 해줘야 한다. 우선은 내 환경은 다음과 같다.

  • Windows 10 x64
  • Python 3.7 (Anaconda)
  • Nvidia RTX 2080 TI (w. CUDA v11.1 + cuDNN v8.0.5)

아래 글을 진행하기 전에 먼저 CUDA가 11.1이 설치되어 있는지 확인을 해본다. jax 설치시 현재 제공되는 library는 CUDA 11.1 에서만 정상적으로 진행되는 듯 하다.

우선 dopamine repo를 clone해온다.

git clone https://github.com/google/dopamine
cd dopamine

원래는 pip install을 통해서 설치하나 아마 windows 사용자라면 중간에 jaxlib 설치시 오류가 발생한다. 이는 windows에서는 jax를 공식적으로 지원하지 않기 때문에 나타난다. 그래서 정식적인 방법을 택하자면 다시 jax repository를 받아서 소스 빌드를 해줘야한다. 다행히 어떤 사람이 이를 GPU 설정과 맞춰서 미리 빌드한 파일을 공유해줬다. 대신 CUDA 버전은 이 사람이 설정한 것과 동일한 방식대로 해줘야 한다. link로 들어가서 자신의 환경에 맞는 whl파일을 찾는다. 나같은 경우에는 앞에서 언급한것처럼 python 3.7에 cuda11.1을 사용하고 있으므로 cuda111/jaxlib-0.1.75+cuda111-cp37-none-win_amd64.whl 를 받아서 설치했다. 배포관련 문서는 다음 link를 참고한다.

여기까지 수행하면 아마 jax와 jaxlib이 설치가 되는데, 이후에 dopamine에 필요한 package들을 설치해준다.

pip install -r requirements.txt

여기에서도 문제가 발생하는데 이때 설치되는 numpy가 1.19로 아마 이 버전을 windows에서 설치할 경우에는 연산 오류가 발생한다. 그래서 애초에 requirements.txt에 있는 numpy 버전을 1.21 이상으로 하던가 아니면 설치후 numpy를 upgrade해준다. 추가로 이때 설치되는 tensorflow-gpu가 2.4인데, 후에 진행할 unit_test에서 발생하는 오류를 막기위해 tensorflow도 upgrade해준다.

pip install tensorflow-gpu==2.5
pip install --upgrade numpy

이렇게 하면 기본적으로 dopamine을 실행할 수 있는 환경이 되었다. 이제 다음 명령을 통해서 정상적으로 test가 통과하는지 확인하면 된다.

python -m tests.dopamine.atari_init_test

그러면 결과는 아래와 같이 쭉 나오게 된다.

(base) G:\git\dopamine>python -m tests.dopamine.atari_init_test
2021-12-22 14:31:42.286301: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudart64_110.dll
WARNING:tensorflow:From C:\Users\kcsgo\anaconda3\lib\site-packages\tensorflow\python\compat\v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
Running tests under Python 3.7.10: C:\Users\kcsgo\anaconda3\python.exe
[ RUN      ] AtariInitTest.test_atari_init
2021-12-22 14:31:45.925448: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-12-22 14:31:45.929346: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library nvcuda.dll
2021-12-22 14:31:45.957272: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1733] Found device 0 with properties:
pciBusID: 0000:09:00.0 name: NVIDIA GeForce RTX 2080 Ti computeCapability: 7.5
coreClock: 1.815GHz coreCount: 68 deviceMemorySize: 11.00GiB deviceMemoryBandwidth: 573.69GiB/s
2021-12-22 14:31:45.957393: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudart64_110.dll
2021-12-22 14:31:46.000831: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cublas64_11.dll
2021-12-22 14:31:46.000933: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cublasLt64_11.dll
2021-12-22 14:31:46.022449: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cufft64_10.dll
2021-12-22 14:31:46.028505: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library curand64_10.dll
2021-12-22 14:31:46.081760: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cusolver64_11.dll
2021-12-22 14:31:46.098168: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cusparse64_11.dll
2021-12-22 14:31:46.099267: I tensorflow/stream_executor/platform/default/dso_loader.cc:53] Successfully opened dynamic library cudnn64_8.dll
2021-12-22 14:31:46.099402: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1871] Adding visible gpu devices: 0
2021-12-22 14:31:46.566825: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1258] Device interconnect StreamExecutor with strength 1 edge matrix:
2021-12-22 14:31:46.566941: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1264]      0
2021-12-22 14:31:46.566983: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1277] 0:   N
2021-12-22 14:31:46.567225: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1418] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 8958 MB memory) -> physical GPU (device: 0, name: NVIDIA GeForce RTX 2080 Ti, pci bus id: 0000:09:00.0, compute capability: 7.5)
I1222 14:31:46.569078 30472 dqn_agent.py:151] Creating DQNAgent agent with the following parameters:
I1222 14:31:46.569078 30472 dqn_agent.py:152]    gamma: 0.990000
I1222 14:31:46.569078 30472 dqn_agent.py:153]    update_horizon: 1.000000
I1222 14:31:46.569078 30472 dqn_agent.py:154]    min_replay_history: 20000
I1222 14:31:46.570078 30472 dqn_agent.py:155]    update_period: 4
I1222 14:31:46.570078 30472 dqn_agent.py:156]    target_update_period: 8000
I1222 14:31:46.570078 30472 dqn_agent.py:157]    epsilon_train: 0.010000
I1222 14:31:46.570078 30472 dqn_agent.py:158]    epsilon_eval: 0.001000
I1222 14:31:46.570078 30472 dqn_agent.py:159]    epsilon_decay_period: 250000
I1222 14:31:46.570078 30472 dqn_agent.py:160]    tf_device: /gpu:0
I1222 14:31:46.570078 30472 dqn_agent.py:161]    use_staging: False
I1222 14:31:46.570078 30472 dqn_agent.py:162]    optimizer: <tensorflow.python.training.rmsprop.RMSPropOptimizer object at 0x000002697DE197C8>
I1222 14:31:46.570078 30472 dqn_agent.py:164]    max_tf_checkpoints_to_keep: 4
I1222 14:31:46.572079 30472 circular_replay_buffer.py:155] Creating a OutOfGraphReplayBuffer replay memory with the following parameters:
I1222 14:31:46.572079 30472 circular_replay_buffer.py:156]       observation_shape: (84, 84)
I1222 14:31:46.572079 30472 circular_replay_buffer.py:157]       observation_dtype: <class 'numpy.uint8'>
I1222 14:31:46.572079 30472 circular_replay_buffer.py:158]       terminal_dtype: <class 'numpy.uint8'>
I1222 14:31:46.572079 30472 circular_replay_buffer.py:159]       stack_size: 4
I1222 14:31:46.572079 30472 circular_replay_buffer.py:160]       replay_capacity: 100
I1222 14:31:46.572079 30472 circular_replay_buffer.py:161]       batch_size: 32
I1222 14:31:46.572079 30472 circular_replay_buffer.py:162]       update_horizon: 1
I1222 14:31:46.572079 30472 circular_replay_buffer.py:163]       gamma: 0.990000
WARNING:tensorflow:From C:\Users\kcsgo\anaconda3\lib\site-packages\tensorflow\python\training\rmsprop.py:123: calling Ones.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
W1222 14:31:46.769077 30472 deprecation.py:534] From C:\Users\kcsgo\anaconda3\lib\site-packages\tensorflow\python\training\rmsprop.py:123: calling Ones.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.
Instructions for updating:
Call initializer instance with the dtype argument instead of passing it to the constructor
I1222 14:31:46.854077 30472 atari_lib.py:121] legacy_checkpoint_load: False
I1222 14:31:47.980077 30472 run_experiment.py:521] Beginning training...
W1222 14:31:47.980077 30472 run_experiment.py:524] num_iterations (0) < start_iteration(0)
INFO:tensorflow:time(__main__.AtariInitTest.test_atari_init): 2.84s
I1222 14:31:47.984078 30472 test_util.py:2103] time(__main__.AtariInitTest.test_atari_init): 2.84s
[       OK ] AtariInitTest.test_atari_init
[ RUN      ] AtariInitTest.test_session
[  SKIPPED ] AtariInitTest.test_session
----------------------------------------------------------------------
Ran 2 tests in 2.838s

OK (skipped=1)

 

내가 생각하는 dopamine의 장점은 굳이 코드를 뜯어서 hyperaparameter를 수정하지 않아도, gin이라는 것을 사용해서 설정할 수 있다는 것이다. 조금더 자세한 내용은 공식 사이트를 참고하면 좋을 것 같다.

 

Overview

Dopamine is a research framework for fast prototyping of reinforcement learning algorithms.

google.github.io

최근 논문을 살펴보면 dopamine으로 실험한 경우도 많이 있고, 어떤사람은 MAML을 dopamine으로 구현한 tutorial도 제공하는 것 같다. 나도 offline RL을 최근에 다룰 기회가 있어서 이것저것 해보다가 얻은 삽질을 좀 정리해놓으면 도움이 될거 같아서 한번 남겨봤다.

댓글