【问题标题】:changing keras backend to Theano in Ipython Notebook在 Ipython Notebook 中将 keras 后端更改为 Theano
【发布时间】:2017-07-28 09:40:50
【问题描述】:

我确实按照Keras documentation page 上的说明更改了keras.json 文件。但是在我的 Ipython notebook 中,它仍然说我使用 Tensorflow 作为后端。

也许它与 Jupyter 设置有关?请帮助。我什至不知道如何弄清楚问题出在哪里。谢谢!

【问题讨论】:

  • this
  • 感谢@ParagS.Chandakkar。但是它对我不起作用。当我执行keras.backend.backend() 时,它仍然显示tensorflow。也许我可以通过卸载 tensorflow 来解决这个问题?
  • 你试过KERAS_BACKEND=theano jupyter notebook --no-browser --ip xxx.xxx.xxx.xxx吗?然后 keras.backend.set_image_dim_ordering('tf')
  • 您是否尝试将其关闭再打开?
  • @maz 不,我没有。按照您的建议,我应该使用什么 ip 地址来替换 xxx.xxx.xxx.xxx?

标签: python deep-learning theano ipython-notebook keras


【解决方案1】:

您可以在笔记本的开头尝试以下操作:

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")

基本上,环境 KERAS_BACKEND 可以在某些时候被 Jupyter 覆盖,因此这是在导入 keras.backend 之前强制它成为某种东西的一种方法。

【讨论】:

  • 我正在接收........BaseException: 此脚本使用其他后端...
【解决方案2】:

python 2.7 中的工作原理 - 动态更改 Keras 后端

# 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

-- 不确定,如果我们不能动态更改后端,这会有什么帮助。

-- 相反,帮助我的是以下内容: How to switch Backend with Keras (from TensionFlow to Theano)

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)

iPython 中的输出

tensorflow
Change Keras Backend to Theano
theano

【讨论】:

  • 对于python 3.4及以上版本,使用import importlib; importlib.reload而不是reload
猜你喜欢
  • 2017-12-12
  • 2017-10-29
  • 2017-06-27
  • 2018-04-26
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多