原创

神经网络逼近股票收盘价

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://ailearning.blog.csdn.net/article/details/88529493
import tensorflow as tf    
import numpy as np    #数组矩阵的一个运算库
import matplotlib.pyplot as plt     #绘图模块
#numpy.linspace 函数用于创建一个一维数组,数组是一个等差数列构成的 
#三个参数:起始值,终止值,等步长样本数量
date = np.linspace(1,15,15)
endPrice=
np.array([2511.90,2538.68,2591.66,2732.98,2701.69,2701.29,2678.67,2726.50,2681.50,2739.17,2715.07,2823.53,2864.90,2019.68,2526.32])
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,2653.20])
print(date)
plt.figure()
for i in range(0,15):
    #柱状图
    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(1xx10)+b1(1x10) = B(15X10)
#B(15x10)*w2(10x1)+b2(15x1)  = c(15x1)
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 = tf.placeholder(tf.float32,[None,1])
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)
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})
    #w1 w2 b1 b2 A+wb->>layer2
    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()
文章最后发布于: 2019-03-13 11:50:53
展开阅读全文
0 个人打赏
私信求帮助

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览