In [2]:
import pickle
from keras.models import load_model

# model number to use in order to do the predictions
model_num = 18

meta_data_dic = pickle.load(open("./data/models/meta_data_dic.p", "rb"))
model = load_model("./data/models/model-" + str(model_num) + ".h5")

seq_len = meta_data_dic["seq_len"]
chars = meta_data_dic["chars"]
char_indices = meta_data_dic["char_indices"]
indices_char = meta_data_dic["indices_char"]
max_len = meta_data_dic["max_len"]
padd_char_st = meta_data_dic["padd_char_st"]
padd_char_end = meta_data_dic["padd_char_end"]

model.summary()


Using TensorFlow backend.
____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
gru_1 (GRU)                      (None, 100, 256)      304896      gru_input_1[0][0]                
____________________________________________________________________________________________________
dropout_1 (Dropout)              (None, 100, 256)      0           gru_1[0][0]                      
____________________________________________________________________________________________________
gru_2 (GRU)                      (None, 256)           393984      dropout_1[0][0]                  
____________________________________________________________________________________________________
dropout_2 (Dropout)              (None, 256)           0           gru_2[0][0]                      
____________________________________________________________________________________________________
dense_1 (Dense)                  (None, 140)           35980       dropout_2[0][0]                  
____________________________________________________________________________________________________
activation_1 (Activation)        (None, 140)           0           dense_1[0][0]                    
====================================================================================================
Total params: 734,860
Trainable params: 734,860
Non-trainable params: 0
____________________________________________________________________________________________________

In [17]:
import numpy as np
from utils import random_next_char

# sentence start that the bot will complete
sentence = "Hello America!"

x = np.zeros((seq_len, len(chars)))
x[:-len(sentence), char_indices[padd_char_st]] = 1
for i, char in enumerate(sentence[::-1]):
    x[-i-1, char_indices[char]] = 1
for i in range(max_len-seq_len-1):
    probs = model.predict(x.reshape((1, x.shape[0], len(chars))))
    next_char, pos = random_next_char(probs, indices_char, verbose=False)
    sentence += next_char
    x = np.concatenate((x[-seq_len + 1:, :], np.zeros((1, len(chars)))), axis=0)
    x[-1, pos] = 1
sentence = sentence.replace(padd_char_end, "")
print(sentence)


Hello America! Jef See, New Hampshire - the people voters to #MakeAmericaGreatAgain tomorrow.
https://t.co/WS5qT2fzz4‰Û_