sonia
sonia

Reputation: 180

Assertion error when loading keras model with theano backend

I am having trouble loading a model built using keras with a theano backend. I am using Python 2, keras version 2.3.1, theano version 1.0.4. The model is built, trained, and saved as such:

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

I also tried saving like this:

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

The model trains successfully, and I am even able to use it to make predictions, but when I try to load the file using

model.save("model.pkl")

or

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

I get the following error (the error it the same regardless if I am using cPickle or the regular load function):

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

Would appreciate any input regarding how to save/load my model

Upvotes: 0

Views: 703

Answers (1)

danieleghisi
danieleghisi

Reputation: 48

I had this very same issue, it appears to be related to the usage of Keras 2.3.x with Python 2.7. It may perhaps be a good moment to switch to Python 3. I could not do that, so I downgraded to Keras 2.2.4 and everything runs smoothly now.

pip uninstall keras
pip install keras==2.2.4

Upvotes: 2

Related Questions