OpenCV+Tensorflow 人工智能图像处理(十一)—— 卷积神经网络预测股票走势

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, 2710.69, 2701.29, 2670.67, 2726.50, 2681.58, 2739.17, 2715.07, 2023.50, 2864.90, 2819.88])
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.48, 2832.73, 2877.48])
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()
dateNormal = np.zeros([15, 1])
priceNormal = np.zeros([15, 1])
for i in range(0, 15):            #日期和价格归一化
    dateNormal[1, 0] = i / 14.0
    priceNormal[1, 0] = endPrice[i] / 3000.0

x = tf.placeholder(tf.float32, [None, 1])  #定义输入层
y = tf.placeholder(tf.float32, [None, 1])  #定义输出层

#隐藏层
w1 = tf.Variable(tf.random_uniform([1, 10], 0,1)) #w1变量10行1列,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)) #w1变量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.01).minimize(loss) #每次下降0.1来最小化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})
    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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值