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
Feature request make it easier to supply custom model #457
Comments
Hi @ben-arnao . We have a new way to declare Networks, including preprocessing layers, that is more flexible and may work better in your case. We have a new Here's an example of Sequential with regular DQN. Here's an example of Sequential with NestMap. NestMap can take the new Keras preprocessing layers as well. Let me know if this is interesting to you; we can get a PR out that makes it possible to use these with Categorical DQN, including a unit test example. |
(in the case of your own Sequential layer; you provide the final output layer which projects to |
@ebrevdo Yes that would be great! I could probably find a way to make it work i just wanted to avoid writing too much custom code if possible. |
Are there any plans to unify |
One fundamental issue with If you want to use a |
@sguada we should see who can modify the existing CategoricalDQN unit tests / example to use the new Sequential networks, at which point we can link to that here and consider the issue resolved. Who would you suggest? |
@ebrevdo Thanks for clarifying (and for the quick response)! I guess I didn't know |
I'm having some trouble passing a model to
Here's the keras
I have a feeling this has something to do with the I'll keep digging around in the code to see if I can figure out where this error is coming from, but in the meantime if you guys have any examples or tips that would be much appreciated! (Also I'm not really sure where to go for help on this. If this is the wrong place feel free to let me know) Update: from tf_agents.environments import tf_py_environment
from tf_agents.environments import suite_gym
from tf_agents.specs import tensor_spec
import tensorflow as tf
import tf_agents
def create_keras_agent(env):
input = tf.keras.layers.Input(name="input", shape=[4])
output = tf.keras.layers.Dense(name="q_vals", units=2)(input)
model = tf.keras.Model(
name="f32_logits_qnet", inputs=input, outputs=output
)
# calling with constant works fine
print(model(tf.constant([[0.0, 0.0, 0.0, 0.0]])))
model.summary()
# It kinda looks like Network.create_variables is doing something like this
random_input = tensor_spec.sample_spec_nest(
env.time_step_spec().observation, outer_dims=(1,)
)
print(random_input)
# But it ends up working no problem
print(model(random_input))
qnet = tf_agents.networks.sequential.Sequential([model])
# AssertionError here:
tf_agents.agents.dqn.dqn_agent.DqnAgent(
env.time_step_spec(),
env.action_spec(),
name="f32_logits_agent",
q_network=qnet,
optimizer=tf.keras.optimizers.Adam(),
td_errors_loss_fn=tf_agents.utils.common.element_wise_squared_loss
)
env = suite_gym.load("CartPole-v0")
env = tf_py_environment.TFPyEnvironment(env)
create_keras_agent(env) Update 2 Assuming my guess is correct, I would prefer to keep using the functional API, but I understand if it won't be supported. Any ideas on how I could get this to work or am I stuck with the sequential API unless I write my own custom |
That sucks. Looks like @awestlake87 for now you can work around this by creating the
The key thing is that the q_network and target cannot share the same |
Another issue that will come up. For In order to handle this, you'll need to wrap your model in a layer that knows to automatically flatten the |
@sguada looks like we'll need to provide a README somewhere on how to use |
Thanks for the heads up on the input shape! That'll save me some time. I'll try creating the |
That appears to have worked! It looks like it's working exactly the same as it was when I was just adding the dense layers to the |
Just as an update, when I started trying to create an agent for Breakout, I had to remove the Edit: |
For images looks like you may need inner_rank=3? Anyway glad it's working for you! I'll mark as closed for now but reopen if anything comes up. |
I tried assigning my own layers to the post_processing variable within my categorical qnetwork but i get a message that weights are shared. when i try to use then create my categorical dqn agent. It would be nice if the main categorical q network constructor allowed a parameter for you to provide a set of keras layers where the q_layer is just appended to the end like it is the the encoding network scheme. The weights will be copied for you.
The text was updated successfully, but these errors were encountered: