How to make Keras use Tensorflow backend in Anaconda?

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.


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"