A Real Example: Logistic Regression
原英文文章详见:http://deeplearning.net/software/theano/tutorial/examples.html#logistic-function
代码注释的已经比较详细,请仔细阅读!
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]))