Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: len is not well defined for symbolic Tensors. (activation_3/Identity:0) Please call `x.shape` rather than `len(x)` for shape information

I am trying to implement a DQL model on one game of openAI gym. But it's giving me following error.

TypeError: len is not well defined for symbolic Tensors. (activation_3/Identity:0) Please call x.shape rather than len(x) for shape information.

Creating a gym environment:

ENV_NAME = 'CartPole-v0'

env = gym.make(ENV_NAME)
np.random.seed(123)
env.seed(123)
nb_actions = env.action_space.n

My model looks like this:

model = Sequential()
model.add(Flatten(input_shape=(1,) + env.observation_space.shape))
model.add(Dense(16))
model.add(Activation('relu'))
model.add(Dense(nb_actions))
model.add(Activation('linear'))
print(model.summary())

Fitting that model to DQN model from keral-rl as follows:

policy = EpsGreedyQPolicy()
memory = SequentialMemory(limit=50000, window_length=1)
dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=10, target_model_update=0.001, policy=policy)
dqn.compile(Adam(lr=1e-3), metrics=['mse', 'mae'])
dqn.fit(env, nb_steps=5000, visualize=False, verbose=3)

The error is from this line:

dqn = DQNAgent(model=model, nb_actions=nb_actions, memory=memory, nb_steps_warmup=10, target_model_update=0.001, policy=policy)

I am using keras-rl==0.4.2 and tensorflow==2.1.0. Based on other answers, I also tried tensorflow==2.0.0-beta0 but it doesn't solve the error.

Can someone please explain to me why I am facing this error? and how to solve it?

Thank you.

like image 440
vivekpadia70 Avatar asked Jan 10 '20 13:01

vivekpadia70


2 Answers

The reason this breaks is because, tf.Tensor TF 2.0.0 (and TF 1.15) has the __len__ overloaded and raises an exception. But TF 1.14 for example doesn't have the __len__ attribute.

Therefore, anything TF 1.15+ (inclusive) breaks keras-rl (specifically here), which gives you the above error. So you got two options,

  • Downgrade to TF 1.14 (recommended)
  • Delete the __len__ overloading in TensorFlow source (not recommended as this can break other things)
like image 64
thushv89 Avatar answered Oct 17 '22 18:10

thushv89


a temporal solution for me was to change this line

if hasattr(model.output, '__len__') and len(model.output) > 1:

in the agentfile, where the error come from, in my case dqn.py to :

if hasattr(model.output, '__len__') and len([model.output.shape.dims.__len__()]) > 1:
like image 23
Nie Mand Avatar answered Oct 17 '22 16:10

Nie Mand