3

I did change the keras.json file as instructed on the Keras documentation page. But in my Ipython notebook, it still says I am using Tensorflow as backend.

enter image description here

Maybe it is related to the Jupyter settings somehow? Please kindly help. I don't even know how to figure out where the problem came from. Thanks!

user3768495
  • 4,077
  • 7
  • 32
  • 58

2 Answers2

5

You could try the following at the start of the notebook:

import os
os.environ["KERAS_BACKEND"] = "theano"
import keras; import keras.backend
if keras.backend.backend() != 'theano':
    raise BaseException("This script uses other backend")
else:
    keras.backend.set_image_dim_ordering('th')
    print("Backend ok")

Basically environment KERAS_BACKEND can be overwriten at some point by Jupyter so this is one way to force it to be something before you import keras.backend.

holli
  • 1,546
  • 16
  • 17
1

What works in python 2.7 - dynamically changing Keras backend

# When I executed the suggestion -- the output I got..
BaseExceptionTraceback (most recent call last)
<ipython-input-7-c4352a2d60e6> in <module>()
      3 import keras; import keras.backend
      4 if keras.backend.backend() != 'theano':
----> 5     raise BaseException("This script uses other backend")
      6 else:
      7     keras.backend.set_image_dim_ordering('th')

BaseException: This script uses other backend

-- Not sure, how this would help if we are not able to dynamically change the backend.

-- Instead what helped me was the following: How to switch Backend with Keras (from TensionFlow to Theano)

Code in iPython

from keras import backend; print(backend._BACKEND)
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
print ("Change Keras Backend to Theano")        
set_keras_backend("theano")  
from keras import backend; print(backend._BACKEND)

Output in iPython

tensorflow
Change Keras Backend to Theano
theano
Ram B
  • 11
  • 3