Tensorflow-FailedPreconditionError: Could not find variable dense_24/bias. This could mean that the variable has been deleted

Tensorflow-FailedPreconditionError: Could not find variable dense_24/bias. This could mean that the variable has been deleted

我正在尝试使用 openai gym、tensorflow 和 keras 执行学习模型。

我用这种方法建立我的模型:

def build_model(states, actions):
model = Sequential()
model.add(Dense(24, activation='relu', input_shape=states))
model.add(Dense(24, activation='relu'))
model.add(Dense(actions, activation='linear'))
return model


model = build_model(states, actions)

之后,我构建我的代理:

def build_agent(model, actions):
policy = BoltzmannQPolicy()
memory = SequentialMemory(limit=50000, window_length=1)
dqn = DQNAgent(model=model, memory=memory, policy=policy,
              nb_actions=actions, nb_steps_warmup=10, target_model_update=1e-2)
return dqn

当我调用训练时:

dqn = build_agent(model, actions)
dqn.compile(Adam(lr=1e-3), metrics=['mae'])
dqn.fit(env, nb_steps=50000, visualize=True, verbose=1)

我得到这个错误,就是会话没有初始化或者没有dense_24/bias变量

---------------------------------------------------------------------------

FailedPreconditionError                   Traceback (most recent call last)

 <ipython-input-112-7ebc8b726721> in <module>()
   2 dqn.compile(Adam(lr=1e-3), metrics=['mae'])
   3 
-->4 dqn.fit(env, nb_steps=50000, visualize=True, verbose=1)
   5 

   4 frames

 /usr/local/lib/python3.7/dist-packages/tensorflow/python/client/session.py in __call__(self, *args, **kwargs)
   1483       # called before this destructor, in which case `self._session._session`
   1484       # will be `None`.
-> 1485       if (self._handle is not None and self._session._session is not None and
   1486           not self._session._closed):
   1487         tf_session.TF_SessionReleaseCallable(self._session._session,

   FailedPreconditionError: Could not find variable dense_24/bias. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status error message=Resource localhost/dense_24/bias/N10tensorflow3VarE does not exist.
 [[{{node dense_24/BiasAdd/ReadVariableOp}}]]

谁能帮我解决问题?

我认为是keras引用问题,你应该正确导入:

import tensorflow as tf
from tensorflow import keras

然后像这样创建一个模型:

model = keras.Sequential([
    keras.layers.Dense(24, activation='relu', input_shape=states))
    keras.layers.Dense(24, activation='relu'))
    keras.layers.Dense(actions, activation='linear'))
])

我没有试过你的整个代码,试一试并报告。