forked from hunkim/DeepLearningZeroToAll
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
167 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters