Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Where is the hidden state in the DRQN code? #54

Open
evbtst opened this issue Sep 16, 2023 · 1 comment
Open

Where is the hidden state in the DRQN code? #54

evbtst opened this issue Sep 16, 2023 · 1 comment
Labels
question Further information is requested

Comments

@evbtst
Copy link

evbtst commented Sep 16, 2023

Hello!

First of all, I want to commend your code, it's excellent! Thank you very much for your work!

However, I have a question regarding the file Chapter03/4_drqn.py. Shouldn't it be possible to access the hidden states of the LSTM? Also, shouldn't it be reset at the beginning of each epoch? I looked for this in the book as well, and it wasn't clear to me.

@praveen-palanisamy praveen-palanisamy added the question Further information is requested label Sep 19, 2023
@praveen-palanisamy
Copy link
Collaborator

Hi @evbtst,
Thank you for your kind words!

  1. Shouldn't it be possible to access the hidden states of the LSTM?

Yes, it's possible to access the hidden state of the LSTM that the Agent's model uses.

Based on this definition:

def nn_model(self):
return tf.keras.Sequential(
[
Input((args.time_steps, self.state_dim)),
LSTM(32, activation="tanh"),
Dense(16, activation="relu"),
Dense(self.action_dim),
]
)

that the Agent uses for its model and target_model, you can access the hidden states using the following:

agent_hidden_states = agent.model.model.layers[1].states

You can inspect the state in your IDE or print it to console using:

for state in agent_hidden_states:
    print(state.numpy()
  1. Also, shouldn't it be reset at the beginning of each epoch?

Good question! By default, the LSTM layer we use has stateful set to False, which is the typical use-case (unless we intentionally want to carryover states from the previous batch as a way to process longer sequences). This means that, after each batch of predictions (or training), the hidden states are reset.
There is no explicit state reset used in the code but if you would like to experiment, you could do so by using: agent.model.model.layers[1].reset_states()

I hope that gives you clarifications that you were looking for.
Thank you for your interest in the book and this code repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Development

No branches or pull requests

2 participants