# layer1:激励函数+乘加运算
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
date = np.linspace(1,27,27)
endPrice = np.array([2925.72,2909.38,2910.74,2881.97,2887.62,2890.16,2917.80,2987.12,3001.98,3008.15,2982.07,2976.28,2996.79,2978.88,3044.90,3043.94,3015.26,3005.25,3011.06,2933.26,2928.23,2915.30,2917.76,2930.55,2942.19,2937.62,2931.69]
)
beginPrice = np.array([2915.72,2919.38,2920.74,2882.97,2877.62,2880.16,2918.80,2988.12,3000.98,3009.15,2989.07,2975.28,2997.79,2979.88,3045.90,3046.94,3017.26,3008.25,3019.06,2935.26,2927.23,2918.30,2919.76,2939.55,2949.19,2938.62,2935.69])
print(date)
plt.figure()
for i in range(0,27):
# 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([27,1])
priceNormal = np.zeros([27,1])
for i in range(0,27):
dateNormal[i,0] = i/26.0;
priceNormal[i,0] = endPrice[i]/3150.0;
x = tf.placeholder(tf.float32,[None,1])
y = tf.placeholder(tf.float32,[None,1])
# B
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([27,1]))
wb2 = tf.matmul(layer1,w2)+b2
layer2 = tf.nn.relu(wb2)
loss = tf.reduce_mean(tf.square(y-layer2))#y 真实 layer2 计算
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(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})
# w1w2 b1b2 A + wb -->layer2
pred = sess.run(layer2,feed_dict={x:dateNormal})
predPrice = np.zeros([27,1])
for i in range(0,27):
predPrice[i,0]=(pred*3150)[i,0]
plt.plot(date,predPrice,'b',lw=1)
plt.show()