encoder_inputs = Input(shape=(80, 4096), name="encoder_inputs")
encoder_lstm = LSTM(512, return_state=True, return_sequences=True, name='encoder_lstm')
encoder_seq_out, state_h, state_c = encoder_lstm(encoder_inputs)
encoder_states = [state_h, state_c]
decoder_inputs = Input(shape=(10, 2226), name="decoder_inputs")
decoder_lstm = LSTM(512, return_sequences=True, return_state=True, name='decoder_lstm')
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
decoder_dense = Dense(2226, activation='softmax', name='decoder_softmax')
#Attention
attention_layer = Attention(use_scale=True)
attended_decoder_output = attention_layer([decoder_outputs, encoder_seq_out])
concatenate = Concatenate(axis=-1)
decoder_concat_input = concatenate([decoder_outputs, attended_decoder_output])
decoder_outputs = decoder_dense(decoder_concat_input)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
I have created a encoder decoder model like this with attention mechanism. But having trouble to
save the encoder and decoder models separately and loading the model. What is the proper way to
save the model and use it for inference?
What I have done is: (for saving)
self.encoder_model = Model(encoder_inputs, encoder_states)
decoder_state_input_h = Input(shape=(self.latent_dim,))
decoder_state_input_c = Input(shape=(self.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)
self.decoder_model = Model(
[decoder_inputs] + decoder_states_inputs,
[decoder_outputs] + decoder_states)
self.encoder_model.summary()
self.decoder_model.summary()
#saving the models
self.encoder_model.save(os.path.join(self.save_model_path, 'encoder_weights10.h5'))
self.decoder_model.save_weights(os.path.join(self.save_model_path, 'decoder_weights10.h5'))
And for loading the model it is done: And inference:
def inference_model(): with open(os.path.join('Saved_Models', 'saved_tokenizer' + str(2226)), 'rb') as file: tokenizer = joblib.load(file) #loading encoder model inf_encoder_model = load_model(os.path.join('Saved_Models', 'encoder_weights10.h5'))
# inference decoder model loading
decoder_inputs = Input(shape=(None, 2226))
decoder_dense = Dense(2226, activation='softmax')
decoder_lstm = LSTM(512, return_sequences=True, return_state=True)
decoder_state_input_h = Input(shape=(512,))
decoder_state_input_c = Input(shape=(512,))
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]
#Apply attention
attention_layer = Attention(use_scale=True)
attended_decoder_output = attention_layer([decoder_outputs, inf_encoder_model.output])
decoder_outputs = decoder_dense(attended_decoder_output)
inf_decoder_model = Model(
[decoder_inputs] + decoder_states_inputs,
[decoder_outputs] + decoder_states)
inf_decoder_model.load_weights(os.path.join('Saved_Models', 'decoder_weights10.h5'))
return tokenizer, inf_encoder_model, inf_decoder_model