Python 如何让 Keras 在 Anaconda 中使用 Tensorflow 后端?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43327464/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:53:04  来源:igfitidea点击:

How to make Keras use Tensorflow backend in Anaconda?

pythontensorflowanacondabackendkeras

提问by Tai Christian

I have install tensorflow-gpu in my Anaconda environment. They both work well.

我已经在我的 Anaconda 环境中安装了 tensorflow-gpu。他们都工作得很好。

Now I am trying to install Keras with Tensorflow backend. According to the instructionI just run:

现在我正在尝试使用 Tensorflow 后端安装 Keras。根据我刚刚运行的指令

pip install keras

But it doesn't install keras, then I tried:

但它没有安装keras,然后我尝试了:

conda install -c conda-forge keras=2.0.2

Then I am now able import keras in python. But the problem is, it always use the Theano backend. I am trying to change this, but not knowing how to do it.

然后我现在可以在 python 中导入 keras。但问题是,它总是使用 Theano 后端。我试图改变这一点,但不知道如何去做。

I also tried edit the file ~/.keras, but actually default backend was tensorflow already.

我也尝试编辑文件~/.keras,但实际上默认后端已经是 tensorflow。

Please help.. Thank you so much!

请帮助..非常感谢!

回答by Nehal J Wani

This happens because the keras conda-forge package puts a file in ${CONDA_PREFIX}/etc/conda/activate.d/keras_activate.sh, which sets the environment variable KERAS_BACKEND

发生这种情况是因为 keras conda-forge 包在 中放置了一个文件${CONDA_PREFIX}/etc/conda/activate.d/keras_activate.sh,该文件设置了环境变量KERAS_BACKEND

(root) [root@starlabs ~]# cat $CONDA_PREFIX/etc/conda/activate.d/keras_activate.sh
#!/bin/bash
if [ "$(uname)" == "Darwin" ]
then
    # for Mac OSX
    export KERAS_BACKEND=tensorflow
elif [ "$(uname)" == "Linux" ]
then
    # for Linux
    export KERAS_BACKEND=theano
fi

As you can see from the file, in Linux, it sets the value to 'theano' and according to the official docs:

正如您从文件中看到的,在 Linux 中,它将值设置为 'theano' 并根据官方文档:

the environment variable KERAS_BACKEND will override what is defined in your config file

环境变量 KERAS_BACKEND 将覆盖配置文件中定义的内容

To work around this, you can either edit this file and change 'theano' to 'tensorflow' (which would probably get overwritten on reinstall or on changing environments) or, do the following:

要解决此问题,您可以编辑此文件并将“theano”更改为“tensorflow”(这可能会在重新安装或更改环境时被覆盖),或者执行以下操作:

export KERAS_BACKEND=tensorflow
python /path/to/python/program.py

回答by fairnc

Had the same problem after installing keras from conda-forge. keras.jsonalready had tensorflow:

从 conda-forge 安装 keras 后遇到了同样的问题。keras.json已经有张量流:

{
    "floatx": "float32",
    "epsilon": 1e-07,
    "backend": "tensorflow",
    "image_data_format": "channels_last"
}

but activate tensorflow_keras(where "tensorflow_keras" is the environment name), changes the backend to theano:

但是activate tensorflow_keras(其中“ tensorflow_keras”是环境名称),将后端更改为 theano:

C:\Users\User1>activate tensorflow_keras

(tensorflow_keras) C:\Program Files\Anaconda3\envs\tensorflow_keras\etc\conda\ac
tivate.d>set "KERAS_BACKEND=theano"

Following @FvD above, I edited this file:

按照上面的@FvD,我编辑了这个文件:

C:\Program Files\Anaconda3\envs\tensorflow_keras\etc\conda\activate.d

and changed theano to tensorflow:

并将 theano 更改为 tensorflow:

set "KERAS_BACKEND=tensorflow"

回答by 4Oh4

On a multi-user install on Windows 10 the Anaconda environment activation file is:

在 Windows 10 上的多用户安装中,Anaconda 环境激活文件是:

C:\Users\<user name>\AppData\Local\Continuum\Anaconda3\envs\<environment name>\etc\conda\activate.d\keras_activate.bat

Just change <user name>and <environment name>to match.

只是改变<user name><environment name>匹配。

回答by Rami Alloush

