-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrn.py
124 lines (95 loc) · 3.66 KB
/
rn.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
from __future__ import print_function
from keras.models import Sequential
from keras.layers.core import Dense, Activation, Dropout, Flatten
from keras.layers.recurrent import LSTM
from keras.utils.data_utils import get_file
import numpy as np
import random
import sys
import os
path = "datasets/cuarteCorpus.txt"
sys.stdout = open('output.txt','wt')
try:
text = open(path).read().lower()
except UnicodeDecodeError:
import codecs
text = codecs.open(path, encoding='utf-8').read().lower()
print('corpus length:', len(text))
chars = set(text)
words = set(open('datasets/cuarteCorpus.txt').read().lower().split())
print("chars:",type(chars))
print("words",type(words))
print("total number of unique words",len(words))
print("total number of unique chars", len(chars))
word_indices = dict((c, i) for i, c in enumerate(words))
indices_word = dict((i, c) for i, c in enumerate(words))
print("word_indices", type(word_indices), "length:",len(word_indices) )
print("indices_words", type(indices_word), "length", len(indices_word))
maxlen = 30
step = 3
print("maxlen:",maxlen,"step:", step)
sentences = []
next_words = []
next_words= []
sentences1 = []
list_words = []
sentences2=[]
list_words=text.lower().split()
for i in range(0,len(list_words)-maxlen, step):
sentences2 = ' '.join(list_words[i: i + maxlen])
sentences.append(sentences2)
next_words.append((list_words[i + maxlen]))
print('nb sequences(length of sentences):', len(sentences))
print("length of next_word",len(next_words))
print('Vectorization...')
X = np.zeros((len(sentences), maxlen, len(words)), dtype=np.bool)
y = np.zeros((len(sentences), len(words)), dtype=np.bool)
for i, sentence in enumerate(sentences):
for t, word in enumerate(sentence.split()):
X[i, t, word_indices[word]] = 1
y[i, word_indices[next_words[i]]] = 1
print('Build model...')
model = Sequential()
model.add(LSTM(512, return_sequences=True, input_shape=(maxlen, len(words))))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(len(words)))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
if os.path.isfile('GoTweights'):
model.load_weights('GoTweights')
def sample(preds, temperature=1.0):
# helper function to sample an index from a probability array
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)
# train the model, output generated text after each iteration
for iteration in range(1, 300):
model.fit(X, y, batch_size=128, epochs=2)
model.save_weights('GoTweights',overwrite=True)
start_index = random.randint(0, len(list_words) - maxlen - 1)
for diversity in [0.2, 0.5, 1.0, 1.2]:
generated = ''
sentence = list_words[start_index: start_index + maxlen]
generated += ' '.join(sentence)
sys.stdout.write(generated)
verse_length = random.randint(4,8)
for i in range(verse_length):
x = np.zeros((1, maxlen, len(words)))
for t, word in enumerate(sentence):
x[0, t, word_indices[word]] = 1.
preds = model.predict(x, verbose=0)[0]
next_index = sample(preds, diversity)
next_word = indices_word[next_index]
generated += next_word
del sentence[0]
verse_length = random.randint(4,8)
sentence.append(next_word)
sys.stdout.write(' ')
sys.stdout.write(next_word)
sys.stdout.flush()
print()
#model.save_weights('weights')