【发布时间】:2018-10-16 02:08:15
【问题描述】:
我正在学习使用 RNN 来预测市场指数,例如标准普尔 500 指数(注意;这是标准普尔指数,而不是 500 家不同的公司)。除了价格变化数据,我还提供其他信息,例如 RSI、MACD、EMA
然后我有 3 个标签,分别是 1w、2w 和 3w,我从单独的 CSV 加载。
说这是我的示例数据(完全虚构):
price change RSI MACD EMA
0.3 3.2 0.1 0.0
-0.1 3.1 0.1 0.0
-1.2 3.8 0.1 0.2
0.9 2.7 0.1 0.2
1.3 1.7 0.2 0.2
然后我有一个单独的 CSV 用于标签
1w future price change % 2w future price change % 3w future price change %
1.2 1.8 -0.3
0.8 0.2 1.1
0.2 1.5 0.7
1.2 1.7 0.1
-0.2 1.8 -0.3
我的麻烦是我只能找到使用单一特征和/或使用未来训练数据作为标签的示例,而我使用单独定义的一组数据作为标签。
我已经拼凑了下面的代码,但我在这一行收到了形状馈送错误:
mse = loss.eval(feed_dict={X: trX, Y: trY})
我怀疑我的数据格式有误,因为它仍然是我用来训练“正常”前馈网络的格式。我怀疑需要进行一些重新塑造,但老实说,由于具有多种功能,我不知道什么格式。我也可能错误地定义了模型(?)。
如果有人能帮我解决这个问题,我将不胜感激。
我还有一个额外的问题:以前(正如您将在代码中看到的那样)我会对数据进行洗牌,这对于小批量前馈 NN 来说是很好的,但是如何与 RNN 一起工作,其中我假设您需要按顺序显示数据?接下来,假设我将其用于股票(而不是市场指数);我是否需要逐个股票地提供数据以构成移动窗口,而不是每天都这样做?显然,在日常基础上,每一行数据都针对不同的股票。
抱歉所有问题,我仍然对 RNN 感兴趣!
import tensorflow as tf
import numpy as np
import pandas as pd
import datetime
from sklearn.model_selection import train_test_split
# hyperparameters
epochs = 600
batch_size = 128
num_hidden = 100
df = pd.read_csv('C:\\python\\MarketData-Inputs.csv',header=None)
ldf = pd.read_csv('C:\\python\\MarketData-Results.csv',header=None)
# 20% test, shuffle the data, and use random state for like-like comparison between runs
trX, teX, trY, teY = train_test_split(df, ldf, test_size=0.2, shuffle=True, random_state=42)
trX = trX.values.astype('float')
trY = trY.values.astype('float')
teX = teX.values.astype('float')
teY = teY.values.astype('float')
print(trX.shape)
print(trY.shape)
print(teX.shape)
print(teY.shape)
#data params
features_size = len(trX[0])
labels_size = len(trY[0])
step_size = 3
tf.reset_default_graph()
X = tf.placeholder("float", [None, step_size, features_size], name="X")
Y = tf.placeholder("float", [None, labels_size], name="Y")
basic_cell = tf.contrib.rnn.BasicRNNCell(num_units=num_hidden, activation=tf.nn.relu)
rnn_outputs, _ = tf.nn.dynamic_rnn(basic_cell, X, dtype=tf.float32)
stacked_rnn_outputs = tf.reshape(rnn_outputs, [-1, num_hidden])
stacked_outputs = tf.layers.dense(stacked_rnn_outputs, labels_size)
outputs = tf.reshape(stacked_outputs, [-1, step_size, labels_size])
with tf.name_scope("loss"):
loss = tf.reduce_sum(tf.square(outputs - Y))
training_op = tf.train.AdamOptimizer().minimize(loss)
tf.summary.scalar("loss", loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
init.run()
for ep in range(epochs):
sess.run(training_op, feed_dict={X: trX, Y: trY})
if ep % 100 == 0:
mse = loss.eval(feed_dict={X: trX, Y: trY})
print(ep, "\tMSE:", mse)
y_pred = sess.run(stacked_outputs, feed_dict={X: teX})
print(y_pred)
【问题讨论】: