Theano學習筆記(二)——邏輯迴歸函數解析

有了前面的準備,可以用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.判定函數


2.代價函數


3.總目標函數


第二項是權重衰減項,減小權重的幅度,用來防止過擬合的。

#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次訓練,預測結果與標籤已經完全相同了。


歡迎參與討論並關注本博客微博以及知乎個人主頁後續內容繼續更新哦~

轉載請您尊重作者的勞動,完整保留上述文字以及文章鏈接,謝謝您的支持!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章