39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
from pickle import load
|
|
from keras.models import load_model
|
|
from keras.utils import to_categorical
|
|
from keras.preprocessing.sequence import pad_sequences
|
|
import keras as K
|
|
|
|
|
|
# generate a sequence of characters with a language model
|
|
def generate_seq(model, mapping, seq_length, seed_text, n_chars):
|
|
in_text = seed_text
|
|
# generate a fixed number of characters
|
|
for _ in range(n_chars):
|
|
# encode the characters as integers
|
|
encoded = [mapping[char] for char in in_text]
|
|
# truncate sequences to a fixed length
|
|
encoded = pad_sequences([encoded], maxlen=seq_length, truncating='pre')
|
|
# one hot encode
|
|
encoded = to_categorical(encoded, num_classes=len(mapping))
|
|
# predict character
|
|
yhat = model.predict_classes(encoded, verbose=0)
|
|
# reverse map integer to character
|
|
out_char = ''
|
|
for char, index in mapping.items():
|
|
if index == yhat:
|
|
out_char = char
|
|
break
|
|
# append to input
|
|
in_text += char
|
|
return in_text
|
|
|
|
|
|
# load the model
|
|
model = load_model('model.h5')
|
|
|
|
# load the mapping
|
|
mapping = load(open('mapping.pkl', 'rb'))
|
|
|
|
print(generate_seq(model, mapping, 10, 'the ', 1000))
|