Home > Software design >  Tensorflow-FailedPreconditionError: Could not find variable dense_24/bias. This could mean that the
Tensorflow-FailedPreconditionError: Could not find variable dense_24/bias. This could mean that the

Time:12-13

I am trying to perform a learning model with openai gym, tensorflow and keras.

I build my model with this method:

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)

After, i build my agent:

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

When i call training:

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

I get this error, that is that the session is not initialized or there is no dense_24/bias variable

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

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}}]]

Who can help me solve the problem?

CodePudding user response:

I think it's a keras reference problem, you should correctly import:

import tensorflow as tf
from tensorflow import keras

and then create a model like this:

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

I have not tried with your whole code, try it and report.

  • Related