1.logistic regression
不懂
2.代码分析
代码:
import numpy
import theano
import theano.tensor as T
rng = numpy.random
N = 400
feats = 784
training_steps = 10000
D = (rng.randn(N,feats), rng.randint(size =N, low =0, high=2))
w = theano.shared(rng.rand(feats), name = 'w')
b = theano.shared(0, name = 'b')
p_1=1/(1+T.exp(-T.dot(x,w)-b))
prediction = p_1 > 0.5
xent = -y * T.log(p_1) - (1-y) * T.log(1 - p_1)
cost = xent.mean() + 0.01 * (w**2).sum()
gw, gb = T.grad(cost, [w, b])
train = theano.function( inputs = [x,y] , outputs = [prediction, xent], updates = ((w, w - 0.01 * gw), (b, b - 0.01 * gb)))
predict = theano.function(inputs = [x], outputs = prediction)
for i in range(training_steps):
train(D[0], D[1])
print(D[1])
print(predict(D[0]))
分析:
这个代码首先随机生成一个N行feats列的矩阵D[0]和一个N位矢量D[1],构成训练数据,分别对应x和y。然后初始化权重w和偏置b。随后建立公式,计算cost损失函数,w和b偏导gw和gb。最后构建训练函数train和显示结果的预测函数predict。最后迭代训练,通过梯度下降的方法,更新w和b的值,使得cost的值最小。最后,可以输出D[1]和predict(D[0])的结果对比进行验证。
笔记:
numpy.random.randint(size,low,high)函数可以生成size大小的矢量,矢量的值low<=x<high
xent.mean()用于计算xent的平均值
交叉熵xent = -y * T.log(p_1) - (1-y) * T.log(1 - p_1)用于表示实际值和预测值的偏离程度
theano.tensor.grad(cost,[w,b])是theano提供的求偏导数的函数传进损失函数和要求偏导数的变量,依次返回偏导数的值
train函数中updates = ((w, w - 0.01 * gw), (b, b - 0.01 * gb)) 0.01是学习率(learning rate),控制梯度下降的速度
网友评论