Keras:AttributeError:“Adam”对象没有属性“_name”

问题描述 投票:0回答:3

我想编译我的 DQN 代理,但出现错误:

AttributeError: 'Adam' object has no attribute '_name'
,

DQN = buildAgent(model, actions)
DQN.compile(Adam(lr=1e-3), metrics=['mae'])

我尝试添加假

_name
,但它不起作用,我正在遵循教程并且它可以在导师的机器上运行,这可能是一些新的更新更改,但如何解决这个问题

这是我的完整代码:

from keras.layers import Dense, Flatten
import gym
from keras.optimizer_v1 import Adam
from rl.agents.dqn import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory

env = gym.make('CartPole-v0')
states = env.observation_space.shape[0]
actions = env.action_space.n

episodes = 10

def buildModel(statez, actiones):
    model = Sequential()
    model.add(Flatten(input_shape=(1, statez)))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(actiones, activation='linear'))
    return model

model = buildModel(states, actions)

def buildAgent(modell, actionz):
    policy = BoltzmannQPolicy()
    memory = SequentialMemory(limit=50000, window_length=1)
    dqn = DQNAgent(model=modell, memory=memory, policy=policy, nb_actions=actionz, nb_steps_warmup=10, target_model_update=1e-2)
    return dqn

DQN = buildAgent(model, actions)
DQN.compile(Adam(lr=1e-3), metrics=['mae'])
DQN.fit(env, nb_steps=50000, visualize=False, verbose=1)
python tensorflow keras reinforcement-learning
3个回答
2
投票

您的错误来自于使用

Adam
导入
from keras.optimizer_v1 import Adam
,您可以使用
tf.keras.optimizers.Adam
中的
TensorFlow >= v2
解决您的问题,如下所示:

lr
参数已弃用,最好使用
learning_rate
代替。)

# !pip install keras-rl2
import tensorflow as tf
from keras.layers import Dense, Flatten
import gym
from rl.agents.dqn import DQNAgent
from rl.policy import BoltzmannQPolicy
from rl.memory import SequentialMemory

env = gym.make('CartPole-v0')
states = env.observation_space.shape[0]
actions = env.action_space.n
episodes = 10

def buildModel(statez, actiones):
    model = tf.keras.Sequential()
    model.add(Flatten(input_shape=(1, statez)))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(24, activation='relu'))
    model.add(Dense(actiones, activation='linear'))
    return model

def buildAgent(modell, actionz):
    policy = BoltzmannQPolicy()
    memory = SequentialMemory(limit=50000, window_length=1)
    dqn = DQNAgent(model=modell, memory=memory, policy=policy, 
                   nb_actions=actionz, nb_steps_warmup=10, 
                   target_model_update=1e-2)
    return dqn

model = buildModel(states, actions)
DQN = buildAgent(model, actions)
DQN.compile(tf.keras.optimizers.Adam(learning_rate=1e-3), metrics=['mae'])
DQN.fit(env, nb_steps=50000, visualize=False, verbose=1)

2
投票

我的 2 美分:使用旧版 keras 优化器!

#from tensorflow.keras.optimizers import Adam
from tensorflow.keras.optimizers.legacy import Adam

它适用于我的情况。


1
投票

#pip 安装 keras==2.11.0

#pip 安装tensorflow==2.11.0

dqn.compile(tf.keras.optimizers.legacy.Adam(learning_rate=1e-3), metrics=['mae'])

© www.soinside.com 2019 - 2024. All rights reserved.