Dependencies(探索出可行的版本)
cuda==11.0
pytorch==1.7.0
torchvision==0.8.0
torchaudio==0.7.0
cudatoolkit=11.0
python==3.8.13
advertorch==0.2.3
tianshou==0.3.1
atari-py==0.2.9
gym==0.18.0
(atari-py需要导入Rom)
1、环境配置BUG
项目地址:https://github.com/davide97l/rl-policies-attacks-defenses。GitHub没有写环境版本,项目GitHub的issues里有人问环境版本,但是作者没有回答。
用xshell连接Ubuntu服务器,安装了Anaconda3,配置CUDA环境变量,安装Pytorch,配置jupyter notebook。xftp修改代码不方便,于是找了教程用vscode连接实验室的服务器。连好服务器后,在jupyter notebook中运行代码。
error:cannot import name ‘zero_gradients’ from ‘torch.autograd.gradcheck’ ,网上说是旧版的pytorch有这个函数,但是新版pytorch没有,可以在新版中添加这个函数,但是添加了也太行,那就更换pytorch的版本。
尝试在base环境中更换pytorch版本为1.7.0,然后:
error:failed with initial frozen solve. Retrying with flexible solve.
https://blog.csdn.net/weixin_38419133/article/details/115863940
所以新建了一个conda虚拟环境,在新虚拟环境中先把cuda环境变量改成了10.1,然后装了pytorch1.7.0
conda切换虚拟环境之后:
error:Command ‘jupyter’ not found, but can be installed with:
安装nb_conda后能打开jupyter notebook
pip安装了requirements(默认安装的都是最新版的):
advertorch,gym,opencv-python,atari-py
然后在jupyter notebook运行代码遇到如下问题:
error:cannot import name ‘ListReplayBuffer’ from 'tianshou.data’
更换tianshou版本:https://pypi.org/project/tianshou/#history
error:TqdmWarning: IProgress not found. Please update jupyter and ipywidgets.
https://blog.csdn.net/buluxianfeng/article/details/123316251
error:NameNotFound: Environment PongNoFrameskip doesn’t exist.
https://stackoverflow.com/questions/69442971/error-in-importing-environment-openai-gym
pip install gym==0.18.0
Exception: ROM is missing for pong, see https://github.com/openai/atari-py#roms for instructions
python -m atari_py.import_roms jupyter/Roms
work!
2、如何指定GPU
查看GPU情况:https://blog.csdn.net/dcrmg/article/details/78146797。
pytorch指定gpu:https://blog.csdn.net/LLY_A_/article/details/119488452
https://blog.csdn.net/weixin_43402775/article/details/109223794
UserWarning:NVIDIA GeForce RTX 3080 Ti with CUDA capability sm_86 is not compatible with the current PyTorch installation.The current PyTorch install supports CUDA capabilities sm_37 sm_50 sm_60 sm_61 sm_70 sm_75 compute_37.
https://blog.csdn.net/weixin_44120025/article/details/121747212
算力为8.x的显卡使用的cuda版本应该大于等于11.0
有的函数要用到相对路径,因此要:%cd jupyter/rl-policies-attacks-defenses-master
代码中指定GPU:device = 'cuda:1' if torch.cuda.is_available() else 'cpu’
查看、关闭当前服务器上进程:https://blog.csdn.net/jiangyu1013/article/details/82984210
3、用PPO训练chopper command
Train PPO agent to play ChopperCommand.
(14:35~19:12,训练了5h左右;19:55测试完,测试用了1h左右;用的GPU1)
python atari_a2c_ppo.py --env-name "ChopperCommandNoFrameskip-v4" --algo ppo --use-gae --lr 2.5e-4 --clip-param 0.1 --value-loss-coef 0.5 --num-processes 8 --num-steps 128 --num-mini-batch 4 --log-interval 1 --use-linear-lr-decay --entropy-coef 0.01 --cuda-deterministic
Test PPO agent playing ChopperCommand:python atari_a2c_ppo.py --env-name "ChopperCommandNoFrameskip-v4" --algo ppo --resume_path "log/ChopperCommandNoFrameskip-v4/ppo/policy.pth" --watch --test_num 10
测试1:
最终测试:
后来上paperswithcode看sota,最好的模型的Score竟可以上万!不过这里的mean reward和score真的一样吗?paperswithcode.com/task/atari-games
经常出现警告:Visual Studio Code is unable to watch for file changes in this large workspace
。应该2个程序一起训练vscode看不过来了?
还有error:Bus error (core dumped)
Train DQN agent to play ChopperCommand.
(用的GPU2)
python atari_dqn.py --task ChopperCommandNoFrameskip-v4
vscode 连接服务器,为什么跑一会程序就自己中断了?休眠时间更改为“永不”、断国外VPN、改用流量……都不行。应该是图书馆wifi又差了,经过1个小时又连上了。
Test DQN agent playing ChopperCommand:.python atari_dqn.py --resume_path "log/ChopperCommandNoFrameskip-v4/dqn/policy.pth" --watch --test_num 10 --task "PongNoFrameskip-v4”
生成GIF动画
方法: