-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoetry_generation_seq2seq.py
executable file
·248 lines (187 loc) · 6.45 KB
/
poetry_generation_seq2seq.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 14 15:19:24 2020
@author: ritu
"""
from __future__ import print_function, division
from builtins import range, input
# Note: you may need to update your version of future
# sudo pip install -U future
import os
import sys
import string
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from keras.models import Model
from keras.layers import Dense, Embedding, Input, LSTM
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.optimizers import Adam, SGD
import keras.backend as K
if len(K.tensorflow_backend._get_available_gpus()) > 0:
from keras.layers import CuDNNLSTM as LSTM
from keras.layers import CuDNNGRU as GRU
abspath = os.path.abspath(__file__)
dname = os.path.dirname(abspath)
os.chdir(dname)
# some configuration
MAX_SEQUENCE_LENGTH = 100
MAX_VOCAB_SIZE = 3000
EMBEDDING_DIM = 50
VALIDATION_SPLIT = 0.2
BATCH_SIZE = 128
EPOCHS = 2000
LATENT_DIM = 25
# load in the data
input_texts = []
target_texts = []
for line in open('robert_frost.txt'):
line = line.rstrip()
if not line:
continue
input_line = '<sos> ' + line
target_line = line + ' <eos>'
input_texts.append(input_line)
target_texts.append(target_line)
all_lines = input_texts + target_texts
# convert the sentences (strings) into integers
tokenizer = Tokenizer(num_words=MAX_VOCAB_SIZE, filters='')
tokenizer.fit_on_texts(all_lines)
input_sequences = tokenizer.texts_to_sequences(input_texts)
target_sequences = tokenizer.texts_to_sequences(target_texts)
# find max seq length
max_sequence_length_from_data = max(len(s) for s in input_sequences)
print('Max sequence length:', max_sequence_length_from_data)
# get word -> integer mapping
word2idx = tokenizer.word_index
print('Found %s unique tokens.' % len(word2idx))
assert('<sos>' in word2idx)
assert('<eos>' in word2idx)
# pad sequences so that we get a N x T matrix
max_sequence_length = min(max_sequence_length_from_data, MAX_SEQUENCE_LENGTH)
input_sequences = pad_sequences(input_sequences, maxlen=max_sequence_length, padding='post')
target_sequences = pad_sequences(target_sequences, maxlen=max_sequence_length, padding='post')
print('Shape of data tensor:', input_sequences.shape)
# load in pre-trained word vectors
print('Loading word vectors...')
word2vec = {}
with open(os.path.join('/Users/ritu/Documents/NLP/My_udemy_NLP/large_files/glove.6B/glove.6B.%sd.txt' % EMBEDDING_DIM)) as f:
# is just a space-separated text file in the format:
# word vec[0] vec[1] vec[2] ...
for line in f:
values = line.split()
word = values[0]
vec = np.asarray(values[1:], dtype='float32')
word2vec[word] = vec
print('Found %s word vectors.' % len(word2vec))
# prepare embedding matrix
print('Filling pre-trained embeddings...')
num_words = min(MAX_VOCAB_SIZE, len(word2idx) + 1)
embedding_matrix = np.zeros((num_words, EMBEDDING_DIM))
for word, i in word2idx.items():
if i < num_words:
embedding_vector = word2vec.get(word)
if embedding_vector is not None:
# words not found in embedding index will be all zeros.
embedding_matrix[i] = embedding_vector
# one-hot the targets
# (can't use sparse cross-entropy) doesnt work when each target is a sequence
one_hot_targets = np.zeros((len(input_sequences), max_sequence_length, num_words))
for i, target_sequence in enumerate(target_sequences):
for t, word in enumerate(target_sequence):
if word > 0:
one_hot_targets[i, t, word] = 1
# load pre-trained word embeddings into an Embedding layer
embedding_layer = Embedding(
num_words,
EMBEDDING_DIM,
weights=[embedding_matrix],
# trainable=False
)
print('Building model...')
# create an LSTM network with a single LSTM
input_ = Input(shape=(max_sequence_length,))
# set the intitial hidden and cell state rather to be able to use the same during prediction
initial_h = Input(shape=(LATENT_DIM,))
initial_c = Input(shape=(LATENT_DIM,))
x = embedding_layer(input_)
lstm = LSTM(LATENT_DIM, return_sequences=True, return_state=True)
x, _, _ = lstm(x, initial_state=[initial_h, initial_c]) # don't need the states here
dense = Dense(num_words, activation='softmax')
output = dense(x)
model = Model([input_, initial_h, initial_c], output)
model.compile(
loss='categorical_crossentropy',
# optimizer='rmsprop',
optimizer=Adam(lr=0.01),
# optimizer=SGD(lr=0.01, momentum=0.9),
metrics=['accuracy']
)
# Accuracy doesnt mean much, since we are generating poetry, next word after the couldnt be many
# other words, theres no right word
print('Training model...')
z = np.zeros((len(input_sequences), LATENT_DIM))
r = model.fit(
[input_sequences, z, z],
one_hot_targets,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
validation_split=VALIDATION_SPLIT
)
# plot some data
plt.plot(r.history['loss'], label='loss')
plt.plot(r.history['val_loss'], label='val_loss')
plt.legend()
plt.show()
# accuracies
plt.plot(r.history['accuracy'], label='acc')
plt.plot(r.history['val_accuracy'], label='val_acc')
plt.legend()
plt.show()
'''
Prediction: New Model, cause input and output lengths are different
'''
# make a sampling model
input2 = Input(shape=(1,)) # we'll only input one word at a time
x = embedding_layer(input2)
x, h, c = lstm(x, initial_state=[initial_h, initial_c]) # now we need states to feed back in
output2 = dense(x)
sampling_model = Model([input2, initial_h, initial_c], [output2, h, c])
# reverse word2idx dictionary to get back words
# during prediction
idx2word = {v:k for k, v in word2idx.items()}
def sample_line():
# initial inputs
np_input = np.array([[ word2idx['<sos>'] ]])
h = np.zeros((1, LATENT_DIM))
c = np.zeros((1, LATENT_DIM))
# so we know when to quit
eos = word2idx['<eos>']
# store the output here
output_sentence = []
for _ in range(max_sequence_length):
o, h, c = sampling_model.predict([np_input, h, c])
# print("o.shape:", o.shape, o[0,0,:10])
# idx = np.argmax(o[0,0])
probs = o[0,0]
if np.argmax(probs) == 0:
print("wtf")
probs[0] = 0
probs /= probs.sum()
idx = np.random.choice(len(probs), p=probs)
if idx == eos:
break
# accuulate output
output_sentence.append(idx2word.get(idx, '<WTF %s>' % idx))
# make the next input into model
np_input[0,0] = idx
return ' '.join(output_sentence)
# generate a 4 line poem
while True:
for _ in range(4):
print(sample_line())
ans = input("---generate another? [Y/n]---")
if ans and ans[0].lower().startswith('n'):
break