用神经网络预测股票价格,tensorflow代码实现

  1. # layer1:激励函数+乘加运算

  2. import tensorflow as tf

  3. import numpy as np

  4. import matplotlib.pyplot as plt

  5. date = np.linspace(1,15,15)

  6. 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]

  7. )

  8. 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])

  9. print(date)

  10. plt.figure()

  11. for i in range(0,15):

  12. # 1 柱状图

  13. dateOne = np.zeros([2])

  14. dateOne[0] = i;

  15. dateOne[1] = i;

  16. priceOne = np.zeros([2])

  17. priceOne[0] = beginPrice[i]

  18. priceOne[1] = endPrice[i]

  19. if endPrice[i]>beginPrice[i]:

  20. plt.plot(dateOne,priceOne,'r',lw=8)

  21. else:

  22. plt.plot(dateOne,priceOne,'g',lw=8)

  23. #plt.show()

  24. # A(15x1)*w1(1x10)+b1(1*10) = B(15x10)

  25. # B(15x10)*w2(10x1)+b2(15x1) = C(15x1)

  26. # 1 A B C

  27. dateNormal = np.zeros([15,1])

  28. priceNormal = np.zeros([15,1])

  29. #归一化

  30. for i in range(0,15):

  31. dateNormal[i,0] = i/14.0;

  32. priceNormal[i,0] = endPrice[i]/3000.0;

  33. x = tf.placeholder(tf.float32,[None,1])

  34. y = tf.placeholder(tf.float32,[None,1])

  35. # B

  36. w1 = tf.Variable(tf.random_uniform([1,10],0,1))

  37. b1 = tf.Variable(tf.zeros([1,10]))

  38. wb1 = tf.matmul(x,w1)+b1

  39. layer1 = tf.nn.relu(wb1) # 激励函数

  40. # C

  41. w2 = tf.Variable(tf.random_uniform([10,1],0,1))

  42. b2 = tf.Variable(tf.zeros([15,1]))

  43. wb2 = tf.matmul(layer1,w2)+b2

  44. layer2 = tf.nn.relu(wb2)

  45. loss = tf.reduce_mean(tf.square(y-layer2))#y 真实 layer2 计算

  46. #梯度下级法

  47. train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

  48. with tf.Session() as sess:

  49. sess.run(tf.global_variables_initializer())

  50. for i in range(0,10000):

  51. sess.run(train_step,feed_dict={x:dateNormal,y:priceNormal})

  52. #预测下

  53. # w1w2 b1b2 A + wb -->layer2

  54. pred = sess.run(layer2,feed_dict={x:dateNormal})

  55. predPrice = np.zeros([15,1])

  56. for i in range(0,15):

  57. predPrice[i,0]=(pred*3000)[i,0]

  58. plt.plot(date,predPrice,'b',lw=1)

  59. plt.show()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值