How to make Keras use Tensorflow backend in Anaconda?
Solution 1
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
Solution 2
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"
Solution 3
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.
Solution 4
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.
Solution 5
Had a similar problem, seems that if ~/.keras/keras.json
is not accessible, keras is using /tmp/.keras/keras.json
Related videos on Youtube
Tai Christian
Updated on March 10, 2020Comments
-
Tai Christian about 4 years
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!
-
Tai Christian about 7 yearsHi @nehal, thank you! The work-around method you said is working. But I am trying to find the keras_activate.sh. However there is no conda/ in ${CONDA_PREFIX}/etc/. This is my ls:
dbus-1/ fish/ fonts/ jupyter/ rc.d/
-
Nehal J Wani about 7 years@TaiChristian Search for keras_activate.sh .
find $CONDA_PREFIX | grep keras_activate.sh
-
FvD almost 7 yearsJust noting for lost souls that this applies to Windows as well, but your commands are "echo %CONDA_PREFIX%" and "set KERAS_BACKEND=tensorflow".
-
Tai Christian over 6 yearsThank you! Could be the answer for people who use Windows :3
-
BallpointBen almost 6 yearsWould
os.environ['KERAS_BACKEND'] = 'tensorflow'
(before importingkeras
) also work? -
Nehal J Wani almost 6 years@BallpointBen Probably, but you will have to do it before importing keras