Keras: How can I save and load the whole model for offline inference with examples/lstm_seq2seq.py

Created on 2 Mar 2018  Â·  4Comments  Â·  Source: keras-team/keras

I implement a callback , override on_epoch_end, when save the model and decode_model ,there is a warning mag:

UserWarning: Layer lstm_2 was passed non-serializable keyword arguments: {'initial_state': [, ]}. They will not be included in the serialized model (and thus will be missing at deserialization time).
str(node.arguments) + '. They will not be included '
it means some weight can't save to disk, when inference

How can I save the model , encoder_model, decoder_model for offline test & application

save the model:
print('save s2s h5')
model.save('s2s.h5.'+str(epoch))
model_json = model.to_json()
with open("model.json."+str(epoch), "w") as json_file:
print('save s2s json')
json_file.write(model_json)

    encoder_model = Model(encoder_inputs, encoder_states)
    decoder_state_input_h = Input(shape=(latent_dim,))
    decoder_state_input_c = Input(shape=(latent_dim,))
    decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
    decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_states_inputs)
    decoder_states = [state_h, state_c]
    decoder_outputs = decoder_dense(decoder_outputs)
    decoder_model = Model([decoder_inputs] + decoder_states_inputs,[decoder_outputs] + decoder_states)

    #save encoder model
    print('save encoder h5')
    encoder_model.save('encoder_model.h5.'+str(epoch))
    encoder_model_json = encoder_model.to_json()
    with open("encoder_model.json."+str(epoch), "w") as json_file:
        print('save encoder json')
        json_file.write(encoder_model_json)

    #save decoder model
    print('save decoder h5')
    decoder_model.save('decoder_model.h5.'+str(epoch))
    decoder_model_json = decoder_model.to_json()
    with open("decoder_model.json."+str(epoch), "w") as json_file:
        print('save decoder json')
        json_file.write(decoder_model_json)

All 4 comments

I get the exact same warning using the code online for seq2seq.
Without touching any line of code I train on the given dataset and it gives reasonable results. The warning appears when I try to save the model.
If I load the saved model everything runs but I get completely wrong predictions.

you can refer to the example code

I got the same problem. Anyone has an idea?

the example code will show you how to load

Was this page helpful?
0 / 5 - 0 ratings

Related issues

harishkrishnav picture harishkrishnav  Â·  3Comments

braingineer picture braingineer  Â·  3Comments

farizrahman4u picture farizrahman4u  Â·  3Comments

amityaffliction picture amityaffliction  Â·  3Comments

KeironO picture KeironO  Â·  3Comments