Skip to main content
 首页 » 编程设计

python之新虚拟环境中的keras导入断言错误

2025年01月19日14qq号

在 Win10 机器上的虚拟环境中安装最新的 Keras 和 TF 后,我一直遇到断言错误

AssertionError                            Traceback (most recent call last) 
<ipython-input-6-88d96843a926> in <module>() 
----> 1 import keras 
 
~\AppData\Local\Continuum\Anaconda3\envs\aind-vui\lib\site-packages\keras\__init__.py in <module>() 
      1 from __future__ import absolute_import 
      2  
----> 3 from . import activations 
      4 from . import applications 
      5 from . import backend 
 
~\AppData\Local\Continuum\Anaconda3\envs\aind-vui\lib\site-packages\keras\activations.py in <module>() 
      2 import six 
      3 import warnings 
----> 4 from . import backend as K 
      5 from .utils.generic_utils import deserialize_keras_object 
      6 from .engine import Layer 
 
~\AppData\Local\Continuum\Anaconda3\envs\aind-vui\lib\site-packages\keras\backend\__init__.py in <module>() 
     69 if 'KERAS_BACKEND' in os.environ: 
     70     _backend = os.environ['KERAS_BACKEND'] 
---> 71     assert _backend in {'theano', 'tensorflow', 'cntk'} 
     72     _BACKEND = _backend 
     73  
 
AssertionError: 

tensorflow 本身可以正常导入。我也让 keras 在我的主要工作环境中工作得很好,并且 keras.json 正确指向 tensorflow。

pip list 显示环境中安装了Keras。

python -c "from keras import backend" 
Using TensorFlow backend. 

显示正确的消息。

沮丧地

$ python 
>>> import keras 
>>> quit() 

有效,但在 jupyternotebook 中做同样的事情却无效

请您参考如下方法:

做

$ export KERAS_BACKEND=tensorflow 

然后重新运行你的程序。 显然你定义了它,但选择了这 3 个值之外的值。