1. 程式人生 > >利用theano編寫logistic迴歸模型(A Real Example: Logistic Regression)

利用theano編寫logistic迴歸模型(A Real Example: Logistic Regression)

A Real Example: Logistic Regression

程式碼註釋的已經比較詳細,請仔細閱讀!

import numpy
import theano
import theano.tensor as T
import matplotlib.pyplot as plt
rng = numpy.random


N = 400          # 訓練樣本的大小
feats = 784      # 輸入變數的個數

# generate a dataset: D = (input_values, target_class)
# 生成資料集D = (輸入值,目標值)
D = (rng.randn(N, feats), rng.randint(size=N, low=0, high=2))
training_steps = 10000   # 訓練步數

# Declare Theano symbolic variables
# 宣告自變數x,以及每個樣本對應的標籤y
x = T.dmatrix("x")
y = T.dvector("y")

# initialize the weight vector w randomly

# this and the following bias variable b
# are shared so they keep their values
# between training iterations (updates)
# 初始化權重向量w,初始化b=0,並且兩者為共享變數
w = theano.shared(rng.randn(feats), name="w")

# initialize the bias term
# b為偏置量
b = theano.shared(0., name="b")

print("Initial model:")
print(w.get_value())
print(b.get_value())

# Construct Theano expression graph
# 構造Theano表示式
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b))   # Probability that target = 1
# p_1為目標函式等於1的概率
prediction = p_1 > 0.5                    # The prediction thresholded
# prediction為預測
xent = -y * T.log(p_1) - (1-y) * T.log(1-p_1) # Cross-entropy loss function
# xent為交叉熵損失函式
cost = xent.mean() + 0.01 * (w ** 2).sum()# The cost to minimize
# cost為損失函式,並使損失函式最小
# 交叉熵損失函式的平均值+L2正則項,其中權重衰減係數為0.01

gw, gb = T.grad(cost, [w, b])             # Compute the gradient of the cost
# 計算損失函式在w,b方向上的偏導數gw,gb
                                          # w.r.t weight vector w and

# Compile
train = theano.function(
          inputs=[x, y],
          outputs=[prediction, xent],
          updates=((w, w - 0.1 * gw), (b, b - 0.1 * gb)))
# train為訓練所需要的函式
predict = theano.function(inputs=[x], outputs=prediction)
# predict為測試預測函式

# Train
for i in range(training_steps):
    pred, err = train(D[0], D[1])
# D[0]為預測值,D[1]為誤差值
print("Final model:")
print(w.get_value())
print(b.get_value())
print("target values for D:")
print(D[1])
print("prediction on D:")
print(predict(D[0]))