Skip to content

Commit

Permalink
Added LSTM to data prediction
Browse files Browse the repository at this point in the history
  • Loading branch information
hunkim committed Feb 27, 2017
1 parent ddc7e95 commit 065d6f0
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 12 deletions.
17 changes: 9 additions & 8 deletions klab-12-3-rnn_prediction.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# http://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, LSTM
from keras.layers import Dense, LSTM, Activation
from sklearn.preprocessing import MinMaxScaler
import os

Expand All @@ -17,7 +17,7 @@

import matplotlib.pyplot as plt
# Open,High,Low,Close,Volume
xy = np.loadtxt('data-02-data-02-stock_daily.csv', delimiter=',')
xy = np.loadtxt('data-02-stock_daily.csv', delimiter=',')
xy = xy[::-1] # reverse order (chronically ordered)

# very important. It does not work without it.
Expand Down Expand Up @@ -45,8 +45,9 @@
dataY[train_size:len(dataY)])

model = Sequential()
model.add(LSTM(5, input_shape=(timesteps, data_dim), return_sequences=False))
model.add(Dense(1))
model.add(LSTM(1, input_shape=(seq_length, data_dim), return_sequences=False))
#model.add(Dense(1))
model.add(Activation("linear"))
model.compile(loss='mean_squared_error', optimizer='adam')

model.summary()
Expand All @@ -64,7 +65,7 @@
#testPredict = scaler.transform(testPredict)
#testY = scaler.transform(testY)

print(testPredict)
# plt.plot(testY)
# plt.plot(testPredict)
# plt.show()
#print(testPredict)
plt.plot(testY)
plt.plot(testPredict)
plt.show()
8 changes: 4 additions & 4 deletions klab-12-4-rnn_2_prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import matplotlib.pyplot as plt
# Open,High,Low,Close,Volume
xy = np.loadtxt('data-02-data-02-stock_daily.csv', delimiter=',')
xy = np.loadtxt('data-02-stock_daily.csv', delimiter=',')
xy = xy[::-1] # reverse order (chronically ordered)

# very important. It does not work without it.
Expand Down Expand Up @@ -70,6 +70,6 @@
#testY = scaler.transform(testY)

print(testPredict)
# plt.plot(testY)
# plt.plot(testPredict)
# plt.show()
plt.plot(testY)
plt.plot(testPredict)
plt.show()
72 changes: 72 additions & 0 deletions lab-12-4-rnn_stock_prediction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler

tf.set_random_seed(777) # reproducibility

timesteps = seq_length = 7
data_dim = 5
output_dim = 1

# Open,High,Low,Close,Volume
xy = np.loadtxt('data-02-stock_daily.csv', delimiter=',')
xy = xy[::-1] # reverse order (chronically ordered)

# very important. It does not work without it.
scaler = MinMaxScaler(feature_range=(0, 1))
xy = scaler.fit_transform(xy)

x = xy
y = xy[:, [-1]] # Close as label

dataX = []
dataY = []
for i in range(0, len(y) - seq_length):
_x = x[i:i + seq_length]
_y = y[i + 1] # Next close price
print(_x, "->", _y)
dataX.append(_x)
dataY.append(_y)

# split to train and testing
train_size = int(len(dataY) * 0.7)
test_size = len(dataY) - train_size
trainX, testX = np.array(dataX[0:train_size]), np.array(
dataX[train_size:len(dataX)])
trainY, testY = np.array(dataY[0:train_size]), np.array(
dataY[train_size:len(dataY)])

# input place holders
X = tf.placeholder(tf.float32, [None, seq_length, data_dim])
Y = tf.placeholder(tf.float32, [None, 1])

cell = tf.contrib.rnn.BasicLSTMCell(num_units=output_dim, state_is_tuple=True)
outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
Y_pred = outputs[:, -1] # We use the last cell's output

print(outputs[:,-1])

