Deep learning:三(Multivariance Linear Regression练习)

转载自:Deep learning:三(Multivariance Linear Regression练习)

 

前言:

  本文主要是来练习多变量线性回归问题(其实本文也就3个变量),参考资料见网页:http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex3/ex3.html.其实在上一篇博文Deep learning:二(linear regression练习)中已经简单介绍过一元线性回归问题的求解,但是那个时候用梯度下降法求解时,给出的学习率是固定的0.7.而本次实验中学习率需要自己来选择,因此我们应该从小到大(比如从0.001到10)来选择,通过观察损失值与迭代次数之间的函数曲线来决定使用哪个学习速率。当有了学习速率alpha后,则本问问题求解方法和上面的没差别。

  本文要解决的问题是给出了47个训练样本,训练样本的y值为房子的价格,x属性有2个,一个是房子的大小,另一个是房子卧室的个数。需要通过这些训练数据来学习系统的函数,从而预测房子大小为1650,且卧室有3个的房子的价格。

 

实验基础:

  dot(A,B):

表示的是向量A和向量B的内积。

  又线性回归的理论可以知道系统的损失函数如下所示:

 

    其向量表达形式如下:

  

  当使用梯度下降法进行参数的求解时,参数的更新公式如下:

  

  当然它也有自己的向量形式(程序中可以体现)。

 

实验结果:

  测试学习率的结果如下:

   

  由此可知,选用学习率为1时,可以到达很快的收敛速度,因此最终的程序中使用的学习率为1.

  最终使用梯度下降法和公式法的预测结果如下:

   

  可以看出两者的结果是一致的。

 

实验主要程序及代码:

 1 %% 方法一:梯度下降法
 2 x = load('ex3x.dat');
 3 y = load('ex3y.dat');
 4 
 5 x = [ones(size(x,1),1) x];
 6 meanx = mean(x);%求均值
 7 sigmax = std(x);%求标准偏差
 8 x(:,2) = (x(:,2)-meanx(2))./sigmax(2);
 9 x(:,3) = (x(:,3)-meanx(3))./sigmax(3);
10 
11 figure
12 itera_num = 100; %尝试的迭代次数
13 sample_num = size(x,1); %训练样本的次数
14 alpha = [0.01, 0.03, 0.1, 0.3, 1, 1.3];%因为差不多是选取每个3倍的学习率来测试,所以直接枚举出来
15 plotstyle = {'b', 'r', 'g', 'k', 'b--', 'r--'};
16 
17 theta_grad_descent = zeros(size(x(1,:)));
18 for alpha_i = 1:length(alpha) %尝试看哪个学习速率最好
19     theta = zeros(size(x,2),1); %theta的初始值赋值为0
20     Jtheta = zeros(itera_num, 1);
21     for i = 1:itera_num %计算出某个学习速率alpha下迭代itera_num次数后的参数       
22         Jtheta(i) = (1/(2*sample_num)).*(x*theta-y)'*(x*theta-y);%Jtheta是个行向量
23         grad = (1/sample_num).*x'*(x*theta-y);
24         theta = theta - alpha(alpha_i).*grad;
25     end
26     plot(0:49, Jtheta(1:50),char(plotstyle(alpha_i)),'LineWidth', 2)%此处一定要通过char函数来转换
27     hold on
28     
29     if(1 == alpha(alpha_i)) %通过实验发现alpha为1时效果最好,则此时的迭代后的theta值为所求的值
30         theta_grad_descent = theta
31     end
32 end
33 legend('0.01','0.03','0.1','0.3','1','1.3');
34 xlabel('Number of iterations')
35 ylabel('Cost function')
36 
37 %下面是预测公式
38 price_grad_descend = theta_grad_descent'*[1 (1650-meanx(2))/sigmax(2) (3-meanx(3)/sigmax(3))]'
39                                      
40                                      
41 %%方法二:normal equations
42 x = load('ex3x.dat');
43 y = load('ex3y.dat');
44 x = [ones(size(x,1),1) x];
45 
46 theta_norequ = inv((x'*x))*x'*y
47 price_norequ = theta_norequ'*[1 1650 3]'
View Code

参考资料:

http://openclassroom.stanford.edu/MainFolder/DocumentPage.php?course=DeepLearning&doc=exercises/ex3/ex3.html

Deep learning:二(linear regression练习)

附上自己的代码:

 1 % exercise: multivariate linear regression
 2 % wrj 2014.11.22
 3 % 
 4 % output: y are the prices
 5 % input: x are the the living area and number of bedrooms
 6 % m = 47 training examples
 7 
 8 clear all; clc;
 9 x = load('ex3x.dat'); y = load('ex3y.dat');
10 m = length(y);
11 x = [ones(m, 1), x];
12 exact_theta = (x'*x) \ x' * y
13 % standardization
14 sigma = std(x);
15 mu = mean(x);
16 x(:, 2) = (x(:, 2) - mu(2))./sigma(2);
17 x(:, 3) = (x(:, 3) - mu(3))./sigma(3);
18 MAX_ITER = 50;
19 % theta = zeros(size(x(1, :)))';
20 theta = zeros(size(x,2), 1); % 推荐用这种
21 plotstyle = {'b', 'r', 'g', 'k', 'b--', 'r--'};
22 alpha = [.01, .03, .1, .3, 1, 1.3]; % init fitting parameters
23 figure;
24 for num_alpha = 1: length(alpha)
25     theta = zeros(size(x, 2), 1); % init fitting parameters 推荐用这种
26     % theta = zeros(size(x(1, :)));不推荐这种
27     Jtheta = zeros(MAX_ITER, 1);
28     for num_iterations = 1: MAX_ITER
29         Jtheta(num_iterations) = .5/m .*(x*theta-y)'*(x*theta-y);
30         grad = 1/m .* x' * (x * theta - y);
31         theta = theta -  alpha(num_alpha).*grad;
32     end
33     plot(0:49, Jtheta(1:50), char(plotstyle(num_alpha)), 'LineWidth', 2);
34     hold on;
35     if (1 == alpha(num_alpha)) % alpha为1时最好
36         theta_best = theta;
37     end
38 end
39 legend('0.01', '0.03', '0.1', '0.3', '1', '1.3');
40 xlabel('Number of iterations');
41 ylabel('Cost J');
42 
43 
44 
45 alpha = 1;
46 price_predicted = [1, (1650-mu(2))./sigma(2), (3-mu(3))./sigma(3)] * theta_best
View Code

 

 

 

posted on 2014-11-22 21:15  fzyzwrj  阅读(65)  评论(0)    收藏  举报