Theano學習筆記(二)――邏輯回歸函數解析
來源:程序員人生 發布時間:2014-09-02 17:23:07 閱讀次數:3190次
有了前面的準備,可以用Theano實現一個邏輯回歸程序,邏輯回歸是典型的有監督學習。
為了形象,這里我們假設分類任務是區分人與狗的照片。
首先是生成隨機數對象
importnumpy
importtheano
importtheano.tensor as T
rng= numpy.random
數據初始化
有400張照片,這些照片不是人的就是狗的。
每張照片是28*28=784的維度。
D[0]是訓練集,是個400*784的矩陣,每一行都是一張照片。
D[1]是每張照片對應的標簽,用來記錄這張照片是人還是狗。
training_steps是迭代上限。
N= 400
feats= 784
D= (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))
training_steps= 10000
#Declare Theano symbolic variables
x= T.matrix("x")
y= T.vector("y")
w= theano.shared(rng.randn(feats), name="w")
b= theano.shared(0., name="b")
print"Initial model:"
printw.get_value(), b.get_value()
x是輸入的訓練集,是個矩陣,把D[0]賦值給它。
y是標簽,是個列向量,400個樣本所以有400維。把D[1]賦給它。
w是權重列向量,維數為圖像的尺寸784維。
b是偏倚項向量,初始值都是0,這里沒寫成向量是因為之后要廣播形式。
#Construct Theano expression graph
p_1= 1 / (1 + T.exp(-T.dot(x, w) - b)) #Probability that target = 1
prediction= p_1 > 0.5 # Theprediction thresholded
xent= -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function
cost= xent.mean() + 0.01 * (w ** 2).sum()# The cost to minimize
gw,gb = T.grad(cost, [w, b]) #Compute the gradient of the cost
# (we shall return to this in a
#following section of this tutorial)
這里是函數的主干部分,涉及到3個公式
1.判定函數
 = frac{1}{{I + {e^{ - { heta ^T}X}}}})
2.代價函數
,y}
ight) = left{ egin{array}{r}egin{array}{*{20}{c}}{ - log left( {{h_ heta }left( x
ight)}
ight)}&{egin{array}{*{20}{c}}{if}&{y = 1}end{array}}end{array}egin{array}{*{20}{c}}{ - log left( {1 - {h_ heta }left( x
ight)}
ight)}&{egin{array}{*{20}{c}}{if}&{y = 0}end{array}}end{array}end{array}
ight.)
3.總目標函數
 = - frac{1}{m}left[ {sumlimits_{i = 1}^m {left( {{y^{left( i
ight)}}log {h_ heta }left( {{x^{left( i
ight)}}}
ight) + left( {1 - {y^{left( i
ight)}}}
ight)log left( {1 - {h_ heta }left( {{x^{left( i
ight)}}}
ight)}
ight)}
ight)} }
ight] + frac{lambda }{2}sumlimits_{i = 1}^m {W_i^2} )
第二項是權重衰減項,減小權重的幅度,用來防止過擬合的。
#Compile
train= theano.function(
inputs=[x,y],
outputs=[prediction, xent],
updates=((w, w - 0.1 * gw), (b, b -0.1 * gb)))
predict= theano.function(inputs=[x], outputs=prediction)
構造預測和訓練函數。
#Train
fori in range(training_steps):
pred,err = train(D[0], D[1])
print"Final model:"
printw.get_value(), b.get_value()
print"target values for D:", D[1]
print"prediction on D:", predict(D[0])
這里算過之后發現,經過10000次訓練,預測結果與標簽已經完全相同了。
歡迎參與討論并關注本博客和微博以及知乎個人主頁后續內容繼續更新哦~
轉載請您尊重作者的勞動,完整保留上述文字以及文章鏈接,謝謝您的支持!
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