keras-rl: len is not well defined for symbolic tensors *AND* using a `tf.Tensor` as a Python `bool` is not allowed in Graph execution

There is an error in the error-handling of the ddpg.py and dqn.py agents in keras-rl/rl/agents while using Tensorflow 2.0, Keras 2.3.1, Keras-rl 0.4.2 dqn.py line 108 ddpg.py line 29, 31 It comes about by calling len(model.output)

Error:

Traceback (most recent call last):
  File "foo.py", line x, in <module>
    agent = DDPGAgent(...)
  **File "foo\ddpg.py", line 29, in __init__
    if hasattr(actor.output, '__len__') and len(actor.output) > 1:**
  File "foo\ops.py", line 741, in __len__
    "shape information.".format(self.name))
TypeError: len is not well defined for symbolic Tensors. (dense_5/BiasAdd:0) Please call `x.shape` rather than `len(x)` for shape information.

Possible solution: What I’ve been using as a fix is summing the shape of the tensor: sum(x is not None for x in model.output.shape) Implementation example: if hasattr(model.output, '__len__') and sum(x is not None for x in model.output.shape) > 1:

There is then another error in ddpg.py at line 130: if i == self.critic_action_input: Error: tensorflow.python.framework.errors_impl.OperatorNotAllowedInGraphError: using a tf.Tensoras a Pythonbool is not allowed in Graph execution. Use Eager execution or decorate this function with @tf.function. Including

import tensorflow as tf
tf.compat.v1.enable_eager_execution()

Does not seem to help, I have also tried creating a session and using tf.equal(i, self.critic_action_input).eval(session=sess) but I’m having issues, as of now I’ve tried

import tensorflow as tf

with tf.compat.v1.Session(graph=self.critic_action_input.graph) as sess:
            for i in self.critic.
                if tf.equal(i, self.critic_action_input).eval(session=sess): #if i == self.critic_action_input:
                    combined_inputs.append([])
                else:
                    combined_inputs.append(i)
                    critic_inputs.append(i)

But I cannot get it to work

Thank you

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 8
  • Comments: 16

Commits related to this issue

Most upvoted comments

Why has the change not been added to the package to make it compatible with tf>2.0?

I think all of you may have mistaken what this library was capable of doing. It’s a Keras module!

It can only work by using import keras instead of from tensorflow import keras.

please upgrade the code to make it compatible with Tensorlfow 2.0+. Thank you !

Hi,

I have run some test the code and I got the following error when initializing the class DQNAgent

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/python-3.7.6/lib/python3.7/site-packages/rl/agents/dqn.py", line 108, in __init__
  if hasattr(model.output, '__len__') and len(model.output) > 1:
File "/opt/python-3.7.6/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 733, in __len__
  "shape information.".format(self.name))
TypeError: len is not well defined for symbolic Tensors. (activation_4/Identity:0) Please call `x.shape` rather than `len(x)` for shape information.

I have fixed the error by changing the specified line of code (i.e., 108) with:

if hasattr(model.output, '__shape__') and len(model.output.shape) > 2:

I hope this is a correct fix.

All the best, Ciprian

P.S. I use

Keras==2.3.1

with the backend

tensorflow==2.1.0
tensorflow-estimator==2.1.0

This fork does support TF2: https://github.com/wau/keras-rl2

https://github.com/keras-rl/keras-rl/issues/348#issuecomment-596560902 Thanks @cipriantruica , this resolved the issue for me.

keras_rl==0.4.2
Keras==2.2.4
numpy==1.16.2
tensorflow==1.15.2
gym==0.12.1
rl==3.0

Note: the if hasattr(model.output, '__len__') and sum(x is not None for x in model.output.shape) > 1: is pretty redundant since these models do not have an attribute len

For those that would like a “solution”, downgrade Tensorflow[1.13.1] and/or Keras[2.2.4], see https://github.com/apple/coremltools/issues/462#issuecomment-536929862

if hasattr(model.output, '__shape__') and len(model.output) > 1: is ok.