莫烦强化学习github中的内容是用tensorflow1实现的,下面总结了如何使用tensorflow2来运行莫烦的强化学习代码。
我使用的是tensorflow==2.10.0
pyglet==1.2.4
gym==0.19.0
1. import tensorflow
改为
import tensorflow.compat.v1 as tf
tf.compat.v1.disable_eager_execution()
2. 由于tf2没有contrib库,需要将w_init = tf.contrib.layers.xavier_initializer()
改为w_init = tf.keras.initializers.glorot_normal()
3. normal_dist = tf.contrib.distributions.Normal(mu, sigma)出错,也是由于contrib库造成。
解决方法:首先pip install tensorflow-probability==0.15.0
然后import tensorflow_probability as tfp
将normal_dist = tf.contrib.distributions.Normal(mu, sigma)改为normal_dist = tfp.distributions.Normal(mu, sigma)
4. rnn_cell = tf.contrib.rnn.BasicRNNCell(cell_size)报错,也是由于contrib库造成。
将代码改成rnn_cell = tf.nn.rnn_cell.BasicRNNCell(cell_size)
5. 在运行Solve_BipedalWalker代码时,报错:Env BipedalWalker-v2 not found (valid versions include ['BipedalWalker-v3'])
解决方法:将ENV_NAME = 'BipedalWalker-v2'
改为ENV_NAME = 'BipedalWalker-v3'
6. module 'gym.envs.box2d' has no attribute 'BipedalWalker'
step1:下载swig包。在https://www.swig.org/download.html 下载swig包,解压保存在某路径,如D:\swigwin-4.1.1。
step2:添加环境变量.
pycharm中file -> settings -> Build, Execution, Deployment -> Console -> Python Console -> Environment variables 输入swig=D:\swigwin-4.1.1\swig.exe,确认。
step3: 下载Box2D包。
在https://www.lfd.uci.edu/~gohlke/pythonlibs/#pybox2d下载适合的版本的Box2D包。我下载的是Box2D-2.3.10-cp39-cp39-win_amd64.whl
step4:安装wheel。
打开cmd,在激活的环境中打开存储的whl的路径,执行
pip install Box2D-2.3.10-cp39-cp39-win_amd64.whl