AttributeError: 'Sequential' object has no attribute 'model'

Question:

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

I am trying to understand Double Deep Q-Learning. There is a pretty good lecture here: https://github.com/philtabor/Youtube-Code-Repository/tree/master/ReinforcementLearning/DeepQLearning

But when I tried to run the code, I got following errors:

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'

And I have no clue on how to fix this. I guess keras has been updated to not allow this?

The different lines are respectively:

line 33:

ddqn_agent.learn()

line 118 (in def learn(self):):

self.update_network_parameters()

line 121 (in def update_network_parameters(self):):

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

line 76:

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

EDIT: updated the problem based on suggestions in the comment section. The suggestion was that I put a tensforflow. in front of keras in the imports. I get the same error as before (as you can see). Here is how the imports look like now:

enter image description here

Asked By: user17350567

||

Answers:

For solving your error you can go through the below steps:

1. Install dependency for can run the 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. Change imports like below:

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

3. Install tf-nightly: (what is tf-nightly)

!pip install tf-nightly
Answered By: I'mahdi