-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRNN.py
90 lines (75 loc) · 2.95 KB
/
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
import numpy as np
import tensorflow as tf
from tensorflow.contrib.rnn import *
import matplotlib.pyplot as plt
num_neurons = 100
num_inputs = 1
num_outputs = 1
symbol = 'goog' # amzn
epochs = 500
seq_len = 20
learning_rate = 0.001
f = open(symbol + '.txt', 'r').read()
data = f.split('\n')[:-1] # get rid of the last '' so float(n) works
data.reverse()
d = [float(n) for n in data]
result = []
for i in range(len(d) - seq_len - 1):
result.append(d[i: i + seq_len + 1])
result = np.array(result)
row = int(round(0.9 * result.shape[0]))
train = result[:row, :]
test = result[row:, :]
np.random.shuffle(train)
X_train = train[:, :-1] # all rows with all columns except the last one
X_test = test[:, :-1] # each row contains seq_len + 1 columns
y_train = train[:, 1:]
y_test = test[:, 1:]
X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], num_inputs))
X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], num_inputs))
y_train = np.reshape(y_train, (y_train.shape[0], y_train.shape[1], num_outputs))
y_test = np.reshape(y_test, (y_test.shape[0], y_test.shape[1], num_outputs))
X = tf.placeholder(tf.float32, [None, seq_len, num_inputs])
y = tf.placeholder(tf.float32, [None, seq_len, num_outputs])
cell = tf.contrib.rnn.OutputProjectionWrapper(
tf.contrib.rnn.BasicRNNCell(num_units=num_neurons, activation=tf.nn.relu), output_size=num_outputs)
outputs, _ = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
preds = tf.reshape(outputs, [1, seq_len], name="preds")
loss = tf.reduce_mean(tf.square(outputs - y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
training_op = optimizer.minimize(loss)
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
init.run()
count = 0
for _ in range(epochs):
n=0
sess.run(training_op, feed_dict={X: X_train, y: y_train})
count += 1
if count % 10 == 0:
saver.save(sess, "/tmp/" + symbol + "_model.ckpt")
loss_val = loss.eval(feed_dict={X: X_train, y: y_train})
print(count, "loss:", loss_val)
correct = 0
y_pred = sess.run(outputs, feed_dict={X: X_test})
targets = []
predictions = []
for i in range(y_pred.shape[0]):
input = X_test[i]
target = y_test[i]
prediction = y_pred[i]
targets.append(target[-1][0])
predictions.append(prediction[-1][0])
if target[-1][0] >= input[-1][0] and prediction[-1][0] >= input[-1][0]:
correct += 1
elif target[-1][0] < input[-1][0] and prediction[-1][0] < input[-1][0]:
correct += 1
total = len(X_test)
xs = [i for i, _ in enumerate(y_test)]
plt.plot(xs, predictions, 'r-', label='prediction')
plt.plot(xs, targets, 'b-', label='true')
plt.legend(loc=0)
plt.title("%s - %d/%d=%.2f%%" %(symbol, correct, total,
100*float(correct)/total))
plt.show()