Training a model and generating new text

For training we need to select an batch_size and the number of epochs we want to train. For the batch_size I choose 128 which is just an arbitrary number. I trained the model for only 5 epochs so I didn’t need to wait for so long but if you want you can train it for a lot more.

model.fit(x, y, batch_size=128, epochs=5, callbacks=callbacks)

Training output:

Epoch 1/5

187271/187271 [==============================] - 225s 1ms/step - loss: 1.9731 ----- Generating text after Epoch: 0

----- diversity: 0.2

----- Generating with seed: "lge

on the right side of his top-hat to "

lge

on the right side of his top-hat to he wise as the bore with the stor and string that i was a bile that i was a contion with the man with the hadd and the striet with the striet in the stries in the struttle and the striet with the strange with the man with the struttle with the stratter with the striet with the street with the striet which when she with the strunt of the stright of my stright of the string that i shall had been whi

----- diversity: 0.5

----- Generating with seed: "lge

on the right side of his top-hat to "

lge

on the right side of his top-hat to he had putting the stratce, and that is street in the striet man would not the stepe which we had been of the strude

in our in my step withinst in some with the hudied that in had a had become and the corted to a give with his right with a comon was and confice my could to my sule i was and shugher. i little which a sitter and the site my dippene with a chair drive to be but the some his site with

To generate text ourselves we will create a function similar to the on_epoch_end function. It will take a random starting index, take out the next 40 chars from the text and then use them to make predictions. As a parameter we will pass it the length of the text we want to generate and the diversity of the generated text.

def generate_text(length, diversity):

# Get random starting text

start_index = random.randint(0, len(text) - maxlen - 1)

generated = ''

sentence = text[start_index: start_index + maxlen]

generated += sentence

for i in range(length):

x_pred = np.zeros((1, maxlen, len(chars)))

for t, char in enumerate(sentence):

x_pred[0, t, char_indices[char]] = 1.



preds = model.predict(x_pred, verbose=0)[0]

next_index = sample(preds, diversity)

next_char = indices_char[next_index]



generated += next_char

sentence = sentence[1:] + next_char

return generated

Now we can create text by just calling the generate_text function:

print(generate_text(500, 0.2)

Generated text: