keras的backend 设置 tensorflow,theano

时间:2022-01-02 07:16:49
win7 系统环境安装步骤:

1.首先是安装Python,建议安装anaconda
2.安装完anaconda后打开anaconda promp命令行promp,输入conda list.
可以看到已经安装的库以及版本等信息,注意此时没有keras.
3.通过 conda install keras 或  pip install keras 直接安装。(会默认的给你安装keras最新版本和所需要的theano)
4.安装完成之后,就可以打开notebook,输入import keras 检查是否成功。
5.因为windows版本的tensorflow刚刚才推出,所以目前支持性不太好。
但是keras的backend 同时支持tensorflow和theano.
并且默认是tensorflow,因此在win本上需要更改backend为theano才能运行。
这是官网的配置文档:http://keras-cn.readthedocs.io/en/latest/backend/点击打开链接

如果已经运行过一次Keras,你将在下面的目录下找到Keras的配置文件:~/.keras/keras.json
如果该目录下没有该文件,你可以手动创建一个
将文件的默认配置如下:
C:\Users\Administrator>python
Python 2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import keras
Using TensorFlow backend.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Anaconda2\lib\site-packages\keras\__init__.py", line 2, in <module>
from . import backend
File "C:\Anaconda2\lib\site-packages\keras\backend\__init__.py", line 68, in <module>
from .tensorflow_backend import *
File "C:\Anaconda2\lib\site-packages\keras\backend\tensorflow_backend.py", line 1, in <module>
import tensorflow as tf
ImportError: No module named tensorflow
>>> import keras
Using Theano backend.
WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.
方法一:将C:\Anaconda2\Lib\site-packages\keras\backend\__init__.py的line 27修改
# Default backend: TensorFlow.
#_BACKEND = 'tensorflow'
_BACKEND = 'theano'
然后,python-> import keras
方法二: 出现 tensorflow提示错误的话,需要修改下面的位置的内容
C:\Users\Administrator\.keras\keras.json
{
"image_dim_ordering":"tf",
"epsilon":1e-07,
"floatx":"float32",
"backend":"tensorflow"
}

{
"image_dim_ordering": "tf",
"epsilon": 1e-07,
"floatx": "float32",
"backend": "theano"
}