For Windows users using Anaconda. Open the Anaconda Prompt and type:

对于使用 Anaconda 的 Windows 用户。打开 Anaconda Prompt 并输入:

set "KERAS_BACKEND=tensorflow"

That should do the trick. If using Jupyter Notebook, you would need to restart it.

这应该够了吧。如果使用 Jupyter Notebook,则需要重新启动它。

回答by user3236274

Had a similar problem, seems that if ~/.keras/keras.jsonis not accessible, keras is using /tmp/.keras/keras.json

有一个类似的问题,似乎如果~/.keras/keras.json无法访问,则keras正在使用/tmp/.keras/keras.json

回答by Vinay Vamshi Reddy Ramasahayam

Though this seems a bit of work, if you use conda envs as much as I do , where I have environments for Tensorflow and Theano separately. It will reduce a lot of repeated setting and unsetting of environment variables each time it is activated.

虽然这看起来有点工作,但如果你像我一样使用 conda envs,我有分别用于 Tensorflow 和 Theano 的环境。每次激活都会减少很多环境变量的重复设置和取消设置。

https://conda.io/docs/user-guide/tasks/manage-environments.html

https://conda.io/docs/user-guide/tasks/manage-environments.html

According to conda envs page.

根据 conda envs 页面。

1)Locate the directory for the conda environment in your Terminal window, such as

1)在你的终端窗口中找到conda环境的目录,比如

/home/jsmith/anaconda3/envs/analytics.

2)Enter that directory and create these subdirectories and files:

2)进入该目录并创建这些子目录和文件:

cd /home/jsmith/anaconda3/envs/analytics
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh

3)Edit ./etc/conda/activate.d/env_vars.sh as follows:

3) 编辑 ./etc/conda/activate.d/env_vars.sh 如下:

echo 'export KERAS_BACKEND=theano' > ./etc/conda/activate.d/env_vars.sh

4)Edit ./etc/conda/deactivate.d/env_vars.sh as follows:

4) 编辑 ./etc/conda/deactivate.d/env_vars.sh 如下:

echo 'unset KERAS_BACKEND' > ./etc/conda/deactivate.d/env_vars.sh

回答by Vaibhav K

In Mac system, go to terminal and navigate to user profile and check whether .kerasfolder exists or not.

在 Mac 系统中,转到终端并导航到用户配置文件并检查.keras文件夹是否存在。

ls -a
cd .keras
vim keras.json # edit the keras.json file in editor and change the backend to tensorflow

It should look like this.

它应该是这样的。

"floatx": "float32",
"epsilon": 1e-07,
"backend": "tensorflow",
"image_data_format": "channels_last" 

Close the Editor by pressing escapebutton then follwed by :wqIt should work

按关闭编辑器逃生按钮,然后通过follwed WQ:它应该工作

Now if you are using any conda environment then follow below mentioned steps 1. first locate the environment variable by navigating to the anaconda env folder

现在,如果您正在使用任何 conda 环境,请按照下面提到的步骤 1. 首先通过导航到 anaconda env 文件夹来定位环境变量

Anaconda3/envs/"name_of_your_conda_environment"/etc/conda/activate.d

Anaconda3/envs/"name_of_your_conda_environment"/etc/conda/activate.d

It contains the file keras_activate.bat which has theano asbackend by default. Change the value to tensorflow and save the file. KERAS_BACKEND=tensorflow

它包含文件 keras_activate.bat,默认情况下该文件将theano 作为后端。将值更改为 tensorflow 并保存文件。 KERAS_BACKEND=张量流

deactivate the environment and activate it again i.e

停用环境并再次激活它,即

source deactivate name_of_your_conda_environment
source activate name_of_your_conda_environment

Hope this will solve all keras backend tensorflow issues.

希望这将解决所有 keras 后端张量流问题。

回答by Tintenfisch Fee

For Windows users, in my case Windows 10 it seems that how Anaconda creates the .bat files is a bit strange, or at least how they are being executed. They show every command written, in them in the cmd.
At least in my case that is.
So for me it always said: "KERAS_BACKEND=theano", but that was not executed.

Check this:

In this path: <your_conda_install_location>\envs\<your_environment_name>\etc\conda\activate.dyou'll find a .batfile.
(replace <whatever>in path references I make with your system specific names)

