微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TensorFlow之——股票预测小Demo

import tensorflow as tf
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt

data = np.linspace(1,15,15)
endPrice = np.array([2500,2630,2222,2214,2013,2810,2547,1566,2700,2800,2900,3000,2100,2400,2500])
beginPrice = np.array([2438,2500,2536,2512,2569,2788,2466,2455,2655,2144,2100,2300,2577,2930,3000])

print(data)
plt.figure
for i in range(0,15):
    #zhu zhuang tu 
    dataOne = np.zeros([2])
    dataOne[0] = i;
    dataOne[1] = i;
    priceOne = np.zeros([2])
    priceOne[0] = beginPrice[i]
    priceOne[1] = endPrice[i]
    if endPrice[i] > beginPrice[i] :
        plt.plot(dataOne,priceOne,'r',lw=8)
    else:
        plt.plot(dataOne,priceOne,'g',lw=8)
    

datanormal = np.zeros([15,1])
pricenormal = np.zeros([15,1])
for i in range(0,15):
    datanormal[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 = 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)#ji li han shu 
#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)

loss = tf.reduce_mean(tf.square(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:datanormal,y:pricenormal})
    pred = sess.run(layer2,Feed_dict = {x:datanormal})
    predPrice = np.zeros([15,1])
    for i in range(0,15):
        predPrice[i,0] = (pred*3000)[i,0]
        
plt.plot(data,predPrice,'b',lw = 1)
plt.show()
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