[英]changing keras backend to Theano in Ipython Notebook
I did change the keras.json
file as instructed on the Keras documentation page . 我按照keras.json
文档页面上的说明更改了keras.json
文件。 But in my Ipython notebook, it still says I am using Tensorflow as backend. 但在我的Ipython笔记本中,它仍然说我使用Tensorflow作为后端。
Maybe it is related to the Jupyter settings somehow? 也许它与某些Jupyter设置有关? Please kindly help. 请帮忙。 I don't even know how to figure out where the problem came from. 我甚至不知道如何找出问题的来源。 Thanks! 谢谢!
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. 基本上环境KERAS_BACKEND可以在某些时候由Jupyter覆盖,所以这是在导入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) - 取而代之的是以下内容: 如何使用Keras切换后端(从TensionFlow切换到Theano)
Code in iPython 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 在iPython中输出
tensorflow
Change Keras Backend to Theano
theano
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.