python 属性错误:"Sequential"对象没有属性"model"

edqdpe6u  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(378)
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.optimizers import Adam

def build_dqn(lr, n_actions, input_dims, fc1_dims, fc2_dims):
    model = Sequential([
        Dense(fc1_dims, input_shape=(input_dims,)),
        Activation('relu'),
        Dense(fc2_dims),
        Activation('relu'),
        Dense(n_actions)])

    model.compile(optimizer=Adam(lr=lr), loss='mse')

    return model

我正在努力理解双重深度Q学习。这里有一个很好的讲座:https://github.com/philtabor/Youtube-Code-Repository/tree/master/ReinforcementLearning/DeepQLearning
但是当我尝试运行代码时,我得到了以下错误:

Traceback (most recent call last):
  File "/home/panda/PycharmProjects/ddqn/main.py", line 33, in <module>
    ddqn_agent.learn()
  File "/home/panda/PycharmProjects/ddqn/ddqn_keras.py", line 118, in learn
    self.update_network_parameters()
  File "/home/panda/PycharmProjects/ddqn/ddqn_keras.py", line 121, in update_network_parameters
    self.q_target.model.set_weights(self.q_eval.model.get_weights())
AttributeError: 'Sequential' object has no attribute 'model'

而且我对如何修复这个毫无头绪,我猜keras已经更新为不允许这样做了?
不同的行分别为:

第33行:

ddqn_agent.learn()

第118行(单位:def learn(self):):

self.update_network_parameters()

第121行(以def update_network_parameters(self):为单位):

self.q_target.model.set_weights(self.q_eval.model.get_weights())

第76行:

self.q_target = build_dqn(alpha, n_actions, input_dims, 256, 256)

**EDIT:**根据评论部分的建议更新了这个问题。建议是我在导入中的keras前面放了一个tensforflow.。我得到了和以前一样的错误(正如你所看到的)。下面是导入现在的样子:

i7uq4tfw

i7uq4tfw1#

要解决您的错误,您可以通过以下步骤:

1.安装的依赖项可以运行env

!pip install https://github.com/pybox2d/pybox2d/archive/refs/tags/2.3.10.tar.gz
!pip install box2d-py
!pip install gym[all]
!pip install gym[box2d]

2.更改导入,如下所示:

from keras.layers import Dense, Activation
from keras import Sequential
from keras.models import load_model
from tensorflow.keras.optimizers import Adam

3.安装tf-nightlywhat is tf-nightly

!pip install tf-nightly

相关问题