Suzana Ilić
Suzana Ilić

Reputation: 73

LSTM text generation with Keras: What is diversity?

I'm working on a small text generation project using Keras and LSTM. Chollet's code works perfectly fine. Could someone explain to me the diversity steps 0.2, .05, 1.0, 1.2? What's exactly happening here? Thanks in advance!

for diversity in [0.2, 0.5, 1.0, 1.2]:
    print()
    print('----- diversity:', diversity)

    generated = ''
    sentence = text[start_index: start_index + maxlen]
    generated += sentence
    print('----- Generating with seed: "' + sentence + '"')
    sys.stdout.write(generated)

    for i in range(400):
        x = np.zeros((1, maxlen, len(chars)))
        for t, char in enumerate(sentence):
            x[0, t, char_indices[char]] = 1.

        preds = model.predict(x, verbose=0)[0]
        next_index = sample(preds, diversity)
        next_char = indices_char[next_index]

        generated += next_char
        sentence = sentence[1:] + next_char

        sys.stdout.write(next_char)
        sys.stdout.flush()
    print()

https://github.com/fchollet/keras/blob/master/examples/lstm_text_generation.py

Upvotes: 1

Views: 1332

Answers (1)

Julio Daniel Reyes
Julio Daniel Reyes

Reputation: 6365

Those are just different values of the temperature hyper-parameter.

This answer has a good explanation of what temperature means in this context.

Upvotes: 1

Related Questions