首页 > 其他 > 详细

Deep Learning by Andrew Ng --- Softmax regression

时间:2015-04-04 16:46:49      阅读:352      评论:0      收藏:0      [点我收藏+]

这是UFLDL的编程练习。

Weight decay(Softmax 回归有一个不寻常的特点:它有一个“冗余”的参数集)后的cost function和梯度函数:

  • cost function:
    J(θ)=?1m??i=1mj=1k1{y(i)=j}logeθTjx(i)kl=1eθTlx(i)??+λ2i=1kj=0nθ2ij
  • 梯度函数:

?θjJ(θ)=?1mi=1m[x(i)(1{y(i)=j}?p(y(i)=j|x(i);θ))]+λθj
p(y(i)=j|x(i);θ))等于UFLDL练习中step2中的h。

bsxfun函数的使用:

  • to prevent overflow, simply subtract some large constant value from each of the
    θTjx(i)
    terms before computing the exponential:
    % M is the matrix as described in the text
    M = bsxfun(@minus, M, max(M, [], 1));
  • use the following code to compute the hypothesis:
    % M is the matrix as described in the text
    M = bsxfun(@rdivide, M, sum(M)

练习题答案(建议自己完成,后参考):

  • softmaxCost.m:
M = theta*data; %exp(theta(l)‘ * x(i))
M = bsxfun(@minus, M, max(M, [], 1));  
h = exp(M);
h =  bsxfun(@rdivide, h, sum(h));  
size(groundTruth);
cost = -1/numCases*sum(sum(groundTruth.*log(h)))+lambda/2*sum(sum(theta.^2));  
thetagrad = -1/numCases*((groundTruth-h)*data‘)+lambda*theta; 
  • softPredict.m:
[index ,  pred]= max(theta * data,[],1);

Deep Learning by Andrew Ng --- Softmax regression

原文:http://blog.csdn.net/meanme/article/details/44873519

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!