Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An error when saving/opening model and weights (babi_memnn)! #2659

Closed
dmikulic opened this issue May 8, 2016 · 9 comments
Closed

An error when saving/opening model and weights (babi_memnn)! #2659

dmikulic opened this issue May 8, 2016 · 9 comments

Comments

@dmikulic
Copy link

dmikulic commented May 8, 2016

I just used the example script examples/babi_memnn.py and added the saving code at the end of the file.

json_string = answer.to_json()
open('my_model_architecture.json', 'w').write(json_string)
answer.save_weights('my_model_weights.h5', overwrite=True)

And after the training is finished I try to load the model with

from keras.models import model_from_json
model = model_from_json(open('my_model_architecture.json').read())
model.load_weights('my_model_weights.h5')

and then get the Exception: You are trying to load a weight file containing 11 layers into a model with 13 layers.

Here is the gist https://gist.github.com/anonymous/f433fa0b79d251b77edf8d862071841a

@fchollet
Copy link
Member

fchollet commented May 9, 2016

Yes, there appears to be an issue with model serialization for deeply nested Sequential models. I'm looking into it.

Note that a simple fix would be to use the functional API (recommended for complex models) instead of deeply nested Sequential models.

@akmahaja
Copy link

akmahaja commented May 17, 2016

@fchollet When I am using the functional API to create the model and try to save it using the following line, It gives me the following error saying:

  File "trainer.py", line 48, in create_training_features
  json_string = model.to_json()
  File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 2368, in to_json
config = self.get_config()
 File "/usr/local/lib/python2.7/site-packages/keras/engine/topology.py", line 2163, in get_config
new_node_index = node_conversion_map[node_key]
KeyError: 'input_1_ib-0'

@fchollet
Copy link
Member

Please provide the code to reproduce your error.

@akmahaja
Copy link

akmahaja commented May 17, 2016

input1 = Input(shape=(maxlen,), dtype='int32',name='input1')
input2 = Input(shape=(maxlen,), dtype='int32',name='input2')
embedding = Embedding(input_dim=2000,
                         output_dim=300,
                         weights=weights)
e1=embedding(input1)
e2=embedding(input2)
dropout = Dropout(0.25)

f_rnn = LSTM(259, return_sequences=True, dropout_U=0.2, consume_less='mem')
b_rnn = LSTM(259, return_sequences=True, dropout_U=0.2, consume_less='mem',go_backwards=True)

input1_f_rnn = f_rnn(e1)
input1_b_rnn = b_rnn(e1)
input1_f_dropout = dropout(input1_f_rnn)
input1_b_dropout = dropout(input1_b_rnn)

# maxpooling
maxpool = Lambda(lambda x: K.max(x, axis=1, keepdims=False), output_shape=lambda x: (x[0], x[2]))
input1_pool = merge([maxpool(input1_f_dropout), maxpool(input1_b_dropout)], mode='concat', concat_axis=-1)

#input2
f_rnn = AttentionLSTM(259, input1_pool, single_attn=True, dropout_U=0.2,
                          return_sequences=True, consume_less='mem')
b_rnn = AttentionLSTM(259, input1_pool, single_attn=True, dropout_U=0.2,
                          return_sequences=True, consume_less='mem', go_backwards=True)
input2_f_rnn = f_rnn(e2)
input2_b_rnn = b_rnn(e2)
input2_pool = merge([maxpool(input2_f_rnn), maxpool(input2_b_rnn)], mode='concat', concat_axis=-1)

#couple of more dense layers
activation = Activation('tanh')
activated= activation(input2_pool)

dense_layer=Dense(15, activation='softmax',name='output')
output_final=dense_layer(activated)

model=Model(input=[input1,input2],output=[output_final])
model.compile(loss='categorical_crossentropy',optimizer='adam')

model.fit({'input1': X_train1, 'input2': X_train2},{'output': y_train},nb_epoch=1)

json_string=model.to_json()
open('model_architecture.json', 'w').write(json_string)
answer.save_weights('model_weights.h5', overwrite=True)

@joelthchao
Copy link
Contributor

@akmahaja Is it your complete code? Your input2 is never used in the model, and remove input2 from Model(input=[input1, input2], ...) can solve the error.

@akmahaja
Copy link

@fchollet @joelthchao I am using the input2, I hadn't included that in the earlier version. I have updated the code with the latest version that I am working on, still the same issue.
AttentionLSTM that I am using in the above piece of code is taken from here:
https://github.com/codekansas/keras-language-modeling/blob/master/attention_lstm.py

@fchollet
Copy link
Member

Please provide the code to reproduce your error.

The keyword here is "reproduce", if you share some code snippet that I can't run and thus doesn't allow me to reproduce your error, that's not very helpful...

@akmahaja
Copy link

@fchollet this is the entire code here that I am using to reproduce the error. Are you referring to X_train1 and X_train2, y_train values?
After experimentation issue is mainly because the "input1" is not being fed all the way to the output layer. It stops at "input1_pool" in the above code, hence when the overall network diagram is also created it does not show up there.

@fchollet
Copy link
Member

I have fixed the issue from OP.

@akmahaja please open a thread for your own issue, which is different from this one. It should have a code snippet which I can copy and run (i.e. it should be self-contained, include imports and data. Use randomly generated data).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants