TypeError:len对于符号张量没有很好的定义. (activation_3/Identity:0)请致电`x.shape`而不是`len(x)`以获得形状信息 [英] TypeError: len is not well defined for symbolic Tensors. (activation_3/Identity:0) Please call `x.shape` rather than `len(x)` for shape information

查看:578
本文介绍了TypeError:len对于符号张量没有很好的定义. (activation_3/Identity:0)请致电`x.shape`而不是`len(x)`以获得形状信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在一项openAI体育馆游戏中实现DQL模型.但这给了我以下错误.

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

TypeError:对于符号张量,len定义不正确. (activation_3/Identity:0)请致电x.shape而不是len(x) 以获得形状信息.

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

创建体育馆环境:

ENV_NAME = 'CartPole-v0'

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

我的模型如下:

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())

按以下步骤从keral-rl中将该模型拟合为DQN模型:

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)

错误来自此行:

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

我正在使用keras-rl == 0.4.2和tensorflow == 2.1.0.根据其他答案,我也尝试了tensorflow == 2.0.0-beta0,但它不能解决错误.

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?

谢谢.

推荐答案

之所以中断,是因为tf.Tensor TF 2.0.0(和TF 1.15)具有__len__重载且

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.

因此,任何TF 1.15+(含)都破坏了keras-rl(特别是

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)

这篇关于TypeError:len对于符号张量没有很好的定义. (activation_3/Identity:0)请致电`x.shape`而不是`len(x)`以获得形状信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