-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq2seq_init.py
164 lines (129 loc) · 4.24 KB
/
seq2seq_init.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
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 27 01:29:20 2019
@author: tanma
"""
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
MAX_SEQUENCE_LENGTH = 100
MAX_VOCAB_SIZE = 3000
EMBEDDING_DIM = 50
VALIDATION_SPLIT = 0.2
BATCH_SIZE = 128
EPOCHS = 2000
LATENT_DIM = 25
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
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)
word2idx = tokenizer.word_index
assert('<sos>' in word2idx)
assert('<eos>' in word2idx)
max_sequence_length_from_data = max(len(s) for s in input_sequences)
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')
word2vec = {}
with open(os.path.join('Glove Data/glove.6B.%sd.txt' % EMBEDDING_DIM),'rb') as f:
for line in f:
values = line.split()
word = values[0]
vec = np.asarray(values[1:], dtype='float32')
word2vec[word] = vec
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 < MAX_VOCAB_SIZE:
embedding_vector = word2vec.get(word)
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
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
embedding_layer = Embedding(
num_words,
EMBEDDING_DIM,
weights=[embedding_matrix],
# trainable=False
)
input_ = Input(shape=(max_sequence_length,))
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']
)
z = np.zeros((len(input_sequences), LATENT_DIM))
model.fit(
[input_sequences, z, z],
one_hot_targets,
batch_size=BATCH_SIZE,
epochs=EPOCHS,
validation_split=VALIDATION_SPLIT
)
input2 = Input(shape=(1,))
x = embedding_layer(input2)
x, h, c = lstm(x, initial_state=[initial_h, initial_c])
output2 = dense(x)
sampling_model = Model([input2, initial_h, initial_c], [output2, h, c])
idx2word = {v:k for k, v in word2idx.items()}
def sample_line():
np_input = np.array([[ word2idx['<sos>'] ]])
h = np.zeros((1, LATENT_DIM))
c = np.zeros((1, LATENT_DIM))
eos = word2idx['<eos>']
output_sentence = []
for _ in range(max_sequence_length):
o, h, c = sampling_model.predict([np_input, h, c])
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
output_sentence.append(idx2word.get(idx, '<WTF %s>' % idx))
np_input[0,0] = idx
return ' '.join(output_sentence)
while True:
for _ in range(4):
print(sample_line())
ans = input("---generate another? [Y/n]---")
if ans and ans[0].lower().startswith('n'):
break