【机器学习入门笔记15:BP神经网络逼近股票收盘价格2】20190218
生活随笔
收集整理的這篇文章主要介紹了
【机器学习入门笔记15:BP神经网络逼近股票收盘价格2】20190218
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019-02-18? by 崔斐然??
import os import tensorflow as tf import numpy as np import matplotlib.pyplot as pltos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'# linespace 線性增長的(從1,到15,共15個數據) 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])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;priceNormal[i,0] = endPrice[i]/3000.0;x = tf.placeholder(tf.float32,[None,1]) y = tf.placeholder(tf.float32,[None,1])# B # 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) # 激勵函數 # 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) # 真實值與 layer2 計算值得差異 計算均值。(開方再均值,其實就是標準差公式而已)) loss = tf.reduce_mean(tf.square(y-layer2)) #train_step 表示每次調整的步長。使用的tf的梯度下降法GradientDescentOptimizer,被縮小參數為(loss) train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) with tf.Session() as sess:sess.run(tf.global_variables_initializer())# 訓練終止條件:方法1:for循環的次數 方法2、真實值與與測試的差值在允許范圍內# 這里采用訓練十萬次的方法。for i in range(1,100000):sess.run(train_step,feed_dict={x:dateNormal,y:priceNormal})# w1w2 b1b2 A + wb -->layer2pred = 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()?
總結
以上是生活随笔為你收集整理的【机器学习入门笔记15:BP神经网络逼近股票收盘价格2】20190218的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【机器学习入门笔记14:BP神经网络基础
- 下一篇: Debian 9/10快速开启Googl