# loss
loss = tf.reduce_sum(tf.square(Y_pred - Y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)

# RMSE
targets = tf.placeholder(tf.float32, [None, 1])
predictions = tf.placeholder(tf.float32, [None, 1])
rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions)))

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for i in range(1000):
_, l = sess.run([train, loss], feed_dict={X: trainX, Y:trainY})
print(i, l)

testPredict = sess.run(Y_pred, feed_dict={X: testX})
print("RMSE", sess.run(rmse, feed_dict={targets: testY, predictions:testPredict}))
plt.plot(testY)
plt.plot(testPredict)
plt.show()
76 changes: 76 additions & 0 deletions lab-12-5-rnn_softmax_stock_prediction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler

tf.set_random_seed(777) # reproducibility

timesteps = seq_length = 7
data_dim = 5
output_dim = 3

# Open,High,Low,Close,Volume
xy = np.loadtxt('data-02-stock_daily.csv', delimiter=',')
xy = xy[::-1] # reverse order (chronically ordered)

# very important. It does not work without it.
scaler = MinMaxScaler(feature_range=(0, 1))
xy = scaler.fit_transform(xy)

x = xy
y = xy[:, [-1]] # Close as label

dataX = []
dataY = []
for i in range(0, len(y) - seq_length):
_x = x[i:i + seq_length]
_y = y[i + 1] # Next close price as target
print(_x, "->", _y)
dataX.append(_x)
dataY.append(_y)

# split to train and testing
train_size = int(len(dataY) * 0.7)
test_size = len(dataY) - train_size
trainX, testX = np.array(dataX[0:train_size]), np.array(
dataX[train_size:len(dataX)])
trainY, testY = np.array(dataY[0:train_size]), np.array(
dataY[train_size:len(dataY)])

# input place holders
X = tf.placeholder(tf.float32, [None, seq_length, data_dim])
Y = tf.placeholder(tf.float32, [None, 1])

cell = tf.contrib.rnn.BasicLSTMCell(num_units=output_dim, state_is_tuple=True)
cell = tf.contrib.rnn.MultiRNNCell([cell] * 2, state_is_tuple=True)
outputs, _states = tf.nn.dynamic_rnn(cell, X, dtype=tf.float32)
last_output = outputs[:, -1] # We use the last cell's output

# Softmax layer (rnn_hidden_size -> num_classes)
softmax_w = tf.get_variable("softmax_w", [output_dim, 1])
softmax_b = tf.get_variable("softmax_b", [1])
Y_pred = tf.matmul(last_output, softmax_w) + softmax_b

# loss
loss = tf.reduce_sum(tf.square(Y_pred - Y)) # sum of the squares
# optimizer
optimizer = tf.train.GradientDescentOptimizer(0.001)
train = optimizer.minimize(loss)

# RMSE
targets = tf.placeholder(tf.float32, [None, 1])
predictions = tf.placeholder(tf.float32, [None, 1])
rmse = tf.sqrt(tf.reduce_mean(tf.square(targets - predictions)))

sess = tf.Session()
sess.run(tf.global_variables_initializer())

for i in range(1000):
_, l = sess.run([train, loss], feed_dict={X: trainX, Y:trainY})
print(i, l)

testPredict = sess.run(Y_pred, feed_dict={X: testX})
print("RMSE", sess.run(rmse, feed_dict={targets: testY, predictions:testPredict}))
plt.plot(testY)
plt.plot(testPredict)
plt.show()
6 changes: 6 additions & 0 deletions tests/test_square.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,11 @@ def testBroadcast(self):
cost = tf.reduce_mean(tf.square(hypothesis - y))
self.assertAllEqual(cost.eval(), 0)

def testNormalize(self):
with self.test_session():
values = np.array([[10, 20], [1000, -100]], dtype=np.float32)
norm_values = tf.nn.l2_normalize(values, dim=1)
print(norm_values.eval())

if __name__ == '__main__':
tf.test.main()

0 comments on commit 065d6f0

Please sign in to comment.