### decoder of the inference model (Recursive decoder generator) import tensorflow.keras.layers as layers from tensorflow.keras.models import Model en_input_tensors = Input(shape=(en_len,en_vocab)) en_gru_layer = GRU(hsize, return_state=True) en_output_tensors, en_state = en_gru_layer(en_input_tensors) encoder = Model(inputs=en_input_tensors, outputs=en_state) # Our encoder model will generate output state from input data de_input_tensors = Input(shape=(1, fr_vocab)) # An input layer that accepts a single onehot encoded word de_state_in = Input(shape=(hsize,)) # Takes en_state as input (comes from en_gru_layer same tensor dimension) de_gru_layer = GRU(hsize, return_state=True) # GRU layer that produces output and a new state de_output_tensors, de_state_out = de_gru_layer(de_input_tensors, initial_state=de_state_in) # Recursion: state is another input de_dense_layer = layers.Dense(fr_vocab, activation='softmax') # prediction of most probable word among unique words in fr_vocab de_predictions = de_dense_layer(de_output_tensors) decoder = Model(inputs=[de_input_tensors, de_state_in], outputs=[de_predictions, de_state_out]) ### You need to copy the weights from the trained model to the inference model. ### Always remember to perform this step if you have different training and inference models. ### If you miss this step, the model will still run but the translations will be incorrect. en_gru_w = tr_en_gru.get_weights() # Load the weights to the encoder GRU from the trained model en_gru.set_weights(en_gru_w) # Set the weights of the encoder GRU of the inference model de_gru.set_weights(tr_de_gru.get_weights()) # Load and set the weights to the decoder GRU de_dense.set_weights(tr_de_dense.get_weights()) # Load and set the weights to the decoder Dense en_sent = ['the united states is sometimes chilly during december , but it is sometimes freezing in june .'] en_data = sents2seqs('source', en_st, onehot=True, reverse=True) # preprocess the sentence (tokenized, reversed, one-hot encoded) de_input_state = encoder.predict(en_data) # use encoder model to convert into decoder input state de_data = word2onehot(fr_tokens, 'sos', fr_vocab) # Converting "sos" (initial word) to a sequence fr_sent = '' for _ in range(fr_len): # Keep doing this until the end of the sentence is reached de_prob, de_input_state = decoder.predict([de_data,de_input_state]) # This is where de_input_state keeps changing for each word de_word = probs2word(de_prob, fr_tokens) # get the max probable word de_data = word2onehot(fr_tokens, de_w, fr_vocab) if (de_word == 'eos'): # Stop generating once end of sentence word "eos" is found break else: fr_sent += de_word + ' ' # Keep adding words to make a sentence def word2onehot(tokenizer, word, vocab_size): de_seq = tokenizer.texts_to_sequences([[word]]) de_onehot = to_categorical(de_seq, num_classes=vocab_size) de_onehot = np.expand_dims(de_onehot, axis=1) return de_onehot def probs2word(probs, tok): wid = np.argmax(probs[0,:], axis=-1) w = tok.index_word[wid] return w