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

Assertion error when loading keras model with theano backend

我在加载使用带有 theano 后端的 keras 构建的模型时遇到问题。我用的是Python2,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

对于如何save/load我的模型

的任何意见,我们将不胜感激

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

pip uninstall keras
pip install keras==2.2.4