Created
October 16, 2019 12:20
-
-
Save irdanish11/0b8d89c486e145b8af8cb65520f6f3e5 to your computer and use it in GitHub Desktop.
Predict one word at each time step and only return one sentence.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| model = load_model('word_pred_Model4.h5') | |
| tokenizer = load(open('tokenizer_Model4','rb')) | |
| seq_len = 3 | |
| def gen_text(model, tokenizer, seq_len, seed_text, num_gen_words): | |
| output_text = [] | |
| input_text = seed_text | |
| for i in range(num_gen_words): | |
| encoded_text = tokenizer.texts_to_sequences([input_text])[0] | |
| pad_encoded = pad_sequences([encoded_text], maxlen=seq_len,truncating='pre') | |
| pred_word_ind = model.predict_classes(pad_encoded,verbose=0)[0] | |
| pred_word = tokenizer.index_word[pred_word_ind] | |
| input_text += ' '+pred_word | |
| output_text.append(pred_word) | |
| return ' '.join(output_text) | |
| print('\n\n===>Enter --exit to exit from the program') | |
| while True: | |
| seed_text = input('Enter string: ') | |
| if seed_text.lower() == '--exit': | |
| break | |
| else: | |
| out = gen_text(model, tokenizer, seq_len=seq_len, seed_text=seed_text, num_gen_words=5) | |
| print('Output: '+seed_text+' '+out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment