I am trying to follow a tutorial on character based Neural Language Model, which attempts to predict "words in a sequence based on the specific words".
As instructed, I have generated the sequence of texts to a file, defined the language model and saved the model as well as the mapping characters (as *.pkl).
Next in order to generate texts (using the saved mapping), encoded the texts to integers and run the code for predicting characters in sequence using predict_classes(). Using the following function (code below) to predict a sequence of characters using seed text.
However I am running into the following error when I run the script:
ValueError: cannot reshape array of size 380 into shape (1,1,10)
This is the part where I am getting the error:
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))
encoded = encoded.reshape(1, encoded.shape[0], encoded.shape[1]) # <--- Error line
# 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 += out_char
return in_text
The function is being called with:
print(generate_seq(model, mapping, 10, 'Sing a son', 20))
Why am I getting this error?