Keras + tensorflow gives the error "no attribute 'control_flow_ops'"

22,898

Solution 1

There is an issue between Keras and TF, Probably tf.python.control_flow_ops does not exist or not visible anymore. using below import statements you can resolve this issue

import tensorflow as tf
tf.python.control_flow_ops = tf

For Details check: https://github.com/fchollet/keras/issues/3857

Solution 2

If not Using TensorFlow 1.0.0; use tf.python_io in later versions

import tensorflow as tf 

tf.python_io.control_flow_ops = tf

Solution 3

I got this problem when it turns out keras was using the Theano backend. To fix it do one of these:

  • In ~/.keras/keras.json set "backend": "tensorflow".
  • Set the environment variable KERAS_BACKEND to tensorflow.

See Keras backend documentation for more information.

Share:
22,898
graffe
Author by

graffe

Updated on August 09, 2020

Comments

  • graffe
    graffe almost 4 years

    I am trying to run keras for the first time. I installed the modules with:

    pip install keras --user
    pip install tensorflow --user
    

    and then tried to run https://github.com/fchollet/keras/blob/master/examples/mnist_cnn.py.

    However it gives me:

    AttributeError: 'module' object has no attribute 'control_flow_ops'
    

    These are the versions I am using.

    print tensorflow.__version__
    0.11.0rc0
    print keras.__version__
    1.1.0
    

    What can I do to get keras to run with tensorflow?

  • Naomi Fridman
    Naomi Fridman about 6 years
    I have the same problem. when tried your solution, I got: AttributeError: module 'tensorflow' has no attribute 'python'
  • Deepak Sharma
    Deepak Sharma about 6 years
    tf has changed a lot in last 1 or 2 year. I don't think this solution is relevant anymore.
  • Arthur Tacca
    Arthur Tacca over 5 years
    @Ilan What do you mean? If you get this error when you just want to use Theano, that probably means that you don't have it installed. That was certainly true for me when I got this error: I didn't have Theano installed because I hadn't planned to use it! If you want to use Theano sometimes and TensorFlow other times, use the KERAS_BACKEND environment variable I said in my answer; note that environment variables can be different in different processes. If you want to use both in one program, you might be out of luck. In any case, you probably need to post a new question.