Tensorforce中Adam对象无_create_all_weights属性问题求助
你在M1 Mac和Windows 10的Python3.9虚拟环境中使用Tensorforce 0.6.5搭建PPOAgent训练Snake游戏时,遇到了Adam优化器的属性错误,具体报错为AttributeError: 'Adam' object has no attribute '_create_all_weights'和AttributeError: 'Adam' object has no attribute '_create_hypers',同时伴随TF 2.11+优化器在M1/M2上的性能警告。
解决方案
1. 降级TensorFlow到兼容版本
Tensorforce 0.6.5是较旧的版本,和TensorFlow 2.11+的API不兼容(新版Adam移除了_create_all_weights、_create_hypers这些内部方法)。执行以下命令降级TensorFlow:
pip install tensorflow==2.10.0
如果是M1 Mac,安装针对Apple Silicon的版本:
pip install tensorflow-macos==2.10.0 tensorflow-metal==0.6.0
2. 指定使用Legacy Adam优化器(无需降级TF)
在创建PPOAgent时,显式配置优化器为TF的legacy版本,适配Tensorforce 0.6.5的调用方式:
修改你的PPOAgent初始化代码,添加optimizer参数:
import tensorflow as tf agent = PPOAgent( states=environment.states(), actions=environment.actions(), batch_size=128, max_episode_timesteps=100, optimizer=dict( type='adam', optimizer=tf.keras.optimizers.legacy.Adam(learning_rate=0.001) ) )
3. 升级Tensorforce到最新版本(需适配API)
如果愿意调整代码,升级到最新版Tensorforce(当前稳定版为0.8.x),它已经适配了新版TensorFlow的优化器API。执行升级命令:
pip install --upgrade tensorforce
注意:新版Tensorforce的API有变动,比如PPOAgent的初始化参数、环境交互方法可能需要调整,需对应修改代码。
另外,针对开头的状态边界警告,可以在SnakeEnvironment的states()方法中补充状态的取值范围:
def states(self): # 根据你的游戏状态实际范围调整min_value和max_value return dict(type='float', shape=(你的状态维度), min_value=0.0, max_value=1.0)
内容的提问来源于stack exchange,提问作者eternal

