2
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

I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • Before any `keras` adding `tensorflow` like: `from tensorflow.keras.layers import Dense, Activation from tensorflow.keras.models import Sequential, load_model from tensorflow.keras.optimizers import Adam` – I'mahdi Jun 16 '22 at 07:59
  • You can also use `Sequential` without `models` like : `import tensorflow as tf` next line: `tf.keras.Sequential([....])` – I'mahdi Jun 16 '22 at 08:02
  • @I'mahdi thanks, I tried it, it didn't work. Anyway, when I type tensorflow.keras.layers the keras-part of the line is marked with red in PyCharm. I don't know, what I am doing wrong. –  Jun 16 '22 at 08:18
  • add new full code and new error with editing your question – I'mahdi Jun 16 '22 at 08:20
  • @I'mahdi thank you, I have updated this post with your suggestions. –  Jun 16 '22 at 08:35
  • @I'mahdi any ideas what I am doing wrong? –  Jun 16 '22 at 11:07
  • I need to see the full code. where do you crate ddqn_agent and ddqn_agent.learn() and ... – I'mahdi Jun 16 '22 at 11:09
  • The full code is here: https://github.com/philtabor/Youtube-Code-Repository/tree/master/ReinforcementLearning/DeepQLearning ... the entire code consists of the files dqqn_keras.py, main_keras_ddqn_lunar_lander.py and utils.py. The only thing I have changed is what you suggested (tensorflow.keras instead of keras). –  Jun 16 '22 at 12:30

1 Answers1

0

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
I'mahdi
  • 23,382
  • 5
  • 22
  • 30
  • 1
    thank you so much, I am truly grateful <3 –  Jun 16 '22 at 14:51
  • Didn't work in my case... – Caterina Dec 26 '22 at 16:22
  • @Caterina, you can ask your question with your problem and state that you got an error, maybe me or other people can help you. – I'mahdi Dec 26 '22 at 16:57
  • Isn't model attribute deprecated now? This is what I read so I wasn't able to find a solution. – Caterina Dec 27 '22 at 20:08
  • @Caterina, I Edited the answer, I checked the answer on [colab](https://colab.research.google.com/). You can read [this](https://stackoverflow.com/a/70323949/1740577) also. After installing restart runtime and if got an error run again `!pip`s. You can see this [picture](https://i.stack.imgur.com/z81jh.png) – I'mahdi Dec 28 '22 at 06:05
  • I tried that again and I have same error. The line raising the error is: if isinstance(model, Sequential): self.model = model.model and the error is: 'Sequential' object has no attribute 'model' – Caterina Dec 28 '22 at 15:08
  • I created a new post here: https://stackoverflow.com/questions/74941909/sequential-object-has-no-attribute-model – Caterina Dec 28 '22 at 15:14