description: use LSTM to predict words in Text in python
import packages
1 | import numpy |
load ascii text and covert to lowercase
1 | filename = "wonderland.txt" |
create mapping of unique chars to integers
1 | chars = sorted(list(set(raw_text))) |
summarize the loaded data
1 | # Total Characters: 147674 |
split the book text up into subsequences with a fixed length of 100 characters
1 | seq_length = 100 |
reshape X to be [samples, time steps, features]
1 | X = numpy.reshape(dataX, (n_patterns, seq_length, 1)) |
normalize
1 | X = X / float(n_vocab) |
one hot encode the output variable
1 | y = np_utils.to_categorical(dataY) |
define the LSTM model
1 | model = Sequential() |
define the checkpoint
1 | filepath="weights-improvement-{epoch:02d}-{loss:.4f}.hdf5" |
fit the model
1 | model.fit(X, y, epochs=20, batch_size=128, callbacks=callbacks_list) |