用 theano 求解 Logistic Regression (SGD 优化算法)

来源:互联网 发布:中国跳水最厉害的知乎 编辑:程序博客网 时间:2024/05/21 09:30

1. model

这里待求解的是一个 binary logistic regression,它是一个分类模型,参数是权值矩阵 W 和偏置向量 b。该模型所要估计的是概率 P(Y=1|x),简记为 p,表示样本 x 属于类别 y=1 的概率:

P(Y=1|x(i))=p(i)=eWx(i)+b1+eWx(i)+b=11+eWx(i)b

当然最终的目标是求解在整个样本集 D={(x(i),y(i)),0<iN} 的对数概率(关于 Wb):

(W,b)=1NiNy(i)logp(i)+(1y(i))log(1p(i))

  • 这里的取均值是为了解耦后续的正则化系数,以及 SGD 时的步长的选择;

当然也可对 W 进行二范数约束(F范数约束,全部项的平方和):

E(W,b)=(W,b)+0.01W2F

2. theano 的使用

实现 theano 下的最小化问题的求解,涉及如下的四个流程:

  • (1)声明符号变量;

    import numpy import theano.tensor as Tfrom theano import shared, functionx = T.matrix()y = T.lvector()w = shared(numpy.random.randn(100))b = shared(numpy.zeros(()))print 'step 1, initial mode: 'print w.get_value(), b.get_value()
  • (2)使用这些变量构建符号表达式图(symbolic expression graph)

    # hypothesisp_1 = 1/(1+T.exp(-T.dot(x, w)-b))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]);prediction = p_1 > .5
  • (3)编译 Theano functions;

    train = function(inputs=[x, y], outputs=[predication, xent], updates={w:w-0.1*gw, b:b-0.1*gb})predict = function(inputs=[x], outputs=predication)
  • (4)调用编译好的函数来执行数值计算;

    N = 4feats = 100D = (numpy.random.randn(N, feats), numpy.random.randi(low=0, high=2, size=(N,)))training_epochs = 10for _ in range(training_epochs):    pred, err = train(D[0], D[1])print 'final model: 'print 'target values for D', D[1]print 'predication on D', predict(D[0])
0 0
原创粉丝点击