-
Notifications
You must be signed in to change notification settings - Fork 49
/
rnn_tf.py
300 lines (264 loc) · 9.21 KB
/
rnn_tf.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"""
Text generation using a Recurrent Neural Network (LSTM).
"""
import argparse
import os
import random
import time
import numpy as np
import tensorflow as tf
class ModelNetwork:
"""
RNN with num_layers LSTM layers and a fully-connected output layer
The network allows for a dynamic number of iterations, depending on the
inputs it receives.
out (fc layer; out_size)
^
lstm
^
lstm (lstm size)
^
in (in_size)
"""
def __init__(self, in_size, lstm_size, num_layers, out_size, session,
learning_rate=0.003, name="rnn"):
self.scope = name
self.in_size = in_size
self.lstm_size = lstm_size
self.num_layers = num_layers
self.out_size = out_size
self.session = session
self.learning_rate = tf.constant(learning_rate)
# Last state of LSTM, used when running the network in TEST mode
self.lstm_last_state = np.zeros(
(self.num_layers * 2 * self.lstm_size,)
)
with tf.variable_scope(self.scope):
# (batch_size, timesteps, in_size)
self.xinput = tf.placeholder(
tf.float32,
shape=(None, None, self.in_size),
name="xinput"
)
self.lstm_init_value = tf.placeholder(
tf.float32,
shape=(None, self.num_layers * 2 * self.lstm_size),
name="lstm_init_value"
)
# LSTM
self.lstm_cells = [
tf.contrib.rnn.BasicLSTMCell(
self.lstm_size,
forget_bias=1.0,
state_is_tuple=False
) for i in range(self.num_layers)
]
self.lstm = tf.contrib.rnn.MultiRNNCell(
self.lstm_cells,
state_is_tuple=False
)
# Iteratively compute output of recurrent network
outputs, self.lstm_new_state = tf.nn.dynamic_rnn(
self.lstm,
self.xinput,
initial_state=self.lstm_init_value,
dtype=tf.float32
)
# Linear activation (FC layer on top of the LSTM net)
self.rnn_out_W = tf.Variable(
tf.random_normal(
(self.lstm_size, self.out_size),
stddev=0.01
)
)
self.rnn_out_B = tf.Variable(
tf.random_normal(
(self.out_size,), stddev=0.01
)
)
outputs_reshaped = tf.reshape(outputs, [-1, self.lstm_size])
network_output = tf.matmul(
outputs_reshaped,
self.rnn_out_W
) + self.rnn_out_B
batch_time_shape = tf.shape(outputs)
self.final_outputs = tf.reshape(
tf.nn.softmax(network_output),
(batch_time_shape[0], batch_time_shape[1], self.out_size)
)
# Training: provide target outputs for supervised training.
self.y_batch = tf.placeholder(
tf.float32,
(None, None, self.out_size)
)
y_batch_long = tf.reshape(self.y_batch, [-1, self.out_size])
self.cost = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(
logits=network_output,
labels=y_batch_long
)
)
self.train_op = tf.train.RMSPropOptimizer(
self.learning_rate,
0.9
).minimize(self.cost)
# Input: X is a single element, not a list!
def run_step(self, x, init_zero_state=True):
# Reset the initial state of the network.
if init_zero_state:
init_value = np.zeros((self.num_layers * 2 * self.lstm_size,))
else:
init_value = self.lstm_last_state
out, next_lstm_state = self.session.run(
[self.final_outputs, self.lstm_new_state],
feed_dict={
self.xinput: [x],
self.lstm_init_value: [init_value]
}
)
self.lstm_last_state = next_lstm_state[0]
return out[0][0]
# xbatch must be (batch_size, timesteps, input_size)
# ybatch must be (batch_size, timesteps, output_size)
def train_batch(self, xbatch, ybatch):
init_value = np.zeros(
(xbatch.shape[0], self.num_layers * 2 * self.lstm_size)
)
cost, _ = self.session.run(
[self.cost, self.train_op],
feed_dict={
self.xinput: xbatch,
self.y_batch: ybatch,
self.lstm_init_value: init_value
}
)
return cost
def embed_to_vocab(data_, vocab):
"""
Embed string to character-arrays -- it generates an array len(data)
x len(vocab).
Vocab is a list of elements.
"""
data = np.zeros((len(data_), len(vocab)))
cnt = 0
for s in data_:
v = [0.0] * len(vocab)
v[vocab.index(s)] = 1.0
data[cnt, :] = v
cnt += 1
return data
def decode_embed(array, vocab):
return vocab[array.index(1)]
def load_data(input):
# Load the data
data_ = ""
with open(input, 'r') as f:
data_ += f.read()
data_ = data_.lower()
# Convert to 1-hot coding
vocab = sorted(list(set(data_)))
data = embed_to_vocab(data_, vocab)
return data, vocab
def check_restore_parameters(sess, saver):
""" Restore the previously trained parameters if there are any. """
ckpt = tf.train.get_checkpoint_state(os.path.dirname('saved/checkpoint'))
if ckpt and ckpt.model_checkpoint_path:
saver.restore(sess, ckpt.model_checkpoint_path)
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--input_file",
type=str,
default="data/shakespeare.txt",
help="Text file to load."
)
parser.add_argument(
"--test_prefix",
type=str,
default="The ",
help="Test text prefix to train the network."
)
parser.add_argument(
"--ckpt_file",
type=str,
default="saved/model.ckpt",
help="Model checkpoint file to load."
)
parser.add_argument(
"--mode",
type=str,
default="talk",
choices=set(("talk", "train")),
help="Execution mode: talk or train."
)
args = parser.parse_args()
ckpt_file = None
TEST_PREFIX = args.test_prefix # Prefix to prompt the network in test mode
if args.ckpt_file:
ckpt_file = args.ckpt_file
# Load the data
data, vocab = load_data(args.input_file)
in_size = out_size = len(vocab)
lstm_size = 256 # 128
num_layers = 2
batch_size = 64 # 128
time_steps = 100 # 50
NUM_TRAIN_BATCHES = 20000
# Number of test characters of text to generate after training the network
LEN_TEST_TEXT = 500
# Initialize the network
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.InteractiveSession(config=config)
net = ModelNetwork(
in_size=in_size,
lstm_size=lstm_size,
num_layers=num_layers,
out_size=out_size,
session=sess,
learning_rate=0.003,
name="char_rnn_network"
)
sess.run(tf.global_variables_initializer())
saver = tf.train.Saver(tf.global_variables())
# 1) TRAIN THE NETWORK
if args.mode == "train":
check_restore_parameters(sess, saver)
last_time = time.time()
batch = np.zeros((batch_size, time_steps, in_size))
batch_y = np.zeros((batch_size, time_steps, in_size))
possible_batch_ids = range(data.shape[0] - time_steps - 1)
for i in range(NUM_TRAIN_BATCHES):
# Sample time_steps consecutive samples from the dataset text file
batch_id = random.sample(possible_batch_ids, batch_size)
for j in range(time_steps):
ind1 = [k + j for k in batch_id]
ind2 = [k + j + 1 for k in batch_id]
batch[:, j, :] = data[ind1, :]
batch_y[:, j, :] = data[ind2, :]
cst = net.train_batch(batch, batch_y)
if (i % 100) == 0:
new_time = time.time()
diff = new_time - last_time
last_time = new_time
print("batch: {} loss: {} speed: {} batches / s".format(
i, cst, 100 / diff
))
saver.save(sess, ckpt_file)
elif args.mode == "talk":
# 2) GENERATE LEN_TEST_TEXT CHARACTERS USING THE TRAINED NETWORK
saver.restore(sess, ckpt_file)
TEST_PREFIX = TEST_PREFIX.lower()
for i in range(len(TEST_PREFIX)):
out = net.run_step(embed_to_vocab(TEST_PREFIX[i], vocab), i == 0)
print("Sentence:")
gen_str = TEST_PREFIX
for i in range(LEN_TEST_TEXT):
# Sample character from the network according to the generated
# output probabilities.
element = np.random.choice(range(len(vocab)), p=out)
gen_str += vocab[element]
out = net.run_step(embed_to_vocab(vocab[element], vocab), False)
print(gen_str)
if __name__ == "__main__":
main()