Tensorflow實現(xiàn)酸奶銷量預(yù)測分析
更新時間:2019年07月19日 14:06:47 作者:卡卡羅特的爸爸
這篇文章主要為大家詳細(xì)介紹了Tensorflow酸奶銷量預(yù)測分析,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Tensorflow酸奶銷量預(yù)測分析的具體代碼,供大家參考,具體內(nèi)容如下
# coding:utf-8
# 酸奶成本為1元,利潤為9元
# 預(yù)測少了相應(yīng)的損失較大,故不要預(yù)測少
# 導(dǎo)入相應(yīng)的模塊
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
BATCH_SIZE=8
SEED=23455
COST=3
PROFIT=4
rdm=np.random.RandomState(SEED)
X=rdm.randn(100,2)
Y_=[[x1+x2+(rdm.rand()/10.0-0.05)] for (x1,x2) in X]
# 定義神經(jīng)網(wǎng)絡(luò)的輸入、參數(shù)和輸出,定義向前傳播過程
x=tf.placeholder(tf.float32,shape=(None,2))
y_=tf.placeholder(tf.float32,shape=(None,1))
w1=tf.Variable(tf.random_normal([2,1],stddev=1,seed=1))
y=tf.matmul(x,w1)
# 定義損失函數(shù)和反向傳播過程
loss=tf.reduce_sum(tf.where(tf.greater(y,y_),(y-y_)*COST,(y_-y)*PROFIT)) #損失函數(shù)要根據(jù)不同的模型進(jìn)行變換
train_step=tf.train.GradientDescentOptimizer(0.001).minimize(loss)
# sess=tf.Session()
# STEPS=20000
# init_op=tf.global_variables_initializer()
# sess.run(init_op)
# for i in range(STEPS):
# start=(i*BATCH_SIZE)%32
# end=start+BATCH_SIZE
# sess.run(train_step,feed_dict={x:X[start:end],y_:Y[start:end]})
# if i%500==0:
#
# print("After %d steps,w1 is %f",(i,sess.run(w1)))
sess=tf.Session()
init_op=tf.global_variables_initializer()
sess.run(init_op)
STEPS=20000
for i in range(STEPS):
start=(i*BATCH_SIZE)%100
end=start+BATCH_SIZE
sess.run(train_step,feed_dict={x:X[start:end],y_:Y_[start:end]})
if i%500==0:
print("After %d steps"%(i))
# print(sess.run(loss_mse))
# print("Loss is:%f",sess.run(loss_mse,feed_dict={y_:Y_,y:Y_}))
print("w1 is:",sess.run(w1))
print("Final is :",sess.run(w1))
xx,yy=np.mgrid[-3:3:.01,-3:3:.01]
grid=np.c_[xx.ravel(),yy.ravel()]
probs=sess.run(y,feed_dict={x:grid})
probs=probs.reshape(xx.shape)
plt.scatter(X[:,0],X[:,1],c=np.squeeze(Y_))
plt.contour(xx,yy,probs,[.9])
plt.show()
通過改變COST和PROFIT的值近而可以得出,當(dāng)COST=1,PROFIT=9時,基于損失函數(shù),模型的w1=1.02,w2=1.03說明模型會往多了預(yù)測;當(dāng)COST=9,PROFIT=1時模型的w1=0.96,w2=0.97說明模型在往少了預(yù)測。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python獲取時間及時間格式轉(zhuǎn)換問題實例代碼詳解
這篇文章主要介紹了python獲取時間及時間格式轉(zhuǎn)換,需要的朋友可以參考下2018-12-12
Python操作json數(shù)據(jù)的一個簡單例子
這篇文章主要介紹了Python操作json數(shù)據(jù)的一個簡單例子,需要的朋友可以參考下2014-04-04
pycharm 更改創(chuàng)建文件默認(rèn)路徑的操作
今天小編就為大家分享一篇pycharm 更改創(chuàng)建文件默認(rèn)路徑的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02

