-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathlstm_train.py
211 lines (169 loc) · 7.58 KB
/
lstm_train.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
Example script to train a network to generate text with the style of a given corpus
--By word--
It is recommended to run this script on GPU, as recurrent
networks are quite computationally intensive.
Based on
https://github.com/keras-team/keras/blob/master/examples/lstm_text_generation.py
20 epochs should be enough to get decent results.
Uses data generator to avoid loading all the test set into memory.
Saves the weights and model every epoch.
"""
from __future__ import print_function
from keras.callbacks import LambdaCallback, ModelCheckpoint, EarlyStopping
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, LSTM, Bidirectional
import numpy as np
import sys
import io
import os
import codecs
# Parameters: change to experiment different configurations
SEQUENCE_LEN = 10
MIN_WORD_FREQUENCY = 10
STEP = 1
BATCH_SIZE = 32
def shuffle_and_split_training_set(sentences_original, next_original, percentage_test=2):
# shuffle at unison
print('Shuffling sentences')
tmp_sentences = []
tmp_next_word = []
for i in np.random.permutation(len(sentences_original)):
tmp_sentences.append(sentences_original[i])
tmp_next_word.append(next_original[i])
cut_index = int(len(sentences_original) * (1.-(percentage_test/100.)))
x_train, x_test = tmp_sentences[:cut_index], tmp_sentences[cut_index:]
y_train, y_test = tmp_next_word[:cut_index], tmp_next_word[cut_index:]
print("Size of training set = %d" % len(x_train))
print("Size of test set = %d" % len(y_test))
return (x_train, y_train), (x_test, y_test)
# Data generator for fit and evaluate
def generator(sentence_list, next_word_list, batch_size):
index = 0
while True:
x = np.zeros((batch_size, SEQUENCE_LEN, len(words)), dtype=np.bool)
y = np.zeros((batch_size, len(words)), dtype=np.bool)
for i in range(batch_size):
for t, w in enumerate(sentence_list[index % len(sentence_list)]):
x[i, t, word_indices[w]] = 1
y[i, word_indices[next_word_list[index % len(sentence_list)]]] = 1
index = index + 1
yield x, y
def print_vocabulary(words_file_path, words_set):
words_file = codecs.open(words_file_path, 'w', encoding='utf8')
for w in words_set:
if w != "\n":
words_file.write(w+"\n")
else:
words_file.write(w)
words_file.close()
def get_model(dropout=0.2):
print('Build model...')
model = Sequential()
model.add(Bidirectional(LSTM(128), input_shape=(SEQUENCE_LEN, len(words))))
if dropout > 0:
model.add(Dropout(dropout))
model.add(Dense(len(words)))
model.add(Activation('softmax'))
return model
# Functions from keras-team/keras/blob/master/examples/lstm_text_generation.py
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)
def on_epoch_end(epoch, logs):
# Function invoked at end of each epoch. Prints generated text.
examples_file.write('\n----- Generating text after Epoch: %d\n' % epoch)
# Randomly pick a seed sequence
seed_index = np.random.randint(len(sentences+sentences_test))
seed = (sentences+sentences_test)[seed_index]
for diversity in [0.3, 0.4, 0.5, 0.6, 0.7]:
sentence = seed
examples_file.write('----- Diversity:' + str(diversity) + '\n')
examples_file.write('----- Generating with seed:\n"' + ' '.join(sentence) + '"\n')
examples_file.write(' '.join(sentence))
for i in range(50):
x_pred = np.zeros((1, SEQUENCE_LEN, len(words)))
for t, word in enumerate(sentence):
x_pred[0, t, word_indices[word]] = 1.
preds = model.predict(x_pred, verbose=0)[0]
next_index = sample(preds, diversity)
next_word = indices_word[next_index]
sentence = sentence[1:]
sentence.append(next_word)
examples_file.write(" "+next_word)
examples_file.write('\n')
examples_file.write('='*80 + '\n')
examples_file.flush()
if __name__ == "__main__":
# Argument check
if len(sys.argv) != 4:
print('\033[91m' + 'Argument Error!\nUsage: python3 lstm_train.py '
'<path_to_corpus> <examples_txt> <vocabulary_txt>' + '\033[0m')
exit(1)
if not os.path.isfile(sys.argv[1]):
print('\033[91mERROR: ' + sys.argv[1] + ' is not a file!' + '\033[0m')
exit(1)
corpus = sys.argv[1]
examples = sys.argv[2]
vocabulary = sys.argv[3]
if not os.path.isdir('./checkpoints/'):
os.makedirs('./checkpoints/')
with io.open(corpus, encoding='utf-8') as f:
text = f.read().lower().replace('\n', ' \n ')
print('Corpus length in characters:', len(text))
text_in_words = [w for w in text.split(' ') if w.strip() != '' or w == '\n']
print('Corpus length in words:', len(text_in_words))
# Calculate word frequency
word_freq = {}
for word in text_in_words:
word_freq[word] = word_freq.get(word, 0) + 1
ignored_words = set()
for k, v in word_freq.items():
if word_freq[k] < MIN_WORD_FREQUENCY:
ignored_words.add(k)
words = set(text_in_words)
print('Unique words before ignoring:', len(words))
print('Ignoring words with frequency <', MIN_WORD_FREQUENCY)
words = sorted(set(words) - ignored_words)
print('Unique words after ignoring:', len(words))
print_vocabulary(vocabulary, words)
word_indices = dict((c, i) for i, c in enumerate(words))
indices_word = dict((i, c) for i, c in enumerate(words))
# cut the text in semi-redundant sequences of SEQUENCE_LEN words
sentences = []
next_words = []
ignored = 0
for i in range(0, len(text_in_words) - SEQUENCE_LEN, STEP):
# Only add the sequences where no word is in ignored_words
if len(set(text_in_words[i: i+SEQUENCE_LEN+1]).intersection(ignored_words)) == 0:
sentences.append(text_in_words[i: i + SEQUENCE_LEN])
next_words.append(text_in_words[i + SEQUENCE_LEN])
else:
ignored = ignored + 1
print('Ignored sequences:', ignored)
print('Remaining sequences:', len(sentences))
# x, y, x_test, y_test
(sentences, next_words), (sentences_test, next_words_test) = shuffle_and_split_training_set(
sentences, next_words
)
model = get_model()
model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'])
file_path = "./checkpoints/LSTM_LYRICS-epoch{epoch:03d}-words%d-sequence%d-minfreq%d-" \
"loss{loss:.4f}-acc{acc:.4f}-val_loss{val_loss:.4f}-val_acc{val_acc:.4f}" % \
(len(words), SEQUENCE_LEN, MIN_WORD_FREQUENCY)
checkpoint = ModelCheckpoint(file_path, monitor='val_acc', save_best_only=True)
print_callback = LambdaCallback(on_epoch_end=on_epoch_end)
early_stopping = EarlyStopping(monitor='val_acc', patience=5)
callbacks_list = [checkpoint, print_callback, early_stopping]
examples_file = open(examples, "w")
model.fit_generator(generator(sentences, next_words, BATCH_SIZE),
steps_per_epoch=int(len(sentences)/BATCH_SIZE) + 1,
epochs=100,
callbacks=callbacks_list,
validation_data=generator(sentences_test, next_words_test, BATCH_SIZE),
validation_steps=int(len(sentences_test)/BATCH_SIZE) + 1)