资源来自于:http://ufldl.stanford.edu/tutorial/
概要:这篇博客是基于神经网络模型的stanford的系列教程,上面使用了NG在coursera上的minfunc函数,对于想试着实现线性回归和逻辑回归,以及之后的卷积神经网络模型的童鞋来说无疑是很好的教程,课程的作业是放在github上的,GreatWall的存在需要翻墙才能拿到,所以共享在这里,其中包括minfunc的工具包,和课程的题目。
百度云链接: https://pan.baidu.com/s/1nvdMFZb 密码: k68f
Linear Regression
损失函数:
预测函数:
梯度:
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
损失函数:
预测函数:
梯度:
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(i)j 表示第i个样本的第j个特征,故X表示的是m个样本的n+1个特征,其中每一列为一个样本。
对于m个样本中的 X(i)0===0
故:
注解:求和的时候是向量求和,所以求和符号只是一个表示求和的过程,而非循环的意思。
matlab实现:
h=theta'*X;//求h(\theta)
cost =h-y;//求向量的差
f=sum(power(cost,2))/(2*m);//向量求和,其中f就是J
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