tensorflow基本操作《06神经网络逼近股票收盘均价》补充

tensorflow基本操作《06神经网络逼近股票收盘均价》

代码

layer1:激励函数+乘加运算

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
date = np.linspace(1,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])
print('天数为:\n',date)
print('收盘价格为:\n',endPrice)
print('开盘价格为:\n',beginPrice)
plt.figure()
for i in range(0,15):
    # 1 柱状图
    dateOne = np.zeros([2])
    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)
#plt.show()
# A(15x1)*w1(1x10)+b1(1*10) = B(15x10)
# B(15x10)*w2(10x1)+b2(15x1) = C(15x1)
# 1输入层 A  中间层B  输出C 
#方便计算进行归一化处理
dateNormal = np.zeros([15,1]) #日期
priceNormal = np.zeros([15,1]) #价格
# 输入数据进行归一化处理(除法)
for i in range(0,15):
    dateNormal[i,0] = i/14.0; #14.0浮点数 注意下标
    priceNormal[i,0] = endPrice[i]/3000.0;  #最大值不会超过3000
x = tf.placeholder(tf.float32,[None,1])  #  f.float32 制定当前数据类型 [None,1] N 行一列
y = tf.placeholder(tf.float32,[None,1])
# 定义隐藏层B
# 由于梯度下降算法,要对权重w与偏移b进行修改,所以必须是变量Variable
w1 = tf.Variable(tf.random_uniform([1,10],0,1)) # random_uniform随机值初始化 权重随机
b1 = tf.Variable(tf.zeros([1,10])) # 偏移初始为0
wb1 = tf.matmul(x,w1)+b1 # 公式运算 A(15x1)*w1(1x10)+b1(1*10) = B(15x10)
layer1 = tf.nn.relu(wb1) # 激励函数,将结果进行映射,变为另外一种结果
# 定义输出层C
w2 = tf.Variable(tf.random_uniform([10,1],0,1)) # 10行 1列
b2 = tf.Variable(tf.zeros([15,1]))  # 15 行 1列
wb2 = tf.matmul(layer1,w2)+b2
layer2 = tf.nn.relu(wb2) # #激励函数,将结果进行映射,变为另外一种结果
# 计算预测值与实际值之间的差异
loss = tf.reduce_mean(tf.square(y-layer2))#y 真实值 layer2 计算的预测值 reduce_mean均值 square开放 标准差
# 使用梯度下降法GradientDescentOptimizer
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) # 参数1(0.1)每层调整0.1  目的减小loss minimize(loss)
# 运行程序
with tf.Session() as sess:
    # 完成变量初始化
    sess.run(tf.global_variables_initializer())
    # 开始训练 终止通过循环次数控制10000

    for i in range(0,10000):
        # 输入已经归一化的dateNormal   priceNormal
        sess.run(train_step,feed_dict={x:dateNormal,y:priceNormal})
    # 利用训练好的 w 和 b 给出预测结果 w1w2 b1b2  A + wb -->layer2
    pred = sess.run(layer2,feed_dict={x:dateNormal})
    # 定义predPrice
    predPrice = np.zeros([15,1]) # 初始化
    # 对15天数据完成价格计算预测,
    for i in range(0,15):
        predPrice[i,0]=(pred*3000)[i,0]  # (pred*3000)[i,0]转化为i 行n列数据
    #完成预测数据打印并绘制
    print('预测价格:',predPrice)
    plt.plot(date,predPrice,'b',lw=1)
plt.show()

运行结果

天数为:
 [ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11. 12. 13. 14. 15.]
收盘价格为:
 [2511.9  2538.26 2510.68 2591.66 2732.98 2701.69 2701.29 2678.67 2726.5
 2681.5  2739.17 2715.07 2823.58 2864.9  2919.08]
开盘价格为:
 [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.4 ]
预测价格: [[2511.8972168 ]
 [2538.26293945]
 [2510.68115234]
 [2591.66113281]
 [2732.97851562]
 [2701.6887207 ]
 [2701.28979492]
 [2678.67016602]
 [2726.5       ]
 [2681.50024414]
 [2739.1706543 ]
 [2715.0715332 ]
 [2823.57958984]
 [2864.89916992]
 [2919.07910156]]

在这里插入图片描述

发布了83 篇原创文章 · 获赞 25 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/Jamesaonier/article/details/105113933
今日推荐