-
-
Notifications
You must be signed in to change notification settings - Fork 664
/
model_rnn.py
323 lines (261 loc) · 12.7 KB
/
model_rnn.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
"""
@author: lilianweng
"""
import numpy as np
import os
import random
import re
import shutil
import time
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.contrib.tensorboard.plugins import projector
class LstmRNN(object):
def __init__(self, sess, stock_count,
lstm_size=128,
num_layers=1,
num_steps=30,
input_size=1,
embed_size=None,
logs_dir="logs",
plots_dir="images"):
"""
Construct a RNN model using LSTM cell.
Args:
sess:
stock_count (int): num. of stocks we are going to train with.
lstm_size (int)
num_layers (int): num. of LSTM cell layers.
num_steps (int)
input_size (int)
keep_prob (int): (1.0 - dropout rate.) for a LSTM cell.
embed_size (int): length of embedding vector, only used when stock_count > 1.
checkpoint_dir (str)
"""
self.sess = sess
self.stock_count = stock_count
self.lstm_size = lstm_size
self.num_layers = num_layers
self.num_steps = num_steps
self.input_size = input_size
self.use_embed = (embed_size is not None) and (embed_size > 0)
self.embed_size = embed_size or -1
self.logs_dir = logs_dir
self.plots_dir = plots_dir
self.build_graph()
def build_graph(self):
"""
The model asks for five things to be trained:
- learning_rate
- keep_prob: 1 - dropout rate
- symbols: a list of stock symbols associated with each sample
- input: training data X
- targets: training label y
"""
# inputs.shape = (number of examples, number of input, dimension of each input).
self.learning_rate = tf.placeholder(tf.float32, None, name="learning_rate")
self.keep_prob = tf.placeholder(tf.float32, None, name="keep_prob")
# Stock symbols are mapped to integers.
self.symbols = tf.placeholder(tf.int32, [None, 1], name='stock_labels')
self.inputs = tf.placeholder(tf.float32, [None, self.num_steps, self.input_size], name="inputs")
self.targets = tf.placeholder(tf.float32, [None, self.input_size], name="targets")
def _create_one_cell():
lstm_cell = tf.contrib.rnn.LSTMCell(self.lstm_size, state_is_tuple=True)
lstm_cell = tf.contrib.rnn.DropoutWrapper(lstm_cell, output_keep_prob=self.keep_prob)
return lstm_cell
cell = tf.contrib.rnn.MultiRNNCell(
[_create_one_cell() for _ in range(self.num_layers)],
state_is_tuple=True
) if self.num_layers > 1 else _create_one_cell()
if self.embed_size > 0 and self.stock_count > 1:
self.embed_matrix = tf.Variable(
tf.random_uniform([self.stock_count, self.embed_size], -1.0, 1.0),
name="embed_matrix"
)
# stock_label_embeds.shape = (batch_size, embedding_size)
stacked_symbols = tf.tile(self.symbols, [1, self.num_steps], name='stacked_stock_labels')
stacked_embeds = tf.nn.embedding_lookup(self.embed_matrix, stacked_symbols)
# After concat, inputs.shape = (batch_size, num_steps, input_size + embed_size)
self.inputs_with_embed = tf.concat([self.inputs, stacked_embeds], axis=2, name="inputs_with_embed")
self.embed_matrix_summ = tf.summary.histogram("embed_matrix", self.embed_matrix)
else:
self.inputs_with_embed = tf.identity(self.inputs)
self.embed_matrix_summ = None
print "inputs.shape:", self.inputs.shape
print "inputs_with_embed.shape:", self.inputs_with_embed.shape
# Run dynamic RNN
val, state_ = tf.nn.dynamic_rnn(cell, self.inputs_with_embed, dtype=tf.float32, scope="dynamic_rnn")
# Before transpose, val.get_shape() = (batch_size, num_steps, lstm_size)
# After transpose, val.get_shape() = (num_steps, batch_size, lstm_size)
val = tf.transpose(val, [1, 0, 2])
last = tf.gather(val, int(val.get_shape()[0]) - 1, name="lstm_state")
ws = tf.Variable(tf.truncated_normal([self.lstm_size, self.input_size]), name="w")
bias = tf.Variable(tf.constant(0.1, shape=[self.input_size]), name="b")
self.pred = tf.matmul(last, ws) + bias
self.last_sum = tf.summary.histogram("lstm_state", last)
self.w_sum = tf.summary.histogram("w", ws)
self.b_sum = tf.summary.histogram("b", bias)
self.pred_summ = tf.summary.histogram("pred", self.pred)
# self.loss = -tf.reduce_sum(targets * tf.log(tf.clip_by_value(prediction, 1e-10, 1.0)))
self.loss = tf.reduce_mean(tf.square(self.pred - self.targets), name="loss_mse_train")
self.optim = tf.train.RMSPropOptimizer(self.learning_rate).minimize(self.loss, name="rmsprop_optim")
# Separated from train loss.
self.loss_test = tf.reduce_mean(tf.square(self.pred - self.targets), name="loss_mse_test")
self.loss_sum = tf.summary.scalar("loss_mse_train", self.loss)
self.loss_test_sum = tf.summary.scalar("loss_mse_test", self.loss_test)
self.learning_rate_sum = tf.summary.scalar("learning_rate", self.learning_rate)
self.t_vars = tf.trainable_variables()
self.saver = tf.train.Saver()
def train(self, dataset_list, config):
"""
Args:
dataset_list (<StockDataSet>)
config (tf.app.flags.FLAGS)
"""
assert len(dataset_list) > 0
self.merged_sum = tf.summary.merge_all()
# Set up the logs folder
self.writer = tf.summary.FileWriter(os.path.join("./logs", self.model_name))
self.writer.add_graph(self.sess.graph)
if self.use_embed:
# Set up embedding visualization
# Format: tensorflow/tensorboard/plugins/projector/projector_config.proto
projector_config = projector.ProjectorConfig()
# You can add multiple embeddings. Here we add only one.
added_embed = projector_config.embeddings.add()
added_embed.tensor_name = self.embed_matrix.name
# Link this tensor to its metadata file (e.g. labels).
shutil.copyfile(os.path.join(self.logs_dir, "metadata.tsv"),
os.path.join(self.model_logs_dir, "metadata.tsv"))
added_embed.metadata_path = "metadata.tsv"
# The next line writes a projector_config.pbtxt in the LOG_DIR. TensorBoard will
# read this file during startup.
projector.visualize_embeddings(self.writer, projector_config)
tf.global_variables_initializer().run()
# Merged test data of different stocks.
merged_test_X = []
merged_test_y = []
merged_test_labels = []
for label_, d_ in enumerate(dataset_list):
merged_test_X += list(d_.test_X)
merged_test_y += list(d_.test_y)
merged_test_labels += [[label_]] * len(d_.test_X)
merged_test_X = np.array(merged_test_X)
merged_test_y = np.array(merged_test_y)
merged_test_labels = np.array(merged_test_labels)
print "len(merged_test_X) =", len(merged_test_X)
print "len(merged_test_y) =", len(merged_test_y)
print "len(merged_test_labels) =", len(merged_test_labels)
test_data_feed = {
self.learning_rate: 0.0,
self.keep_prob: 1.0,
self.inputs: merged_test_X,
self.targets: merged_test_y,
self.symbols: merged_test_labels,
}
global_step = 0
num_batches = sum(len(d_.train_X) for d_ in dataset_list) // config.batch_size
random.seed(time.time())
# Select samples for plotting.
sample_labels = range(min(config.sample_size, len(dataset_list)))
sample_indices = {}
for l in sample_labels:
sym = dataset_list[l].stock_sym
target_indices = np.array([
i for i, sym_label in enumerate(merged_test_labels)
if sym_label[0] == l])
sample_indices[sym] = target_indices
print sample_indices
print "Start training for stocks:", [d.stock_sym for d in dataset_list]
for epoch in xrange(config.max_epoch):
epoch_step = 0
learning_rate = config.init_learning_rate * (
config.learning_rate_decay ** max(float(epoch + 1 - config.init_epoch), 0.0)
)
for label_, d_ in enumerate(dataset_list):
for batch_X, batch_y in d_.generate_one_epoch(config.batch_size):
global_step += 1
epoch_step += 1
batch_labels = np.array([[label_]] * len(batch_X))
train_data_feed = {
self.learning_rate: learning_rate,
self.keep_prob: config.keep_prob,
self.inputs: batch_X,
self.targets: batch_y,
self.symbols: batch_labels,
}
train_loss, _, train_merged_sum = self.sess.run(
[self.loss, self.optim, self.merged_sum], train_data_feed)
self.writer.add_summary(train_merged_sum, global_step=global_step)
if np.mod(global_step, len(dataset_list) * 200 / config.input_size) == 1:
test_loss, test_pred = self.sess.run([self.loss_test, self.pred], test_data_feed)
print "Step:%d [Epoch:%d] [Learning rate: %.6f] train_loss:%.6f test_loss:%.6f" % (
global_step, epoch, learning_rate, train_loss, test_loss)
# Plot samples
for sample_sym, indices in sample_indices.iteritems():
image_path = os.path.join(self.model_plots_dir, "{}_epoch{:02d}_step{:04d}.png".format(
sample_sym, epoch, epoch_step))
sample_preds = test_pred[indices]
sample_truth = merged_test_y[indices]
self.plot_samples(sample_preds, sample_truth, image_path, stock_sym=sample_sym)
self.save(global_step)
final_pred, final_loss = self.sess.run([self.pred, self.loss], test_data_feed)
# Save the final model
self.save(global_step)
return final_pred
@property
def model_name(self):
name = "stock_rnn_lstm%d_step%d_input%d" % (
self.lstm_size, self.num_steps, self.input_size)
if self.embed_size > 0:
name += "_embed%d" % self.embed_size
return name
@property
def model_logs_dir(self):
model_logs_dir = os.path.join(self.logs_dir, self.model_name)
if not os.path.exists(model_logs_dir):
os.makedirs(model_logs_dir)
return model_logs_dir
@property
def model_plots_dir(self):
model_plots_dir = os.path.join(self.plots_dir, self.model_name)
if not os.path.exists(model_plots_dir):
os.makedirs(model_plots_dir)
return model_plots_dir
def save(self, step):
model_name = self.model_name + ".model"
self.saver.save(
self.sess,
os.path.join(self.model_logs_dir, model_name),
global_step=step
)
def load(self):
print(" [*] Reading checkpoints...")
ckpt = tf.train.get_checkpoint_state(self.model_logs_dir)
if ckpt and ckpt.model_checkpoint_path:
ckpt_name = os.path.basename(ckpt.model_checkpoint_path)
self.saver.restore(self.sess, os.path.join(self.model_logs_dir, ckpt_name))
counter = int(next(re.finditer("(\d+)(?!.*\d)", ckpt_name)).group(0))
print(" [*] Success to read {}".format(ckpt_name))
return True, counter
else:
print(" [*] Failed to find a checkpoint")
return False, 0
def plot_samples(self, preds, targets, figname, stock_sym=None, multiplier=5):
def _flatten(seq):
return np.array([x for y in seq for x in y])
truths = _flatten(targets)[-200:]
preds = (_flatten(preds) * multiplier)[-200:]
days = range(len(truths))[-200:]
plt.figure(figsize=(12, 6))
plt.plot(days, truths, label='truth')
plt.plot(days, preds, label='pred')
plt.legend(loc='upper left', frameon=False)
plt.xlabel("day")
plt.ylabel("normalized price")
plt.ylim((min(truths), max(truths)))
plt.grid(ls='--')
if stock_sym:
plt.title(stock_sym + " | Last %d days in test" % len(truths))
plt.savefig(figname, format='png', bbox_inches='tight', transparent=True)
plt.close()