Allows choosing the backend by setting the KERAS_BACKEND environment variable

This commit is contained in:
Julien Rebetez 2015-12-17 11:45:52 +01:00
parent 063dd7d6c5
commit ed92c14185
2 changed files with 14 additions and 0 deletions

@ -23,6 +23,15 @@ It probably looks like this:
Simply change the field `backend` to either `"theano"` or `"tensorflow"`, and Keras will use the new configuration next time you run any Keras code.
You can also define the environment variable ``KERAS_BACKEND`` and this will
override what is defined in your config file :
```bash
KERAS_BACKEND=tensorflow python -c "from keras import backend; print backend._BACKEND"
Using TensorFlow backend.
tensorflow
```
## Using the abstract Keras backend to write new code
If you want the Keras modules you write to be compatible with both Theano and TensorFlow, you have to write them via the abstract Keras backend API. Here's an intro.

@ -31,6 +31,11 @@ else:
# add new line in order for bash 'cat' display the content correctly
f.write(json.dumps(_config) + '\n')
if 'KERAS_BACKEND' in os.environ:
_backend = os.environ['KERAS_BACKEND']
assert _backend in {'theano', 'tensorflow'}
_BACKEND = _backend
if _BACKEND == 'theano':
print('Using Theano backend.')
from .theano_backend import *