TensorFlow - 神经网络逼近股票价格

流程

首先我们模拟出一个15天的股票数据,包括收盘价格和开盘价格。首先我们将数据进行归一化操作。

dateNormal = np.zeros([15,1])
priceNormal = np.zeros([15,1])
for i in range(0,15):
    dateNormal[i,0] = i/14.0
    priceNormal[i,0] = endPrice[i]/3000.0

处理好数据后我们开始定义输入层:
输入层采用placeholder占位。

x = tf.placeholder(tf.float32,[None,1])#N行一列
y = tf.placeholder(tf.float32,[None,1])

隐藏层我们定义一个系数矩阵w1和偏置矩阵b1,使输入层A和隐层B之间满足B = A*w1+b1的关系。

w1 = tf.Variable(tf.random_uniform([1,10],0,1))
b1 = tf.Variable(tf.zeros([1,10]))
wb1 = tf.matmul(x,w1)+b1
layer1 = tf.nn.relu(wb1) #激励函数

在输出层同样定义:

w2 = tf.Variable(tf.random_uniform([10,1],0,1))
b2 = tf.Variable(tf.zeros([15,1]))
wb2 = tf.matmul(layer1,w2)+b2
layer2 = tf.nn.relu(wb2) #激励函数

然后我们定义好损失和步长:
这里采用梯度下降法。

loss = tf.reduce_mean(tf.square(y-layer2))#计算标准差 对差值开方求均值
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)#每次减小loss0.1

最后我们将模型训练一万次后查看结果:
在这里插入图片描述

整体代码

"""
输入矩阵:A; 隐层矩阵:B;输出矩阵:C
A * w1 + b1 = B  一二层转换关系  w1 1x10  w2 10x1
B * w2 + b2 = C  二三层转换关系  b1 1x10  b2 15x1

1此循环 A 一天w1w2b1b1(0.1 0.1 0.2 0.3)-->C   2400 2511 = 111点
2次循环(梯度下降法)->111减小 w1w2b1b2(0.11 0.09 0.22 0.34)->2450 2511 = 61 证明有效继续循环...
终止 1 for count 2 差异 2% -->w1w2b1b2
"""
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

date = np.linspace(1,15,15) #建立15个横
endPrice = np.array([2511.90,2538.26,2510.68,2591.66,2732.98,2701.69,2701.29,2678.67,2726.50,2681.50,2739.17,2715.07,2823.58,2864.90,2919.08]) #定义收盘价格
beginPrice = np.array([2438.71,2500.88,2534.95,2512.52,2594.04,2743.26,2697.47,2695.24,2678.23,2722.13,2674.93,2744.13,2717.46,2832.73,2877.40]) #定义开盘价格

plt.figure()
for i in range(0,15): 
    #柱状图
    dateOne = np.zeros([2]) #显示日期1-15天
    dateOne[0] = i
    dateOne[1] = i
    priceOne = np.zeros([2]) #填充价格
    priceOne[0] = beginPrice[i]
    priceOne[1] = endPrice[i]
    if endPrice[i]>beginPrice[i]: #涨 标红
        plt.plot(dateOne,priceOne,'r',lw=8)
    else: #跌 标绿
        plt.plot(dateOne,priceOne,'g',lw=8)

dateNormal = np.zeros([15,1])
priceNormal = np.zeros([15,1])
for i in range(0,15):
    dateNormal[i,0] = i/14.0
    priceNormal[i,0] = endPrice[i]/3000.0
#输入x y
x = tf.placeholder(tf.float32,[None,1])#N行一列
y = tf.placeholder(tf.float32,[None,1])
#B 隐藏层
w1 = tf.Variable(tf.random_uniform([1,10],0,1))
b1 = tf.Variable(tf.zeros([1,10]))
wb1 = tf.matmul(x,w1)+b1
layer1 = tf.nn.relu(wb1) #激励函数
#C 输出层
w2 = tf.Variable(tf.random_uniform([10,1],0,1))
b2 = tf.Variable(tf.zeros([15,1]))
wb2 = tf.matmul(layer1,w2)+b2
layer2 = tf.nn.relu(wb2) #激励函数
loss = tf.reduce_mean(tf.square(y-layer2))#计算标准差 对差值开方求均值
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)#每次减小loss0.1

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(0,10000):#训练一万次
        sess.run(train_step,feed_dict={x:dateNormal, y:priceNormal})
    pred = sess.run(layer2,feed_dict={x:dateNormal})
    predPrice = np.zeros([15,1])
    for i in range(0,15):
        predPrice[i,0] = (pred*3000)[i,0]
    plt.plot(date,predPrice,'b',lw=1)
plt.show()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值