Kearas后端——Theano backend 和 Tensorflow backend
问题由来:
加载模型出错
解决一:Keras的后端没选对
解决二:模型参数不匹配
解决三:Kearas版本匹配
(以下方法不行)
1.
os.environ[‘KERAS_BACKEND’]=‘theano’
os.environ[‘KERAS_BACKEND’]
2.
I solved the issue by compelling congruency between the versions of Keras installed on my different machines.
It makes sense to think working with the same model across different versions of the library could engender hiccups due to discrepancies in convention employed in said versions.
First I looked up what versions were installed on my different machines (Guide here: https://stackoverflow.com/a/10215100/2661720),
and for the machine on which I was loading the model, I overwrote the existing Keras version with that installed on the model’s parent machine (Guide here: https://stackoverflow.com/a/33812968/2661720).
This was the thought process that sublimated the problem for me.
到底有何区别?
如何切换?(特别是在colab)
本地:https://www.jianshu.com/p/dd1af9c07b81
云端:https://stackoverflow.com/questions/40310035/how-to-change-keras-backend-wheres-the-json-file
折腾了一天多,试过很多方法,最终成功实现,由默认的tensorflow切换到theano:
主要参考:https://stackoverflow.com/questions/42177658/how-to-switch-backend-with-keras-from-tensorflow-to-theano?rq=1
附代码:
import importlib
from keras import backend as K
from os import environ
#user defined function to change keras backend
def set_keras_backend(backend):
if K.backend() != backend:
environ[‘KERAS_BACKEND’] = backend
importlib.reload(K)
assert K.backend() == backend
#call the function with “theano”
set_keras_backend(“theano”)
检验:
keras.backend.backend() # 显示正在使用的Kerasbacked