LinearLogisticRegression的实现

资源来自于:http://ufldl.stanford.edu/tutorial/
概要:这篇博客是基于神经网络模型的stanford的系列教程,上面使用了NG在coursera上的minfunc函数,对于想试着实现线性回归和逻辑回归,以及之后的卷积神经网络模型的童鞋来说无疑是很好的教程,课程的作业是放在github上的,GreatWall的存在需要翻墙才能拿到,所以共享在这里,其中包括minfunc的工具包,和课程的题目。
百度云链接: https://pan.baidu.com/s/1nvdMFZb 密码: k68f

Linear Regression

损失函数:

J(θ)=12mi=1m(h(i)(θ)y(i))2

预测函数:
h(i)(θ)=θTX(i)

梯度:
dd(θj)J(θ)=1mi=1mX(i)j(h(i)(θ)y(i))

matlab实现:

 for i=1:m
      f=f+power(theta'*X(:,i)-y(i),2);
  end
  %average
  f=f/(2*m);
  %一共有n个g函数
  g=zeros(size(theta));
  %对于g(j),就是theta_j的偏导
  for j=1:n
      for i=1:m
          g(j)=g(j)+X(j,i)*(theta'*X(:,i)-y(i));
      end
      g(j)=g(j)/m;
  end

Logistic Regression

损失函数:

J(θ)=i=1m(y(i)log(hθ(x(i)))+(1y(i))(1log(hθ(x(i))))

预测函数:
hθ(x)=11+eθTx

梯度:
dd(θj)J(θ)=i=1mx(i)j(h(i)(θ)y(i))

matlab实现:

f = 0;
g = zeros(size(theta));
for i=1:m
   f=f-y(i)*log(1/(1+exp(-theta'*X(:,i))))-(1-y(i))*log(1-1/(1+exp(-theta'*X(:,i))));
end
for j=1:n
   for i=1:m
      g(j)=g(j)+X(j,i)*(1/(1+exp(-theta'*X(:,i)))y(i));
   end
end

Vectorization

For small jobs like the housing prices data we used for linear regression, your code does not need to be extremely fast. However, if your implementation for Exercise 1A or 1B used a for-loop as suggested, it is probably too slow to work well for large problems that are more interesting. This is because looping over the examples (or any other elements) sequentially in MATLAB is slow. To avoid for-loops, we want to rewrite our code to make use of optimized vector and matrix operations so that MATLAB will execute it quickly. (This is also useful for other languages, including Python and C/C++ — we want to re-use optimized operations when possible.)
简言之就是for循环太慢,所以我们需要进行向量化

首先将上面的式子都转化为向量的形式

X=X(1)0,X(2)0,,X(m)0X(1)1,X(2)1,,X(m)1X(1)n,X(2)n,,X(m)nn+1×my=[y(1),,y(m)](1×m)

说明: X(i)j 表示第i个样本的第j个特征,故X表示的是m个样本的n+1个特征,其中每一列为一个样本。

对于m个样本中的 X(i)0===0

故:

θ=θ0θnhθ(X)=[h(1)θ(X),,h(m)θ(X)]1×m=θTXJ(θ)=12m(hθ(X)y)2

注解:求和的时候是向量求和,所以求和符号只是一个表示求和的过程,而非循环的意思。

matlab实现:

h=theta'*X;//求h(\theta)  
cost =h-y;//求向量的差
f=sum(power(cost,2))/(2*m);//向量求和,其中f就是J

g=ddθJ(θ)=[ddθ1J(θ),, ddθnJ(θ)]=1m(XcostT)

g=(X*cost')./m;

下面比较一下向量化前后所花费的时间:
未向量化:
这里写图片描述
向量化:
这里写图片描述
可以看出准确率差不多,但是向量化的时间比未向量化时间快了将近20倍。

上面给出的是线性回归的向量化,下面的代码是逻辑回归的向量化:

h=1./(1+exp(-theta'*X));//求h
f=-(y*log(h)'+(1-y)*(1-log(1-h))'); //求f
g=X*(h-y)';//求g

Gradient Check

梯度检测是为了验证我们所写的梯度公式是不是正确的,在前面两种简单的方法里面,梯度的公式是很简单的,所以并不需要使用梯度检测的方法,而在神经网络模型算法中的梯度是很难求解的,所以梯度检测是需要的。NG在coursera的课程上有提到过。具体的等到神经网络部分在更新源码。
NG coursera课程百度云共享:链接: https://pan.baidu.com/s/1mhCKuq0 密码: bjv6

链接失效的话可以在下方留言。
——2016.12.20

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值