使用 theano 后端加载 keras 模型时出现断言错误

Posted

技术标签:

【中文标题】使用 theano 后端加载 keras 模型时出现断言错误【英文标题】:Assertion error when loading keras model with theano backend 【发布时间】:2020-07-26 06:18:32 【问题描述】:

我在加载使用带有 theano 后端的 keras 构建的模型时遇到问题。我正在使用 Python 2、keras 版本 2.3.1、theano 版本 1.0.4。模型是这样构建、训练和保存的:

import cPickle
import os
os.environ['KERAS_BACKEND'] = 'theano'
from keras import Sequential
from keras.layers import Dense, Dropout
from keras.models import load_model

model = Sequential()
model.add(Dense(100, input_dim=len(predVars), activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(100, activation='elu'))
model.add(Dropout(0.25))
model.add(Dense(100, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(100, activation='elu'))
model.add(Dropout(0.25))
model.add(Dense(1, activation='relu'))
model.compile(loss='mean_squared_error', optimizer='adam', metrics=['mae'])
model.fit(X_train, y_train, epochs=5, batch_size=100, verbose=2)

model.save("model.pkl")

我也试过这样保存:

f = open("model.pkl", 'rb')
model = cPickle.load(f)
f.close()

模型训练成功,我什至可以使用它进行预测,但是当我尝试使用

加载文件时
model.save("model.pkl")

f = open("model.pkl", 'wb')
cPickle.dump(model, f, protocol=cPickle.HIGHEST_PROTOCOL)
f.close()

我收到以下错误(无论我使用的是 cPickle 还是常规加载功能,错误都是一样的):

Traceback (most recent call last):
  File "<input>", line 2, in <module>
  File "C:\Python27\lib\site-packages\keras\engine\network.py", line 1334, in __setstate__
    model = saving.unpickle_model(state)
  File "C:\Python27\lib\site-packages\keras\engine\saving.py", line 604, in unpickle_model
    return _deserialize_model(h5dict)
  File "C:\Python27\lib\site-packages\keras\engine\saving.py", line 274, in _deserialize_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "C:\Python27\lib\site-packages\keras\engine\saving.py", line 627, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "C:\Python27\lib\site-packages\keras\layers\__init__.py", line 168, in deserialize
    printable_module_name='layer')
  File "C:\Python27\lib\site-packages\keras\utils\generic_utils.py", line 147, in deserialize_keras_object
    list(custom_objects.items())))
  File "C:\Python27\lib\site-packages\keras\engine\sequential.py", line 302, in from_config
    model.add(layer)
  File "C:\Python27\lib\site-packages\keras\engine\sequential.py", line 166, in add
    layer(x)
  File "C:\Python27\lib\site-packages\keras\engine\base_layer.py", line 463, in __call__
    self.build(unpack_singleton(input_shapes))
  File "C:\Python27\lib\site-packages\keras\layers\core.py", line 895, in build
    constraint=self.kernel_constraint)
  File "C:\Python27\lib\site-packages\keras\engine\base_layer.py", line 279, in add_weight
    weight = K.variable(initializer(shape, dtype=dtype),
  File "C:\Python27\lib\site-packages\keras\initializers.py", line 227, in __call__
    dtype=dtype, seed=self.seed)
  File "C:\Python27\lib\site-packages\keras\backend\theano_backend.py", line 2706, in random_uniform
    return rng.uniform(shape, low=minval, high=maxval, dtype=dtype)
  File "C:\Python27\lib\site-packages\theano\sandbox\rng_mrg.py", line 872, in uniform
    rstates = self.get_substream_rstates(nstreams, dtype)
  File "C:\Python27\lib\site-packages\theano\configparser.py", line 117, in res
    return f(*args, **kwargs)
  File "C:\Python27\lib\site-packages\theano\sandbox\rng_mrg.py", line 771, in get_substream_rstates
    assert isinstance(dtype, str)
AssertionError

不胜感激有关如何保存/加载我的模型的任何意见

【问题讨论】:

【参考方案1】:

我遇到了同样的问题,它似乎与 Keras 2.3.x 与 Python 2.7 的使用有关。现在可能是切换到 Python 3 的好时机。 我做不到,所以我降级到 Keras 2.2.4,现在一切运行顺利。

pip uninstall keras
pip install keras==2.2.4

【讨论】:

@danielghis我从昨天开始面临这个问题现在我检查了你的答案。谢谢

以上是关于使用 theano 后端加载 keras 模型时出现断言错误的主要内容,如果未能解决你的问题,请参考以下文章

导入 keras 时出现 ValueError «您正在尝试使用旧的 GPU 后端»

keras 后端 theano/tensorflow

如何检查 Keras 后端是 tensorflow 还是 theano

将 Keras 与 Tensorflow 2、Theano 或 CNTK 后端一起使用是不是存在语法差异?

无法在 Google Cloud DL VM 中使用 Theano Keras 后端

Keras 切换后端(Theano和TensorFlow)