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

和自甴很熟 提交于 2020-05-14 16:00:29

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/59682542/typeerror-len-is-not-well-defined-for-symbolic-tensors-activation-3-identity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!