I have install tensorflow-gpu in my Anaconda environment. They both work well.
Now I am trying to install Keras with Tensorflow backend. According to the instruction I just run:
pip install keras
But it doesn't install keras, then I tried:
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.
I also tried edit the file ~/.keras , but actually default backend was tensorflow already.
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
(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:
the environment variable KERAS_BACKEND will override what is defined in your config file
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:
export KERAS_BACKEND=tensorflow
python /path/to/python/program.py
Had the same problem after installing keras from conda-forge. keras.json
already had 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:
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:
C:\Program Files\Anaconda3\envs\tensorflow_keras\etc\conda\activate.d
and changed theano to tensorflow:
set "KERAS_BACKEND=tensorflow"
On a multi-user install on Windows 10 the Anaconda environment activation file is:
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.
有一个类似的问题,似乎如果~/.keras/keras.json
无法访问,则 keras 正在使用/tmp/.keras/keras.json
For Windows users using Anaconda. Open the Anaconda Prompt and type:
set "KERAS_BACKEND=tensorflow"
That should do the trick. If using Jupyter Notebook, you would need to restart it.
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.
https://conda.io/docs/user-guide/tasks/manage-environments.html
According to conda envs page.
1)Locate the directory for the conda environment in your Terminal window, such as
/home/jsmith/anaconda3/envs/analytics.
2)Enter that directory and create these subdirectories and files:
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:
echo 'export KERAS_BACKEND=theano' > ./etc/conda/activate.d/env_vars.sh
4)Edit ./etc/conda/deactivate.d/env_vars.sh as follows:
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.
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
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
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. Change the value to tensorflow and save the file. KERAS_BACKEND=tensorflow
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.
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.d
you'll find a .bat
file.
(replace <whatever>
in path references I make with your system specific names)
also check whether the keras.json
file has the backend set to 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. 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 .bat
file)
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.
:
if errorlevel 0 (
echo %er0%
)
inside the 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:
@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 check that the keras.json
has the backend set to tensorflow.
I hope this helps some windows users.
You cannot explicitly call 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"
.
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
Then install tensor flow as the backend engine using the following command:
pip3 install --upgrade tensorflow
Then install keras using the following command:
pip3 install keras
More information available here: http://royalcrowntutorials.blogspot.com/2018/02/installing-tensor-flow-start-terminal.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.