简体   繁体   English

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

[英]How to make Keras use Tensorflow backend in Anaconda?

I have install tensorflow-gpu in my Anaconda environment.我已经在我的 Anaconda 环境中安装了 tensorflow-gpu。 They both work well.他们都工作得很好。

Now I am trying to install Keras with Tensorflow backend.现在我正在尝试使用 Tensorflow 后端安装 Keras。 According to the instruction I just run:根据我刚刚运行的指令

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.然后我现在可以在 python 中导入 keras。 But the problem is, it always use the Theano backend.但问题是,它总是使用 Theano 后端。 I am trying to change this, but not knowing how to do it.我试图改变这一点,但不知道如何去做。

I also tried edit the file ~/.keras , but actually default backend was tensorflow already.我也尝试编辑文件~/.keras ,但实际上默认后端已经是 tensorflow。

Please help.. Thank you so much!请帮助..非常感谢!

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- ${CONDA_PREFIX}/etc/conda/activate.d/keras_activate.sh包在${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

Had the same problem after installing keras from conda-forge.从 conda-forge 安装 keras 后遇到了同样的问题。 keras.json already had tensorflow: keras.json已经有了 tensorflow:

{
    "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"

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>即可匹配。

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

For Windows users using Anaconda.对于使用 Anaconda 的 Windows 用户。 Open the Anaconda Prompt and type:打开 Anaconda Prompt 并输入:

set "KERAS_BACKEND=tensorflow"

That should do the trick.这应该够了吧。 If using Jupyter Notebook, you would need to restart it.如果使用 Jupyter Notebook,则需要重新启动它。

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.虽然这看起来有点工作,但如果你像我一样使用 conda envs,我有分别用于 Tensorflow 和 Theano 的环境。 It will reduce a lot of repeated setting and unsetting of environment variables each time it is activated.每次激活都会减少很多环境变量的重复设置和取消设置。

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

In Mac system, go to terminal and navigate to user profile and check whether .keras folder 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 escape button then follwed by :wq It should work转义按钮关闭编辑器,然后按: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 as backend by default.它包含文件 keras_activate.bat,默认情况下该文件将theano 作为后端。 Change the value to tensorflow and save the file.将值更改为 tensorflow 并保存文件。 KERAS_BACKEND=tensorflow KERAS_BACKEND=张量流

deactivate the environment and activate it again ie停用环境并再次激活它,即

source deactivate name_of_your_conda_environment
source activate name_of_your_conda_environment

Hope this will solve all keras backend tensorflow issues.希望这将解决所有 keras 后端张量流问题。

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.对于 Windows 用户,在我的 Windows 10 中,Anaconda 创建 .bat 文件的方式似乎有点奇怪,或者至少它们是如何执行的。 They show every command written, in them in the cmd.它们显示了在 cmd 中写入的每个命令。
At least in my case that is.至少在我的情况下是这样。
So for me it always said: "KERAS_BACKEND=theano" , but that was not executed.所以对我来说,它总是说: "KERAS_BACKEND=theano" ,但这并没有被执行。

Check this:检查这个:

In this path: <your_conda_install_location>\\envs\\<your_environment_name>\\etc\\conda\\activate.d you'll find a .bat file.在此路径中: <your_conda_install_location>\\envs\\<your_environment_name>\\etc\\conda\\activate.d您将找到一个.bat文件。
(replace <whatever> in path references I make with your system specific names) (用你的系统特定名称替换路径引用中的<whatever>

also check whether the keras.json file has the backend set to tensorflow.还要检查keras.json文件是否将后端设置为 tensorflow。

The content in mine was:我的内容是:

:: 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 off so it doesn't show all the code and added some console output to tell me what was actually executed.我只是添加了@echo off所以它没有显示所有代码并添加了一些控制台输出来告诉我实际执行了什么。 That simultaneously works as kind of a debugger or proof that it actually did not execute set "KERAS_BACKEND=theano"它同时作为一种调试器或证明它实际上没有执行set "KERAS_BACKEND=theano"
(scroll down to see the full edited .bat file) (向下滚动以查看完整编辑的.bat文件)

on top I added:最重要的是我补充说:

@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 code of the .bat for 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.因此,现在 cmd 上只有消息,说明是否使用了默认后端,或者在出现错误时是否使用了 theano 后端。
Also make sure to check that the keras.json has the backend set to tensorflow.还要确保检查keras.json是否将后端设置为 tensorflow。

I hope this helps some windows users.我希望这可以帮助一些 Windows 用户。

You cannot explicitly call import keras .您不能显式调用import keras Use:用:

import tensorflow as tf

Now you can use:现在您可以使用:

tf.keras

There is a file keras_activate.sh in the path "/anaconda2/envs/py2/etc/conda/activate.d" .路径"/anaconda2/envs/py2/etc/conda/activate.d"有一个文件keras_activate.sh

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"

First of all you have to install one of the following python versions首先,您必须安装以下python版本之一

Then install tensor flow as the backend engine using the following command:然后使用以下命令安装张量流作为后端引擎:

pip3 install --upgrade tensorflow

Then install keras using the following command:然后使用以下命令安装 keras:

pip3 install keras

More information available here: http://royalcrowntutorials.blogspot.com/2018/02/installing-tensor-flow-start-terminal.html此处提供更多信息: http : //royalcrowntutorials.blogspot.com/2018/02/installing-tensor-flow-start-terminal.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM