深度学习入门(3)----用长短时记忆网络进行汇率预测

在前面两篇博文中分别介绍了深度学习入门(1)----用卷积神经网络进行图像识别(一)深度学习入门(1)----用卷积神经网络进行图像识别(二),在本文中将介绍长短时记忆网络的应用。
卷积神经网络是一种前馈神经网络,其特点是信号在其中始终向前传播,每一次神经网络的输入数据与以前输入的数据也没有任何关系,也就是神经网络的计算结果与不同批次输入数据的前后时间顺序没有关系。但是在实际的应用过程中,很多问题与时间顺序相关,此时,就需要用到循环神经网络(Recurrent Neural Network)进行处理。
百度百科对循环神经网络的介绍为:
在这里插入图片描述
本文将以循环神经网络中的长短时记忆网络为例,对货币汇率进行预测。长短时记忆网络的结构图为:
LSTM网络结构图

应用背景说明:

本文的应用为货币汇率的预测,是通过前几天的货币汇率情况,预测未来一天的货币汇率值。

数据说明:

本例中的数据均为虚构的数据,用纯文本文件保存,数据下载为:链接:exchangeData.txt
提取码:iyxm
数据中每一行为一天汇率值,排列顺序按照越往后日期也越来新。

算法说明:

在本例中以前三天的汇率值来预测第四天的汇率值,所以需要3个输入值,分别为X1,X2,X3,本例中的神经网络模型如下所示:
本例中LSTM神经网络模型

代码如下:

import tensorflow as tf
import numpy as np
import pandas as pd
import sys

roundT = 100
learnRateT = 0.001

argt = sys.argv[1:]
print("argt: %s" % argt)

for v in argt:
    if v.startswith("-round="):
        roundT = int(v[len("-round="):])
    if v.startswith("-learnrate="):
        learnRateT = float(v[len("-learnrate="):])

fileData = pd.read_csv('exchangeData.txt', dtype=np.float32, header=None)
wholeData = np.reshape(fileData.as_matrix(), (-1))

print("wholeData: %s" % wholeData)

cellCount = 3
unitCount = 5

testData = wholeData[-cellCount:]
print("testData: %s\n" % testData)

rowCount = wholeData.shape[0] - cellCount
print("rowCount: %d\n" % rowCount)

xData = [wholeData[i:i + cellCount] for i in range(rowCount)]
yTrainData = [wholeData[i + cellCount] for i in range(rowCount)]

print("xData: %s\n" % xData)
print("yTrainData: %s\n" % yTrainData)

x = tf.placeholder(shape=[cellCount], dtype=tf.float32)
yTrain = tf.placeholder(dtype=tf.float32)

cellT = tf.nn.rnn_cell.BasicLSTMCell(unitCount)

initState = cellT.zero_state(1, dtype=tf.float32)

h, finalState = tf.nn.dynamic_rnn(cellT, tf.reshape(x, [1, cellCount, 1]), initial_state=initState, dtype=tf.float32)
hr = tf.reshape(h, [cellCount, unitCount])

w2 = tf.Variable(tf.random_normal([unitCount, 1]), dtype=tf.float32)
b2 = tf.Variable(0.0, dtype=tf.float32)

y = tf.reduce_sum(tf.matmul(hr, w2) + b2)

loss = tf.abs(y - yTrain)

optimizer = tf.train.RMSPropOptimizer(learnRateT)
train = optimizer.minimize(loss)

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

for i in range(roundT):
    lossSum = 0.0
    for j in range(rowCount):
        result = sess.run([train, x, yTrain, y, h, finalState, loss], feed_dict={x: xData[j], yTrain: yTrainData[j]})
        lossSum = lossSum + float(result[len(result) - 1])
        if j == (rowCount - 1):
            print("i: %d, x: %s, yTrain: %s, y: %s, h: %s, finalState: %s, loss: %s, avgLoss: %10.10f\n" % (i, result[1], result[2], result[3], result[4], result[5], result[6], (lossSum / rowCount)))

result = sess.run([x, y], feed_dict={x: testData})
print("x: %s, y: %s\n" % (result[0], result[1]))

运行方式:

进入cmd,用如下命令对本程序进行执行:

python lstm1.py -round=1000

通过1000次训练,得到的运行结果为:

在这里插入图片描述

训练结果说明:

其中最后一行输出的:x: [6.3188 6.3198 6.3198], y: 6.338545,表示通过x: [6.3188 6.3198 6.3198]前三天的汇率值,预测第四天的汇率值为y: 6.338545。

发布了13 篇原创文章 · 获赞 2 · 访问量 2635

猜你喜欢

转载自blog.csdn.net/weixin_43981621/article/details/104216242