keras: I get AttributeError on tf 0.10.0 running model_from_json

cnn = model_from_json(jsn)
  File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 197, in model_fr
om_json
    return layer_from_config(config, custom_objects=custom_objects)
  File "/usr/local/lib/python3.5/dist-packages/keras/utils/layer_utils.py", line 36, i
n layer_from_config
    return layer_class.from_config(config['config'])
  File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 1025, in from_co
nfig
    model.add(layer)
  File "/usr/local/lib/python3.5/dist-packages/keras/models.py", line 308, in add
    output_tensor = layer(self.outputs[0])
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 515, in __call__
    self.add_inbound_node(inbound_layers, node_indices, tensor_indices)
File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 573, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 150, in create_node
    output_tensors = to_list(outbound_layer.call(input_tensors[0], mask=input_masks[0]))
  File "/usr/local/lib/python3.5/dist-packages/keras/layers/core.py", line 91, in call
    x = K.in_train_phase(K.dropout(x, self.p, noise_shape), x)
  File "/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py", line 1243, in in_train_phase
    x = tf.python.control_flow_ops.cond(tf.cast(_LEARNING_PHASE, 'bool'),
AttributeError: module 'tensorflow.python' has no attribute 'control_flow_ops'

The issue is reproduced by doing a model_from_json on tf 0.10.0, this doesnt happen below tf 0.9.0. Thanks.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 6
  • Comments: 17 (6 by maintainers)

Most upvoted comments

Quick fix / hack workaround for v0.11

import tensorflow as tf
tf.python.control_flow_ops = tf

model = keras.models.Sequential()
# ...

I think I found the issue.

I’m running Ubuntu 14.04.5 LTS with tensorflow==0.10.0 and keras==1.1.0. I received the same error as above from keras/backend/tensorflow_backend.py

AttributeError: module 'tensorflow.python' has no attribute 'control_flow_ops'

I attempted to install TF 0.10.0 rc0 but the installation failed for unrelated reasons (though, in hindsight, it would have avoided the root problem if the installation did work).

After some digging around, I found out that tensorflow had made a change to remove undocumented symbols in their tensorflow/python/__init__.py: https://github.com/tensorflow/tensorflow/commit/5563229b5b45917cd8155bc47a8356af90fb20a2#diff-6ad579dc34adb7a581b8c0bcb1a4dd79

Probably as an accident, they made it so that control_flow_ops was no longer visible as a submodule of tensorflow.python. But it can still be imported with from tensorflow.python.ops import control_flow_ops so I added from tensorflow.python.ops import control_flow_ops as tf_control_flow_ops at the top of keras/backend/tensorflow_backend.py and replaced all references to tensorflow.python.control_flow_ops with tf_control_flow_ops and it fixed the issue.

I’ll go over the contribution guide and submit a pull request.

you can simply go for this monkey patch in your code :

import tensorflow
from tensorflow.python.ops import control_flow_ops 
tensorflow.python.control_flow_ops = control_flow_ops

I had the same problem with master version of TF today. Changed to TF 0.10.0 rc0 solved the issue for me.

TF 0.10 appears to be breaking some imports. I’ll look into it, we have to fix it while maintaining compatibility with 0.9.

if I use add(Dropout(1)) it works.

Of course it does, because 1 just disables dropout.

I was getting following error while running kears and tensor flow as backend

x = tf.python.control_flow_ops.cond(tf.cast(_LEARNING_PHASE, ‘bool’), AttributeError: ‘module’ object has no attribute ‘control_flow_ops’

but when i did following, error got vanished, now its working fine import tensorflow as tf tf.python.control_flow_ops = tf