Jaf*_*son 8 python python-3.x pandas tensorflow jupyter-notebook
我已经创建了tensorflow程序,以便为外汇收盘价.我已成功创建了预测但未能理解预测未来值的方法.看以下是我的预测功能:
test_pred_list = []
def testAndforecast(xTest1,yTest1):
# test_pred_list = 0
truncated_backprop_length = 3
with tf.Session() as sess:
# train_writer = tf.summary.FileWriter('logs', sess.graph)
tf.global_variables_initializer().run()
counter = 0
# saver.restore(sess, "models\\model2298.ckpt")
try:
with open ("Checkpointcounter.txt","r") as file:
value = file.read()
except FileNotFoundError:
print("First Time Running Training!....")
if(tf.train.checkpoint_exists("models\\model"+value+".ckpt")):
saver.restore(sess, "models\\model"+value+".ckpt")
print("models\\model"+value+".ckpt Session Loaded for Testing")
for test_idx in range(len(xTest1) - truncated_backprop_length):
testBatchX = xTest1[test_idx:test_idx+truncated_backprop_length,:].reshape((1,truncated_backprop_length,num_features))
testBatchY = yTest1[test_idx:test_idx+truncated_backprop_length].reshape((1,truncated_backprop_length,1))
#_current_state = np.zeros((batch_size,state_size))
feed = {batchX_placeholder : testBatchX,
batchY_placeholder : testBatchY}
#Test_pred contains 'window_size' predictions, we want the last one
_last_state,_last_label,test_pred = sess.run([last_state,last_label,prediction],feed_dict=feed)
test_pred_list.append(test_pred[-1][-1]) #The last one
Run Code Online (Sandbox Code Playgroud)
以下是测试和训练的完整jupyter和数据集:
我的存储库和代码.
请帮助我如何预测未来的接近值.请不要像我试过的那样分享与预测相关的内容.请允许,让我知道在没有任何支持的情况下仅根据我所提供的培训进行预测的事情.
我希望很快能听到.
如果我正确理解你的问题,那么预测你的意思是预测未来的多个收盘价(例如当前状态的下 5 个收盘价)。我浏览了你的 jupyter 笔记本。简而言之,你不能轻易做到这一点。
现在,您的代码采用多个期货定义的最后三个头寸(开盘价/最低价/最高价/收盘价和一些指标值)。在此基础上,您预测下一个收盘价。如果您想预测进一步的仓位,您必须根据预测的收盘价创建一个“人工”仓位。在这里您可以估计开盘价与前一天收盘价相同,但您只能猜测最高价和最低价。然后,您将计算其他期货/价值(根据指标),并将该头寸与前两个头寸一起使用来预测下一个收盘价。您可以像这样继续执行后续步骤。
问题在于开盘价/最低价/最高价,因为您只能估算它们。您可以从数据中删除它们,重新训练模型,并在没有它们的情况下进行预测,但它们可能是指标计算所必需的。
我以某种方式压缩了您的代码,以展示预测所有 OHLC 价格的方法:
# Data
xTrain = datasetTrain[
["open", "high", "low", "close", "k",
"d", "atr", "macdmain", "macdsgnal",
"bbup", "bbmid", "bblow"]].as_matrix()
yTrain = datasetTrain[["open", "high", "low", "close"]].as_matrix()
# Settings
batch_size = 1
num_batches = 1000
truncated_backprop_length = 3
state_size = 12
num_features = 12
num_classes = 4
# Graph
batchX_placeholder = tf.placeholder(
dtype=tf.float32,
shape=[None, truncated_backprop_length, num_features],
name='data_ph')
batchY_placeholder = tf.placeholder(
dtype=tf.float32,
shape=[None, num_classes],
name='target_ph')
cell = tf.contrib.rnn.BasicRNNCell(num_units=state_size)
states_series, current_state = tf.nn.dynamic_rnn(
cell=cell,
inputs=batchX_placeholder,
dtype=tf.float32)
states_series = tf.transpose(states_series, [1,0,2])
last_state = tf.gather(
params=states_series,
indices=states_series.get_shape()[0]-1)
weight = tf.Variable(tf.truncated_normal([state_size, num_classes]))
bias = tf.Variable(tf.constant(0.1, shape=[num_classes]))
prediction = tf.matmul(last_state, weight) + bias
loss = tf.reduce_mean(tf.squared_difference(last_label, prediction))
train_step = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
# Training
for batch_idx in range(num_batches):
start_idx = batch_idx
end_idx = start_idx + truncated_backprop_length
batchX = xTrain[start_idx:end_idx,:].reshape(batch_size, truncated_backprop_length, num_features)
batchY = yTrain[end_idx].reshape(batch_size, truncated_backprop_length, num_classes)
feed = {batchX_placeholder: batchX, batchY_placeholder: batchY}
_loss, _train_step, _pred, _last_label,_prediction = sess.run(
fetches=[loss, train_step, prediction, last_label, prediction],
feed_dict=feed)
Run Code Online (Sandbox Code Playgroud)
我认为编写整个代码并不重要,而且我不知道指标是如何计算的。此外,您还应该更改数据馈送的方式,因为现在它仅适用于操作系统大小为 1 的批次。
归档时间: |
|
查看次数: |
628 次 |
最近记录: |