數據有15天股票的開盤價格和收盤價格,能夠經過比較當天開盤價格和收盤價格的大小來判斷當天股票價格的漲跌狀況,紅色表示漲,綠色表示跌,測試代碼以下:網絡
1 # encoding:utf-8 2 3 import tensorflow as tf 4 import numpy as np 5 import matplotlib.pyplot as plt 6 date = np.linspace(1, 15, 15) 7 # 當天的收盤價格 8 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] 9 ) 10 # 當天的開盤價格 11 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]) 12 print(date) # 打印日期 13 plt.figure() 14 for i in range(0,15): 15 # 經過循環遍歷數據畫出柱狀圖 16 dateOne = np.zeros([2]) 17 dateOne[0] = i 18 dateOne[1] = i 19 print(dateOne) 20 priceOne = np.zeros([2]) 21 priceOne[0] = beginPrice[i] 22 priceOne[1] = endPrice[i] 23 if endPrice[i] > beginPrice[i]: 24 # 若是收盤價格大於開盤價格說明股票上漲 用紅色表示 lw爲線條粗細 25 plt.plot(dateOne, priceOne,'r',lw=8) 26 else: 27 # 若是收盤價格小於開盤價格說明股票下跌 用綠色表示 lw爲線條粗細 28 plt.plot(dateOne, priceOne,'g',lw=5) 29 plt.show()
運行後的圖以下:dom
創建一個簡單的三層人工神經網絡。函數
循環的終止條件能夠爲預先設定的循環次數或者與真實值的差別百分比測試
功能實現,完整的測試代碼以下:spa
1 # encoding:utf-8 2 3 import tensorflow as tf 4 import numpy as np 5 import matplotlib.pyplot as plt 6 date = np.linspace(1, 15, 15) 7 # 當天的收盤價格 8 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] 9 ) 10 # 當天的開盤價格 11 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]) 12 print(date) # 打印日期 13 plt.figure() 14 for i in range(0,15): 15 # 經過循環遍歷數據畫出柱狀圖 16 dateOne = np.zeros([2]) 17 dateOne[0] = i 18 dateOne[1] = i 19 print(dateOne) 20 priceOne = np.zeros([2]) 21 priceOne[0] = beginPrice[i] 22 priceOne[1] = endPrice[i] 23 if endPrice[i] > beginPrice[i]: 24 # 若是收盤價格大於開盤價格說明股票上漲 用紅色表示 lw爲線條粗細 25 plt.plot(dateOne, priceOne,'r',lw=8) 26 else: 27 # 若是收盤價格小於開盤價格說明股票下跌 用綠色表示 lw爲線條粗細 28 plt.plot(dateOne, priceOne,'g',lw=5) 29 # plt.show() 30 # A(15x1)*w1(1x10)+b1(1*10) = B(15x10) 31 # B(15x10)*w2(10x1)+b2(15x1) = C(15x1) 32 # 1 A B C 33 dateNormal = np.zeros([15,1]) 34 priceNormal = np.zeros([15,1]) 35 # 日期和價格進行歸一化處理 36 for i in range(0, 15): 37 dateNormal[i, 0] = i/14.0 38 priceNormal[i, 0] = endPrice[i]/3000.0 39 print(dateNormal) 40 print(priceNormal) 41 42 x = tf.placeholder(tf.float32, [None, 1]) # 代表是N行1列的 43 y = tf.placeholder(tf.float32, [None, 1]) # 代表是N行1列的 44 45 # B 46 w1 = tf.Variable(tf.random_uniform([1, 10], 0, 1)) # 可變值 能夠經過偏差修改值 範圍0-1 47 b1 = tf.Variable(tf.zeros([1, 10])) # 可變值 能夠經過偏差修改值 48 wb1 = tf.matmul(x, w1)+b1 49 layer1 = tf.nn.relu(wb1) # 激勵函數 映射成另外一個值 50 # 第一二層完畢 51 52 # C 53 w2 = tf.Variable(tf.random_uniform([10, 1], 0, 1)) # 可變值 能夠經過偏差修改值 範圍0-1 54 b2 = tf.Variable(tf.zeros([15, 1])) 55 wb2 = tf.matmul(layer1, w2)+b2 56 layer2 = tf.nn.relu(wb2) # 激勵函數 映射成另外一個值 57 # 第二三層完畢 58 59 # 偏差用loss表示 實際是一個標準差 60 loss = tf.reduce_mean(tf.square(y-layer2)) # y 真實 layer2 計算 61 # 每次調整的步長 梯度降低0.1 目的是縮小loss減少真實值與偏差值的差別 62 train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss) 63 with tf.Session() as sess: 64 sess.run(tf.global_variables_initializer()) # 初始化 65 for i in range(0, 10000): # 訓練次數爲10000 66 sess.run(train_step, feed_dict={x: dateNormal, y: priceNormal}) 67 # w1w2 b1b2 A + wb -->layer2 68 pred = sess.run(layer2, feed_dict={x: dateNormal}) 69 predPrice = np.zeros([15, 1]) # 預測結果 70 for i in range(0, 15): # 還原數據須要*3000 71 predPrice[i, 0] = (pred*3000)[i, 0] 72 plt.plot(date, predPrice, 'b', lw=1) 73 plt.show()
運行結果以下:(圖中藍色的線表示股票的預測值)code