gpt4 book ai didi

python - 如何使用 Tensorflow 模型进行预测?

转载 作者:太空狗 更新时间:2023-10-29 21:09:16 24 4
gpt4 key购买 nike

我已经创建了 tensorflow 程序来获取外汇的收盘价。我已经成功地创建了预测,但未能理解预测 future 值(value)的方法。看下面是我的预测函数:

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

这是用于测试和训练的完整 jupyter 和数据集:
My repository with code .

请帮助我如何预测 future 的收盘价。请不要像我尝试的那样分享与预测相关的内容。请让我知道一些可以在没有任何支持的情况下仅根据我所提供的培训进行预测的东西。

希望早日听到。

最佳答案

如果我对您的问题的理解正确,那么预测是指预测 future 的多个收盘价(例如当前状态下的下 5 个收盘价)。我浏览了你的 jupyter notebook。简而言之,您不能轻易做到这一点。

现在您的代码采用多个 future 定义的最后三个位置(开盘价/最低价/最高价/收盘价和一些指标值)。在此基础上,您可以预测下一个收盘价。如果您想预测更多头寸,则必须根据预测的收盘价创建一个“人工”头寸。在这里您可以近似开盘价与前收盘价相同,但您只能猜测最高价和最低价。然后您将计算其他 future /值(value)(来自指标),并使用该头寸与前两个头寸来预测下一个收盘价。您可以在以后的步骤中继续这样做。

问题在于开盘价/最低价/最高价,因为您只能估计它们。您可以将它们从数据中移除,重新训练模型,并在没有它们的情况下进行预测,但它们可能是指标计算所必需的。


我以某种方式压缩了您的代码以显示预测所有 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)

我认为编写整个代码并不重要,而且我不知道指标是如何计算的。此外,您应该更改数据馈送方式,因为现在它仅适用于大小为 1 的批处理操作系统。

关于python - 如何使用 Tensorflow 模型进行预测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51444526/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com