I tried to switch Backend with Keras (from TensorFlow to Theano) but did not manage. I followed the temps described here but it doesn't work. I created a keras.json in the keras' directory (as it did not exist) but it doesn't change anything when I import it from Python.
10 Answers
Create a .keras
(note the .
in front) folder in you home directory and put the keras.json
file there.
For example, /home/DaniPaniz/.keras/keras.json
(or ~/.keras/keras.json
in short) if you are on a UNIX like system (MacOS X, Linux, *BSD). On Windows you want to create the folder %USERPROFILE%/.keras
and put the JSON file there.
Alternatively, you can also set the environment variable KERAS_BACKEND
:
KERAS_BACKEND=theano python mymodel.py

- 55,207
- 13
- 135
- 135
-
24You can also put `import os; os.environ['KERAS_BACKEND'] = 'theano'` before importing `keras` – BallpointBen Aug 27 '18 at 21:45
In case you want to change the config permanently, the json is available here: ~/.keras/keras.json
and you can change the backend.
To do this dynamically in python 2.7 you can run:
from keras import backend as K
import os
def set_keras_backend(backend):
if K.backend() != backend:
os.environ['KERAS_BACKEND'] = backend
reload(K)
assert K.backend() == backend
set_keras_backend("theano")

- 12,340
- 5
- 53
- 75

- 1,797
- 17
- 13
-
2
-
2If you're using python 3, reload is not built-in. To use reload in python 3: https://stackoverflow.com/a/961219 – Wmol Jul 10 '17 at 17:41
-
3
-
1Still (using the `reload` function of the `importlib` library) the backend is not being dynamically changed for me. Any ideas on how to overcome this issue is really appreciated. @Wmol @Demitri – talha06 Jul 04 '20 at 23:18
I had an issue where I could not from keras import backend
at all until I set the backend to theano. The provided answers should work if you can import backend, but if not, just use:
import os
os.environ['KERAS_BACKEND'] = 'theano'
import keras as ks
# Using Theano backend.

- 12,340
- 5
- 53
- 75
In windows, you need to find .keras folder in your C drive. Most probably, it will be somewhere in C:/users/username/. There you will find .keras folder, it contains a json file, keras.json, open it. You will see:
{
“backend”: “tensorflow”,
“floatx”: “float32”,
“epsilon”: 1e-07
}
more or less. replace 'tensorflow' with 'theano'. and save the file.

- 322
- 7
- 15
If you're using windows you can run from command line:
set "KERAS_BACKEND=theano"

- 3,461
- 5
- 41
- 61
from keras import backend as K
from os import environ
# user defined function to change keras backend
def set_keras_backend(backend):
if K.backend() != backend:
environ['KERAS_BACKEND'] = backend
reload(K)
assert K.backend() == backend
# call the function with "theano"
set_keras_backend("theano")

- 12,340
- 5
- 53
- 75

- 2,314
- 21
- 29
-
-
@StephenRauch When you import keras, it picks up "tensorflow" as a default backend. If you need to switch to "theano", the above lines will do that job. – Hafizur Rahman Jan 15 '18 at 05:27
-
-
Did not work for me as I've created my own question: https://stackoverflow.com/questions/62735466/how-can-i-change-the-backend-of-keras-dynamically – talha06 Jul 05 '20 at 03:44
-
@talha06 which system are you using - Windows, Mac or Linux? I'd recommend editing your `keras.json` file. So locate the file and change it accordingly. – Hafizur Rahman Jul 05 '20 at 23:52
-
@HafizurRahman Yes, I'm aware of that - but I need to change the backend dynamically. – talha06 Jul 06 '20 at 07:55
Type following on command prompt and press enter:
%USERPROFILE%/.keras/keras.json
Change backend in the opened text file and save it. You are done.

- 2,905
- 17
- 15
For Linux systems, the hidden .keras directory will be created in the user’s home directory. To observe whether or not it has been created, run the following command from your home directory (the -a allows you to see hidden files and directories).
ls –a
If the directory is there, then cd into it and modify the keras.json file. If it’s not there, then create the directory with
mkdir .keras
Then create the file with
touch keras.json
Then edit the file to make the config changes you referenced to change the backend engine to Theano.
This process is covered fully in this video.

- 2,975
- 2
- 13
- 13
Termux - Android 11 - Finally working with H5py 3.4.0 Keras 2.2.0 Theano 1.0.5 Numpy 1.17.5
I find that packages sometimes do not install when built and --update. ***** You need to manually uninstall / install. .. I fond
-
As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Miguel Jan 13 '22 at 06:39
I am not sure if this possible anymore.
After some searching I came across this keras 2.4.0 release note, where they state that they discountinued multi-backend Keras.
Am not exactly an expert so I can't tell if this is possible anymore. You might try using an older keras version.
The release Note page: https://github.com/keras-team/keras/releases/tag/2.4.0

- 1,008
- 3
- 16
- 34

- 13
- 5