farizrahman4u / recurrentshop

Framework for building complex recurrent neural networks with Keras
MIT License
766 stars 218 forks source link

Can't Save Model #41

Open masakistan opened 7 years ago

masakistan commented 7 years ago

I've been using the recurrentshop-1 branch and unable to save models that have RecurrentModel layers in them. Here's an example using the test_recurrent_model.py in the tests.

from recurrentshop import RecurrentModel
from keras.models import Model
from keras.layers import *

x = Input((5,), name = 'xinput' )
h_tm1 = Input((10,), name = 'hinput' )
h = add([Dense(10)(x), Dense(10, use_bias=False)(h_tm1)])
h = Activation('tanh')(h)
a = Input((7, 5), name = 'ainput' )

rnn = RecurrentModel(input=x, output=h, initial_states=h_tm1, final_states=h)
b = rnn(a)
model = Model(a, b)

model.compile(loss='mse', optimizer='sgd')
model.save( 'test.h5' )

it returns the following error:

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    model.save( 'test.h5' )
  File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.3-py2.7.egg/keras/engine/topology.py", line 2457, in save
    save_model(self, filepath, overwrite, include_optimizer)
  File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.3-py2.7.egg/keras/models.py", line 102, in save_model
    'config': model.get_config()
  File "/usr/local/lib/python2.7/dist-packages/Keras-2.0.3-py2.7.egg/keras/engine/topology.py", line 2320, in get_config
    new_node_index = node_conversion_map[node_key]
KeyError: 'ainput_ib-0'

What I've found is that somehow the input node names in the rnn layer are getting changed. This is problematic when connecting the rnn layer with the rest of the network. I found that if I give a specific name to the input node that connects to the rnn layer that the error goes away and the model is successfully saved.

from recurrentshop import RecurrentModel
from keras.models import Model
from keras.layers import *

x = Input((5,), name = 'xinput' )
h_tm1 = Input((10,), name = 'hinput' )
h = add([Dense(10)(x), Dense(10, use_bias=False)(h_tm1)])
h = Activation('tanh')(h)
a = Input((7, 5), name = 'private__optional_input_place_holder_1' )

rnn = RecurrentModel(input=x, output=h, initial_states=h_tm1, final_states=h)
b = rnn(a)
model = Model(a, b)

model.compile(loss='mse', optimizer='sgd')
model.save( 'test.h5' )

Though saving works doing this, I am unable to load the model afterwards. I get the following error:

ValueError: Unknown layer: _OptionalInputPlaceHolder

Should I be specifying _OptionalInputPlaceHolder as a custom_object when I'm using load_model?

Thanks!

Slyne commented 7 years ago

try to use model.load_weights("model_file")

kix2mix2 commented 6 years ago

Did you fix this in the end? I'm having the same problem

JiaqiuWang commented 6 years ago

I have same problem.

JiaqiuWang commented 6 years ago

model = Seq2Seq(input_shape... ....) # Instantiate a model object with the same configuration as above before loading weights model.load_weights('model.h5')