maurock
maurock

Reputation: 541

Tensorflow: AttributeError: 'function' object has no attribute 'graph'

I am using Keras == 1.1.0 and tensorflow-gpu == 1.12.0.
The error is called after:

    input_layer = Input(shape=(2, ))
    layer = Dense(self._hidden[0], activation='relu')(input_layer)

and this is the Traceback

Traceback (most recent call last):
  File "D:/Documents/PycharmProjects/DDPG-master-2/main.py", line 18, in <module>
    main()
  File "D:/Documents/PycharmProjects/DDPG-master-2/main.py", line 14, in main
    agent = Agent(state_size=world.state_size, action_size=world.action_size)
  File "D:\Documents\PycharmProjects\DDPG-master-2\ddpg.py", line 50, in __init__
    batch_size=batch_size, tau=tau)
  File "D:\Documents\PycharmProjects\DDPG-master-2\networks\actor.py", line 68, in __init__
    self._generate_model()
  File "D:\Documents\PycharmProjects\DDPG-master-2\networks\actor.py", line 132, in _generate_model
    layer = Dense(self._hidden[0], activation='relu')(input_layer)
  File "D:\Anaconda3\lib\site-packages\keras\engine\topology.py", line 487, in __call__
    self.build(input_shapes[0])
  File "D:\Anaconda3\lib\site-packages\keras\layers\core.py", line 695, in build
    name='{}_W'.format(self.name))
  File "D:\Anaconda3\lib\site-packages\keras\initializations.py", line 59, in glorot_uniform
    return uniform(shape, s, name=name)
  File "D:\Anaconda3\lib\site-packages\keras\initializations.py", line 32, in uniform
    return K.random_uniform_variable(shape, -scale, scale, name=name)
  File "D:\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 282, in random_uniform_variable
    return variable(value, dtype=dtype, name=name)
  File "D:\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 152, in variable
    if tf.get_default_graph() is get_session().graph:
AttributeError: 'function' object has no attribute 'graph'

Process finished with exit code 1

I previously had tensorflow-gpu == 1.9 and uninstalled it and upgraded to 1.12, as I saw that it was a common solutions for similar problems. It did not work though.

EDIT (adding some relevant code related to the Traceback):

   agent = DDPG(state_size=world.state_size, action_size=world.action_size)

   self._actor = Actor(tensorflow_session=tensorflow_session,
                            state_size=state_size, action_size=action_size,
                            hidden_units=actor_hidden_units,
                            learning_rate=actor_learning_rate,
                            batch_size=batch_size, tau=tau)

       def _generate_model(self):
        """
        Generates the model based on the hyperparameters defined in the
        constructor.

        :return: at tuple containing references to the model, weights,
            and input later
        """
        input_layer = Input(shape=(self._state_size,))
        layer = Dense(self._hidden[0], activation='relu')(input_layer)
        layer = Dense(self._hidden[1], activation='relu')(layer)
        output_layer = Dense(self._action_size, activation='sigmoid')(layer)
        model = Model(input=input_layer, output=output_layer)
        return model, model.trainable_weights, input_layer

The code is related to three different classes.

Upvotes: 2

Views: 6039

Answers (2)

maurock
maurock

Reputation: 541

Here is how I solved the issue.

1. Uninstall keras with pip uninstall keras
2. Check that no other versions were installed (for example through conda)
3. Delete the cache in %LocalAppData%\pip\Cache
4. Re-install keras

Upvotes: 0

I experienced the same issue. Here are the things I did to resolve it:

  1. Make sure that there is no other files in my project named tensorflow.py
  2. re-install tensorflow with --no-cache-dir argument pip --no-cache-dir install tensorflow and removed pip cache files.

    For linux:
    rm -rf ~/.cache/pip/*

    For windows, delete the files in this location: %LocalAppData%\pip\Cache

I hope this helps

Upvotes: 1

Related Questions