"""
Keras LSTM text generator taken from
"""
from tensorflow import keras
import numpy as np
import io
import sys
# prepare the text sampling function
def sample(preds, temperature=1.0):
# helper function to sample an index from a probability array
preds = np.asarray(preds).astype("float64")
#print(f"dividing by {temperature}")
preds = np.log(preds) / temperature
exp_preds = np.exp(preds)
preds = exp_preds / np.sum(exp_preds)
probas = np.random.multinomial(1, preds, 1)
return np.argmax(probas)
if __name__ == "__main__":
if len(sys.argv) < 3:
print("Usage: lstm_predict.py /path/to/corpus.txt /path/to/model")
exit()
corpus_path = sys.argv[1]
model_name = sys.argv[2]
model = keras.models.load_model(model_name)
with io.open(corpus_path, encoding="utf-8") as f:
text = f.read().lower()
#text = text.replace("\n", " ") # We remove newlines chars for nicer display
print("Corpus length:", len(text))
chars = sorted(list(set(text)))
print("Total chars:", len(chars))
char_indices = dict((c, i) for i, c in enumerate(chars))
indices_char = dict((i, c) for i, c in enumerate(chars))
# cut the text in semi-redundant sequences of maxlen characters
maxlen = 40
while True:
diversity = float(input("Provide diversity: "))
sentence = input("Provide seed: ")
generated = ""
for i in range(400):
x_pred = np.zeros((1, maxlen, len(chars)))
for t, char in enumerate(sentence):
x_pred[0, t, char_indices[char]] = 1.0
preds = model.predict(x_pred, verbose=0)[0]
next_index = sample(preds, diversity)
next_char = indices_char[next_index]
sentence = sentence[1:] + next_char
generated += next_char
print(generated)