diff --git a/docs/templates/backend.md b/docs/templates/backend.md index 72c0da164..9e40a19c5 100644 --- a/docs/templates/backend.md +++ b/docs/templates/backend.md @@ -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. diff --git a/keras/backend/__init__.py b/keras/backend/__init__.py index be33bde10..ad038c683 100644 --- a/keras/backend/__init__.py +++ b/keras/backend/__init__.py @@ -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 *