-
Notifications
You must be signed in to change notification settings - Fork 7
/
lstm.py
54 lines (46 loc) · 1.6 KB
/
lstm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/python
import keras
from keras.models import load_model
import numpy as np
from random import choice, randint
import json
#metamorphosis, shakespear , gutenberg, darwin
source = 'darwin'
model = load_model('models/' + source + '.h5')
text = open('source_text/' + source + '.txt').read().lower()
maxlen = 40
chars = sorted(list(set(text)))
char_indices = dict((c,i) for i, c in enumerate(chars))
indices_char = dict((i,c) for i,c in enumerate(chars))
# helper function to sample an index from a probability array
def sample(preds, temperature=0.5):
preds = np.asarray(preds).astype('float64')
preds = np.log(preds) / temperature
exp_preds = np.exp(preds)
preds = exp_preds / np.sum(exp_preds)
probas = np.random.multinomial(1, preds, 1)
return np.argmax(probas)
# Generate LSTM text
def lstmText(classification):
seeds = []
for element in classification:
element = " ".join(element[1].split("_"))
seeds.append(element)
sentence = seeds[0]
while len(sentence) < maxlen:
sentence += sentence
if len(sentence) > maxlen:
sentence = sentence[:maxlen]
temperature = float(0.3)
length = int(100)
generated = ''
for i in range(length):
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, temperature)
next_char = indices_char[next_index]
generated += next_char
sentence = sentence[1:] + next_char
return "the " + seeds[0] + generated;