Python 如何更改 Keras 后端(json 文件在哪里)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40310035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change Keras backend (where's the json file)?
提问by George Liu
I have installed Keras, and wanted to switch the backend to Theano. I checked out this post, but still have no idea where to put the created json file. Also, below is the error I got when running import keras
in Python Shell:
我已经安装了 Keras,并想将后端切换到 Theano。我查看了这篇文章,但仍然不知道将创建的 json 文件放在哪里。此外,以下是我import keras
在 Python Shell 中运行时遇到的错误:
Using TensorFlow backend.
Traceback (most recent call last): File "", line 1, in import keras File "C:\Python27\lib\site-packages\keras__init__.py", line 2, in from . import backend File "C:\Python27\lib\site-packages\keras\backend__init__.py", line 64, in from .tensorflow_backend import * File "C:\Python27\lib\site-packages\keras\backend\tensorflow_backend.py", line 1, in import tensorflow as tf ImportError: No module named tensorflow
使用 TensorFlow 后端。
回溯(最近一次调用):文件“”,第 1 行,导入 keras 文件“C:\Python27\lib\site-packages\keras__init__.py”,第 2 行,来自 . import backend File "C:\Python27\lib\site-packages\keras\backend__init__.py", line 64, in from .tensorflow_backend import * File "C:\Python27\lib\site-packages\keras\backend\tensorflow_backend.py" py", line 1, in import tensorflow as tf ImportError: No module named tensorflow
When running python -c "import keras; print(keras.__version__)"
from Windows command line, I got:
python -c "import keras; print(keras.__version__)"
从 Windows 命令行运行时,我得到:
Using TensorFlow backend. Traceback (most recent call last): File "", line 1, in File "C:\Python27\lib\site-packages\keras__init__.py", line 2, in from . import backend File "C:\Python27\lib\site-packages\keras\backend__init__.py", line 64, in from .tensorflow_backend import * File "C:\Python27\lib\site-packages\keras\backend\tensorflow_backend.py", line 1, in import tensorflow as tf ImportError: No module named tensorflow
使用 TensorFlow 后端。回溯(最近一次调用):文件“”,第 1 行,在文件“C:\Python27\lib\site-packages\keras__init__.py”中,第 2 行,来自 . import backend File "C:\Python27\lib\site-packages\keras\backend__init__.py", line 64, in from .tensorflow_backend import * File "C:\Python27\lib\site-packages\keras\backend\tensorflow_backend.py" py", line 1, in import tensorflow as tf ImportError: No module named tensorflow
Can someone please help? Thanks!
有人可以帮忙吗?谢谢!
回答by sascha
After looking at keras sources (this place):
查看 keras 来源(这个地方)后:
Start up your python-binary and do the following
启动你的 python-binary 并执行以下操作
import os
print(os.path.expanduser('~'))
# >>> C:\Users\Sascha' # will look different for different OS
- This should be the base-directory
- Keras will build an folder
.keras
there wherekeras.json
resides (if it was already created). If it's not there, create it there - Example:
C:\\Users\\Sascha\\.keras\\keras.json'
- 这应该是基本目录
- Keras 将
.keras
在那里建立一个文件夹keras.json
(如果它已经创建)。如果它不存在,在那里创建它 - 例子:
C:\\Users\\Sascha\\.keras\\keras.json'
回答by rojan sudev
In case of Ubuntu,the following worked for me:
在 Ubuntu 的情况下,以下对我有用:
The '.keras' folder is present in your home directory,but is hidden.So,you need to unhide the hidden files in your home directory. You can see the hidden files in Ubuntu by
“.keras”文件夹存在于您的主目录中,但处于隐藏状态。因此,您需要取消隐藏主目录中的隐藏文件。您可以通过以下方式查看 Ubuntu 中的隐藏文件
- View-> show hidden files or
- pressing ctrl+H.
- 查看-> 显示隐藏文件或
- 按 ctrl+H。
You can now see the '.keras' folder in your home directory.Inside that folder,you will see the 'keras.json' file which you can modify to switch the keras backend to theanoaccording to the official documentation https://keras.io/backend/
您现在可以在您的主目录中看到“.keras”文件夹。在该文件夹内,您将看到“keras.json”文件,您可以根据官方文档https://keras修改该文件以将 keras 后端切换到theano .io/后端/
回答by Abhijeet
"Can't find your keras.json file? : Windows
On most systems the keras.json file (and associated sub-directories) will not be created until you open up a Python shell and directly import the keras package itself.
"找不到您的 keras.json 文件?:Windows
在大多数系统上,在您打开 Python shell 并直接导入 keras 包本身之前,不会创建 keras.json 文件(和相关的子目录)。
If you find that the ~/.keras/keras.json file does not exist on your system, simply open up a shell, (optionally) access your Python virtual environment (if you are using virtual environments), and then import Keras:
如果您发现系统上不存在 ~/.keras/keras.json 文件,只需打开一个 shell,(可选)访问您的 Python 虚拟环境(如果您使用的是虚拟环境),然后导入 Keras:
$ workon keras_tf
$ python
>>> import keras
>>> quit()
"
”
Referenced from : keras-with-tensorflow/theano-backend
回答by Roelant
For those with a python shell open:
对于那些打开了 python shell 的人:
import os
with open(os.path.expanduser('~')+'\.keras\keras.json','w') as f:
new_settings = """{\r\n
"epsilon": 1e-07,\r\n
"image_data_format": "channels_last",\n
"backend": "theano",\r\n
"floatx": "float32"\r\n
}"""
f.write(new_settings)
import keras
回答by Shashank Singla
In case you want to change the config, the json is available here: ~/.keras/keras.json
如果您想更改配置,可以在此处获取 json: ~/.keras/keras.json
To do this dynamically in python 2.7 you can run:
要在 python 2.7 中动态执行此操作,您可以运行:
from keras import backend as K
import os
def set_keras_backend(backend):
if K.backend() != backend:
os.environ['KERAS_BACKEND'] = backend
reload(K)
assert K.backend() == backend
set_keras_backend("theano")
回答by Harshit Mehta
You can directly use,
可以直接使用,
import os
os.environ['KERAS_BACKEND']='theano'
or
或者
os.environ['KERAS_BACKEND']='tensorflow'
回答by saurabh
Simplest Solution:
最简单的解决方案:
Google's TensorFlow is default backend for keras but for example if u want to change it with theano then First check if the alternate backend you wish to work with is installed successfully by importing it in python shell:
Google 的 TensorFlow 是 keras 的默认后端,但例如,如果您想使用 theano 更改它,那么首先检查您希望使用的备用后端是否已成功安装,方法是将其导入 python shell:
import theano as th
导入 theano 作为 th
if that works fine
如果这工作正常
Step 2: if you have installed keras, theano in virtualenv then go to virtualenv directory
第二步:如果你已经安装了 keras、theano 在 virtualenv 中然后进入 virtualenv 目录
e.g virtualenv/virtual/lib/python2.7/site-packages/keras/backend/
例如 virtualenv/virtual/lib/python2.7/site-packages/keras/backend/
open init.py change line 27
打开init.py 更改第 27 行
Default backend: TensorFlow.
_BACKEND = 'theano'
and thats it
就是这样
open python shell and import keras
打开python shell并导入keras
import keras as kd
导入 keras 作为 kd
回答by Beta
Just to add this informative post. I'm using anaconda for my task. And imported keras through my anaconda python. So keras got installed in
只是为了添加这个信息丰富的帖子。我正在使用 anaconda 来完成我的任务。并通过我的 anaconda python 导入了 keras。所以 keras 安装了
C:\Users\username\AppData\Local\Continuum\Anaconda3\Lib\site-packages
There are 2 folders in site-packages: keras
& Keras-1.0.8.dist-info
.
站点包中有 2 个文件夹:keras
& Keras-1.0.8.dist-info
。
In Keras-1.0.8.dist-info
, there's a file called metadata.json
. This by default has "Theano" as backend. So, if you change that to tensorflow, you will get tensoflow backend.
在 中Keras-1.0.8.dist-info
,有一个名为metadata.json
. 默认情况下,它以“Theano”作为后端。因此,如果您将其更改为 tensorflow,您将获得 tensoflow 后端。
Hope it will help someone who has might type of issues.
希望它能帮助有可能类型问题的人。
回答by blackHoleDetector
For Linux systems, the hidden .keras directory will be created in the user's home directory. To observe whether or not it has been created, run the following command from your home directory (the -a allows you to see hidden files and directories).
对于 Linux 系统,将在用户的主目录中创建隐藏的 .keras 目录。要观察它是否已创建,请从您的主目录运行以下命令(-a 允许您查看隐藏的文件和目录)。
ls –a
If the directory is there, then cd into it and modify the keras.json file. If it's not there, then create the directory with
如果目录在那里,那么 cd 进入它并修改 keras.json 文件。如果它不存在,则使用以下命令创建目录
mkdir .keras
Then create the file with
然后创建文件
touch keras.json
Then edit the file to make the config changes you referenced to change the backend engine to Theano.
然后编辑该文件以进行您引用的配置更改,以将后端引擎更改为 Theano。
This process is covered fully in this video.
本视频全面介绍了此过程。
回答by Jose Kj
In ubuntu you can use this command to open keras.json file in vi editor and editing and saveing
在 ubuntu 中可以使用这个命令在 vi 编辑器中打开 keras.json 文件并进行编辑和保存
sudo vi $HOME/.keras/keras.json
or use the following for opening in gedit
或使用以下在 gedit 中打开
sudo gedit $HOME/.keras/keras.json