also checkwhether the keras.jsonfile has the backend set to tensorflow.

The content in mine was:

对于 Windows 用户,在我的 Windows 10 中,Anaconda 创建 .bat 文件的方式似乎有点奇怪,或者至少它们是如何执行的。它们显示了在 cmd 中写入的每个命令。
至少在我的情况下是这样。
所以对我来说,它总是说: "KERAS_BACKEND=theano",但这并没有被执行。

检查这个:

在这个路径中:<your_conda_install_location>\envs\<your_environment_name>\etc\conda\activate.d你会找到一个.bat文件。
(替代<whatever>路径引用我就与你的系统的具体名称)

也检查了是否keras.json文件具有后端设置为tensorflow。

我的内容是:

:: Figure out the default Keras backend by reading the config file.
python %CONDA_PREFIX%\etc\keras\load_config.py > temp.txt
set /p KERAS_BACKEND=<temp.txt
del temp.txt

:: Try to use the default Keras backend.
:: Fallback to Theano if it fails (Theano always works).
python -c "import keras" 1> nul 2>&1
if errorlevel 1 (
    ver > nul
    set "KERAS_BACKEND=theano"
    python -c "import keras" 1> nul 2>&1
)

I simply added @echo offso it doesn't show all the code and added some console output to tell me what was actually executed. That simultaneously works as kind of a debugger or proof that it actually did not execute set "KERAS_BACKEND=theano"
(scroll down to see the full edited .batfile)

on top I added:

我只是添加了@echo off所以它不显示所有代码并添加了一些控制台输出来告诉我实际执行了什么。这同时作为一种调试器或证明它实际上没有执行set "KERAS_BACKEND=theano"
(向下滚动以查看完整的编辑.bat文件)

在我添加的顶部:

@echo off
:: message variables
set er0=used default Keras backend tensorflow
set er1=used fallback Keras backend theano

for the messages:
at the end of :: Figure out the default Keras backend by reading the config file.:

对于消息:
在结束时:: Figure out the default Keras backend by reading the config file.

if errorlevel 0 (
    echo %er0%
    )

inside the if from :: Fallback to Theano if it fails (Theano always works):

在 if from 中:: Fallback to Theano if it fails (Theano always works)

echo %er1%


And now the complete codeof the .batfor a better overview:


而现在的完整代码.bat一个更好的概述:

@echo off
:: message variables
set er0=used default Keras backend tensorflow
set er1=used fallback Keras backend theano

:: Figure out the default Keras backend by reading the config file.
python %CONDA_PREFIX%\etc\keras\load_config.py > temp.txt
set /p KERAS_BACKEND=<temp.txt
del temp.txt
if errorlevel 0 (
    echo %er0%
    )

:: Try to use the default Keras backend.
:: Fallback to Theano if it fails (Theano always works).
python -c "import keras" 1> nul 2>&1
if errorlevel 1 (
    ver > nul
    set "KERAS_BACKEND=theano"
    echo %er1%
    python -c "import keras" 1> nul 2>&1
)

So now there are only messages on the cmd which state if the default backend was used or if in case of an error it used the theano backend.
Also make sure to checkthat the keras.jsonhas the backend set to tensorflow.

I hope this helps some windows users.

因此,现在 cmd 上只有消息,说明是否使用了默认后端,或者在出现错误时是否使用了 theano 后端。
请务必检查keras.json具有后端设置为tensorflow。

我希望这可以帮助一些 Windows 用户。

回答by Saroj Bhattarai

You cannot explicitly call import keras. Use:

您不能显式调用 import keras. 用:

import tensorflow as tf

import tensorflow as tf

Now you can use:

现在您可以使用:

tf.keras

tf.keras

回答by Li.seascape

There is a file keras_activate.shin the path "/anaconda2/envs/py2/etc/conda/activate.d".

keras_activate.sh路径中有一个文件"/anaconda2/envs/py2/etc/conda/activate.d"

Then editing it, delete the content :

然后编辑它,删除内容:

"if [ "$(uname)" == "Darwin" ]

then

然后

    # for Mac OSX
    export KERAS_BACKEND=tensorflow
elif [ "$(uname)" == "Linux" ]

then

然后

    # for Linux
    export KERAS_BACKEND=theano
fi". 

after that, add the line :

之后,添加行:

set "KERAS_BACKEND=tensorflow"