Keras导入错误Tensorflow后端

时间:2016-11-10 23:37:59

标签: tensorflow keras

    ---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/Users/CJL/anaconda3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in <module>()
      4 try:
----> 5     from tensorflow.python.ops import ctc_ops as ctc
      6 except ImportError:

ImportError: cannot import name 'ctc_ops'

During handling of the above exception, another exception occurred:

ImportError                               Traceback (most recent call last)
<ipython-input-10-c74e2bd4ca71> in <module>()
----> 1 import keras

/Users/CJL/anaconda3/lib/python3.5/site-packages/keras/__init__.py in <module>()
      1 from __future__ import absolute_import
----> 2 from . import backend
      3 from . import datasets
      4 from . import engine
      5 from . import layers

/Users/CJL/anaconda3/lib/python3.5/site-packages/keras/backend/__init__.py in <module>()
     67 elif _BACKEND == 'tensorflow':
     68     sys.stderr.write('Using TensorFlow backend.\n')
---> 69     from .tensorflow_backend import *
     70 else:
     71     raise Exception('Unknown backend: ' + str(_BACKEND))

/Users/CJL/anaconda3/lib/python3.5/site-packages/keras/backend/tensorflow_backend.py in <module>()
      5     from tensorflow.python.ops import ctc_ops as ctc
      6 except ImportError:
----> 7     import tensorflow.contrib.ctc as ctc
      8 
      9 import numpy as np

ImportError: No module named 'tensorflow.contrib.ctc'

根据这篇文章enter link description here,似乎是Keras而不是Tensorflow的问题 我想知道这是否已经修复了吗?

2 个答案:

答案 0 :(得分:5)

这是Keras和tensorflow之间的问题。您应该将此fix更改为使用类似

的内容
if hasattr(tf.contrib, 'ctc'):
  ctc = tf.contrib.ctc  # old version
else:
  ctc = tf.nn  # New official version.

访问ctc操作的当前“官方”方式是tf.nn,例如tf.nn.ctc_loss

答案 1 :(得分:-3)

如果keras出现导入问题,并且您已经安装了“没有名为keras的模块”。

  1. 只需通过以下方式升级您的点数即可:
python -m pip install –upgrade pip
  1. 通过以下方式重新安装升级的keras和tensorflow:
pip install keras
pip install tensorflow

希望它将解决问题。如果没有,请尝试升级您的conda(Anaconda),然后再次执行上述步骤1至3。