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

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章