Skip to content

Instantly share code, notes, and snippets.

@nahidalam
Created August 20, 2020 04:14
Show Gist options
  • Save nahidalam/b4c8dc54a88e4f843026662c7753966c to your computer and use it in GitHub Desktop.
Save nahidalam/b4c8dc54a88e4f843026662c7753966c to your computer and use it in GitHub Desktop.
def evaluate(sentence):
sentence = preprocess_sentence(sentence)
inputs = [inp_lang.word_index[i] for i in sentence.split(' ')]
inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs],
maxlen=max_length_inp,
padding='post')
inputs = tf.convert_to_tensor(inputs)
result = ''
hidden = [tf.zeros((1, units))]
enc_out, enc_hidden = encoder(inputs, hidden)
dec_hidden = enc_hidden
dec_input = tf.expand_dims([targ_lang.word_index['<start>']], 0)
for t in range(max_length_targ):
predictions, dec_hidden = decoder(dec_input,dec_hidden,enc_out)
print(predictions[0])
predicted_id = tf.argmax(predictions[0]).numpy()
print(predicted_id)
result += targ_lang.index_word[predicted_id] + ' '
if targ_lang.index_word[predicted_id] == '<end>':
#return result, sentence, attention_plot
return result, sentence
# the predicted ID is fed back into the model
dec_input = tf.expand_dims([predicted_id], 0)
return result, sentence
def translate(sentence):
result, sentence = evaluate(sentence)
print('Input: %s' % (sentence))
print('Predicted translation: {}'.format(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment